[英]How can I bind a ListBox to a List AND bind an int?
I have several ListBoxes on my Form and each corresponds to a List of doubles AND one int. 我的表格上有几个ListBoxes,每个ListBoxes对应于一个Doubles和一个Int的列表。
I need to use the int that corresponds to each in the click event of each respective ListBox. 我需要在每个各自的ListBox的click事件中使用与每个事件对应的int。
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. 因此,我希望列表框中的值对应于列表中的双精度数,并且将一个int附加到可以在click事件中使用的列表框中。
I thought about using an object made up of int and List<double>
but I believe this will take away the bind functionality. 我考虑过使用由int和
List<double>
组成的对象,但是我相信这将取消绑定功能。
Can anyone suggest a way I can do this? 谁能建议我可以做到这一点的方法?
Any suggestions are greatly appreciated. 任何建议,不胜感激。
You can use the Tag
property of each ListBox
to store the corresponding integer value. 您可以使用每个
ListBox
的Tag
属性来存储相应的整数值。 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: 然后,您可以通过执行以下操作轻松在
Click Event
方法中获取相应的int值:
private void listBox_Click(object sender, EventArgs e)
{
ListBox curListBox = (ListBox)sender;
int curInt = 0;
if(curListBox.Tag != null) curInt = (int)curListBox.Tag;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.