Reputation: 973
I have several ListBoxes on my Form and each corresponds to a List of doubles AND one int.
I need to use the int that corresponds to each in the click event of each respective ListBox.
So, I want the values in the listbox to correspond to the doubles in the List AND have an int attached to the listbox that I can use in a click event.
I thought about using an object made up of int and List<double>
but I believe this will take away the bind functionality.
Can anyone suggest a way I can do this?
Any suggestions are greatly appreciated.
Upvotes: 0
Views: 77
Reputation:
You can use the Tag
property of each ListBox
to store the corresponding integer value. Sample code:
ListBox listBox1 = new ListBox();
List<double> doubleList = new List<double>() {1.0, 1.2, 1.3 };
int curInt = 1;
listBox1.DataSource = doubleList;
listBox1.Tag = curInt;
listBox1.Click +=new EventHandler(listBox_Click);
ListBox listBox2 = new ListBox();
doubleList = new List<double>() { 2.0, 2.2, 2.3 };
curInt = 2;
listBox2.DataSource = doubleList;
listBox2.Tag = curInt;
listBox2.Click += new EventHandler(listBox_Click);
this.Controls.Add(listBox1);
this.Controls.Add(listBox2);
Then you can easily get the corresponding int value in the Click Event
method by doing something like:
private void listBox_Click(object sender, EventArgs e)
{
ListBox curListBox = (ListBox)sender;
int curInt = 0;
if(curListBox.Tag != null) curInt = (int)curListBox.Tag;
}
Upvotes: 1