Newbie: Values of a ListBox

  • Thread starter Thread starter Craig Lister
  • Start date Start date
C

Craig Lister

I'm a Delphi developer, trying my hand at c#

A simple question, I am sure:

I'd populate a ComboBox in Delphi, with values from a table, and then at the
same time, populate a TStringList with the corresponding ID's of the items
going into the ComboBox.

Eg:

select id, description from table
loop through results {
add 'Description' to ComboBox;
add 'ID' to StringList;
}

then, on selection of the item in the combobox:

return item = StringList[ ComboBox.ItemIndex ].value;


So, now, how is this achieved in c# ?
Would you declare some time of stringlist? Or maybe the ID can be stored in
the ComboBox?


Websites:
http://www.thelisters.co.uk/
http://www.myschoolmates.com/
 
...
I'd populate a ComboBox in Delphi, with values from
a table, and then at the same time, populate a
TStringList with the corresponding ID's of the items going into the
ComboBox.

Eg:

select id, description from table
loop through results {
add 'Description' to ComboBox;
add 'ID' to StringList;
}

Assuming the query is using a DataReader (e.g. SqlDataReader,
OleDbDataReader, etc) something similar could be:

ComboBox comboBox1 = new ComboBox();
ArrayList stringlist = new ArrayList();

....

while ( datareader.Read() )
{
comboBox1.Items.Add( datareader["description"] );
stringlist.Add( datareader["id"] );
}

then, on selection of the item in the combobox:

return item = StringList[ ComboBox.ItemIndex ].value;


Which would be something like:

return stringlist[ comboBox1.SelectedIndex ];


// Bjorn A
 
Fantastic!
This is exactly what I was looking for!
Thanks Bjorn.

Also, thanks to Cor for the data aware type example.

Bjorn Abelli said:
...
I'd populate a ComboBox in Delphi, with values from
a table, and then at the same time, populate a
TStringList with the corresponding ID's of the items going into the
ComboBox.

Eg:

select id, description from table
loop through results {
add 'Description' to ComboBox;
add 'ID' to StringList;
}

Assuming the query is using a DataReader (e.g. SqlDataReader,
OleDbDataReader, etc) something similar could be:

ComboBox comboBox1 = new ComboBox();
ArrayList stringlist = new ArrayList();

...

while ( datareader.Read() )
{
comboBox1.Items.Add( datareader["description"] );
stringlist.Add( datareader["id"] );
}

then, on selection of the item in the combobox:

return item = StringList[ ComboBox.ItemIndex ].value;


Which would be something like:

return stringlist[ comboBox1.SelectedIndex ];


// Bjorn A
 
Craig,

I missed a part in your text, in my opinion is more .Net

Roughly typed here

SQLConnection conn = new SQLConnection(Connectionstring);
SQLDataAdapter da = new SQLDataAdapter(selectstring,conn);
DataTable dt = new DataTable();
da.Fill(dt);
myComboBox.DataSource = dt;
myComboBox.DisplayMember = "Descripition";
myComboBox.ValueMember = "Id";

You don't have the array, however why do you need that if you get that
direct from the selectedvalue from the combobox?

I hope that this gives a better idea.

Cor
 
Back
Top