Windows Develop Bookmark and Share   
 index > Windows Forms General > Best way to work with listboxes
 

Best way to work with listboxes

I've got a problem in a C# windows program, I’m working with listboxes to let the user choose a value. The problem is that I use a database query to populate the listbox, like this code:

SqlCommand cmd2 = Conn.CreateCommand();

cmd2.CommandText = "SELECT * FROM " + table_Project_Relaties + " WHERE ProjectNr = '" + textBoxOffertenummer.Text + "'";

SqlDataReader rdr2 = cmd2.ExecuteReader();

while (rdr2.Read())

{

// Get the values from the datareader and put them in the listbox

string name = rdr2.GetString(rdr2.GetOrdinal("cmp_name"));

string number = rdr2.GetValue(rdr2.GetOrdinal("cmp_wwn")).ToString();

listBox1.Items.Add(name).ToString();


}

The problem is that in the listbox the user is seeing the names (witch he can select off course) but I want to use the number in the rest of the program. I want to do another database query with this number value.

Is there any solution to get the number of the selected name of the listbox, maybe there is some “hidden�property possible?

Regards,

Frank

Fdv99  Tuesday, March 13, 2007 9:29 AM

You can fill the ListBox with basically any object you like and then set the DisplayMember and ValueMember properties of the ListBox, to the properties of the object that you want to display and use as value.

I made this example, all you need is a Form with a ListBox on it + EventHandlers for ListBox1.SelectedIndexChanged and Form1.Load(double click on theForm and on the ListBox to create these... guess you already knew that).

  1. I created a struct MyListItem which has two private members, mName and mNumber. These members can be accessed through the structs public Properties, Name and Number.
  2. I then filled the ListBox on the Form1.Load Event(I used a for loop, but you can replace it with your DataReader code). Be sure to set the DisplayMember and ValueMember properties of the ListBox.
  3. Finally, on the ListBox1.SelectedIndexChanged Event, Itype cast the ListBox.SelectedItem property to a MyListItem. Now I can use the appropriate property, for example Name for displaying and Number for use as the parameter to a database query.

(It should also be possible to use listBox1.SelectedValue toaccess theValueMember of the selecteditem, but for some reason it justdidn't work for me, you can try and play around with it if you like)

struct MyListItem
{
private string
mName;
private int
mNumber;

public string
Name
{
get { return
mName; }
set { mName = value
; }
}

public int
Number
{
get { return
mNumber; }
set { mNumber = value
; }
}
}

private void Form1_Load(object sender, EventArgs
e)
{
listBox1.BeginUpdate();
listBox1.DisplayMember =
"Name"
;
listBox1.ValueMember =
"id"
;
for (int
i = 0; i <= 10; i++)
{
MyListItem item = new MyListItem
();
item.Name =
"Item" + i.ToString("00"
);
item.Number = i;
listBox1.Items.Add(item);
}
listBox1.EndUpdate();
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs
e)
{
MyListItem item = (MyListItem
)listBox1.SelectedItem;
MessageBox
.Show(item.Number.ToString());
}

Hope this answers your question,

Tomas F

Tomas Fröjdö  Tuesday, March 13, 2007 11:40 AM

Hi,

Iit seems no hidden property in listbox useful for item specific information(it's there for treectrl and listrctrl).

Anyways for the current problem, we can maintain a parellel array or list (need to be taken care of inserting,removing whenever these operations done on list box),so that u can access the element of this array or list based list box .selected index and u can use the number.

Hope this will work out for ur problem.

Thanx,

Ch.T.Gopi Kumar.

TilakGopi  Tuesday, March 13, 2007 10:58 AM

There is no need for a parallel array.

When you add an item to a ListBox, you are actually adding a ListItem object. The ListItem type has a Value property you can use to store a string.

The ListBox.Items.Add method will take either a string or a ListItem object.

So Fdv99 you could do this:

listBox1.Items.Add(new ListItem(name, number));

This works because your number variable is actually a string!

ShellShock  Tuesday, March 13, 2007 11:34 AM

You can fill the ListBox with basically any object you like and then set the DisplayMember and ValueMember properties of the ListBox, to the properties of the object that you want to display and use as value.

I made this example, all you need is a Form with a ListBox on it + EventHandlers for ListBox1.SelectedIndexChanged and Form1.Load(double click on theForm and on the ListBox to create these... guess you already knew that).

  1. I created a struct MyListItem which has two private members, mName and mNumber. These members can be accessed through the structs public Properties, Name and Number.
  2. I then filled the ListBox on the Form1.Load Event(I used a for loop, but you can replace it with your DataReader code). Be sure to set the DisplayMember and ValueMember properties of the ListBox.
  3. Finally, on the ListBox1.SelectedIndexChanged Event, Itype cast the ListBox.SelectedItem property to a MyListItem. Now I can use the appropriate property, for example Name for displaying and Number for use as the parameter to a database query.

(It should also be possible to use listBox1.SelectedValue toaccess theValueMember of the selecteditem, but for some reason it justdidn't work for me, you can try and play around with it if you like)

struct MyListItem
{
private string
mName;
private int
mNumber;

public string
Name
{
get { return
mName; }
set { mName = value
; }
}

public int
Number
{
get { return
mNumber; }
set { mNumber = value
; }
}
}

private void Form1_Load(object sender, EventArgs
e)
{
listBox1.BeginUpdate();
listBox1.DisplayMember =
"Name"
;
listBox1.ValueMember =
"id"
;
for (int
i = 0; i <= 10; i++)
{
MyListItem item = new MyListItem
();
item.Name =
"Item" + i.ToString("00"
);
item.Number = i;
listBox1.Items.Add(item);
}
listBox1.EndUpdate();
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs
e)
{
MyListItem item = (MyListItem
)listBox1.SelectedItem;
MessageBox
.Show(item.Number.ToString());
}

Hope this answers your question,

Tomas F

Tomas Fröjdö  Tuesday, March 13, 2007 11:40 AM

ShellShock wrote:

There is no need for a parallel array.

When you add an item to a ListBox, you are actually adding a ListItem object. The ListItem type has a Value property you can use to store a string.

The ListBox.Items.Add method will take either a string or a ListItem object.

So Fdv99 you could do this:

listBox1.Items.Add(new ListItem(name, number));

This works because your number variable is actually a string!

Note: System.Windows.Forms doesn't have a ListItem class, it's fromthe System.Web.UI.WebControls namespace. So you would have to import WebControls if you wanted to use it, which makes no sense if you are programming for windows.

On the other hand, if you are programming asp.net, you should of course use the ListItem class that already exists, rather than "re-inventing the wheel".

Tomas Fröjdö  Tuesday, March 13, 2007 12:02 PM

You can use google to search for other answers

Custom Search

More Threads

• Printing labels like in MS Word
• Goal Seek in VB6 Code
• Auto-register new type librarys
• Issues around using GetAsyncKeyState(int vKey ) function
• Using an MDI container to run multiple instances of VS application
• PathGradientBrush throwing OutOfMemory exception
• update db...
• How to get files with multiple conditions
• Accessing a form control in a differnt class
• unmanaged code access violation exception