从列表框中选择项目

时间:2022-12-18 23:00:59

I read in a particular site that the code:

我在特定网站上读到了这段代码:

 txtBox = lstBox.ItemData(lstBox.ListIndex())

will provide me with the item that is clicked in the listbox where the ListIndex provides me with the row and ItemData provides me with the data in the row. However on trying to implement it I've noticed that there are no such option for my listbox. Is there anything that must be added to the code to avail such options? I am programming using the c# language

将为我提供列表框中单击的项目,其中ListIndex为我提供了行,而ItemData为我提供了行中的数据。然而,在尝试实现它时,我注意到我的列表框没有这样的选项。是否有任何必须添加到代码中以利用这些选项?我正在使用c#语言进行编程

2 个解决方案

#1


i think you got some vb6 code that you're trying to implement in a .net language ... from the .net help:

我想你有一些vb6代码,你试图用.net语言实现...来自.net帮助:

In Visual Basic 6.0, the ItemData property for a ComboBox control can be set at design time in the Properties window to associate an Integer with a ComboBox item.

在Visual Basic 6.0中,可以在“属性”窗口中的设计时设置ComboBox控件的ItemData属性,以将Integer与ComboBox项关联。

In Visual Basic 2008, the ItemData property no longer exists. The Microsoft.VisualBasic.Compatibility library contains a SetItemData function that can be used to associate an Integer with an item; the GetItemData function can be used to retrieve the item.

在Visual Basic 2008中,ItemData属性不再存在。 Microsoft.VisualBasic.Compatibility库包含一个SetItemData函数,可用于将Integer与项关联; GetItemData函数可用于检索项目。

best regards, don

最好的问候,不要

#2


Use ListBox.SelectedItem. Note that this returns an Object, so you'll have to cast whatever it is back to the type you require. For example:

使用ListBox.SelectedItem。请注意,这会返回一个Object,因此您必须将它返回的所有内容转换为您需要的类型。例如:

var selectedItem = (int)myListBox.SelectedItem;

#1


i think you got some vb6 code that you're trying to implement in a .net language ... from the .net help:

我想你有一些vb6代码,你试图用.net语言实现...来自.net帮助:

In Visual Basic 6.0, the ItemData property for a ComboBox control can be set at design time in the Properties window to associate an Integer with a ComboBox item.

在Visual Basic 6.0中,可以在“属性”窗口中的设计时设置ComboBox控件的ItemData属性,以将Integer与ComboBox项关联。

In Visual Basic 2008, the ItemData property no longer exists. The Microsoft.VisualBasic.Compatibility library contains a SetItemData function that can be used to associate an Integer with an item; the GetItemData function can be used to retrieve the item.

在Visual Basic 2008中,ItemData属性不再存在。 Microsoft.VisualBasic.Compatibility库包含一个SetItemData函数,可用于将Integer与项关联; GetItemData函数可用于检索项目。

best regards, don

最好的问候,不要

#2


Use ListBox.SelectedItem. Note that this returns an Object, so you'll have to cast whatever it is back to the type you require. For example:

使用ListBox.SelectedItem。请注意,这会返回一个Object,因此您必须将它返回的所有内容转换为您需要的类型。例如:

var selectedItem = (int)myListBox.SelectedItem;