简体   繁体   English

VB.NET列表框选定的索引

[英]VB.NET List Box Selected Index

I seem to be having issues with the selected index on a list box. 我似乎在列表框上的所选索引有问题。

The list box is having various items inserted depending on the user selection. 列表框根据用户选择插入了各种项目。 An example would be: 一个例子是:

LiIndex = ListBox1.Items.Count      
ListBox1.Items.Insert(LiIndex, "Item1")

LiIndex = ListBox1.Items.Count      
ListBox1.Items.Insert(LiIndex, "AND")

LiIndex = ListBox1.Items.Count      
ListBox1.Items.Insert(LiIndex, "Item2")

LiIndex = ListBox1.Items.Count      
ListBox1.Items.Insert(LiIndex, "AND")

LiIndex = ListBox1.Items.Count      
ListBox1.Items.Insert(LiIndex, "Item3")

This all work and displays without a problem. 所有这些都可以正常工作,并且显示没有问题。 The issue I have is if I select the second of the two AND's. 我的问题是,如果我选择两个AND中的第二个。 If I click the second "AND" in the list and then a button to fire a method, the selected index is always the index of the first "AND". 如果我单击列表中的第二个“ AND”,然后单击一个用于触发方法的按钮,则所选索引始终是第一个“ AND”的索引。

Dim listIndex as integer = ListBox1.SelectedIndex

I can't work out why, the listbox itself will always show the second one as selected, but the action will happen against the first one. 我无法弄清楚为什么,列表框本身将始终显示选中的第二个,但操作将针对第一个。

Any ideas as to where I am going wrong would be greatly appreciated. 关于我要去哪里的任何想法将不胜感激。

It looks ok, but i think the index you are creating is wrong, or maybe you are reseting or deselecting the listbox when clicking the button or something... 看起来还可以,但是我认为您创建的索引是错误的,或者您可能在单击按钮或其他操作时正在重置或取消选择列表框...

I did this and it worked, i get index = 3 when selecting the second "AND" (and with a cleaner syntax) 我做到了,它奏效了,选择第二个“ AND”时,索引为3(语法更简洁)

ListBox1.Items.Insert(ListBox1.Items.Count, "Item1")
ListBox1.Items.Insert(ListBox1.Items.Count, "AND")
ListBox1.Items.Insert(ListBox1.Items.Count, "Item2")
ListBox1.Items.Insert(ListBox1.Items.Count, "AND")
ListBox1.Items.Insert(ListBox1.Items.Count, "Item3")

Wathever you want to achieve, handling the ListBox items directly is not a good place to start. 无论您要实现什么目标,直接处理ListBox项都不是一个好的开始。 You should use an ObservableList(Of String) as a property of your code and bind the list in xaml. 您应该将ObservableList(Of String)用作代码的属性,并将列表绑定到xaml中。
After that your code becomes : MyItemList.Add("My Item") 之后,您的代码将变为:MyItemList.Add(“ My Item”)
The issue might come from using SelectedItem in your code OR from the fact your're displaying same object twice (i once had a strange behaviour in a CheckBox displaying twice same object) you can get rid of that by defining/using a class to store the data : anyway, it is not just about a string, no ? 问题可能来自在代码中使用SelectedItem,或者来自您两次显示同一对象的事实(我曾经在一次显示两次相同对象的CheckBox中发生过奇怪的行为),可以通过定义/使用类存储来摆脱这种情况数据:无论如何,它不只是字符串,不是吗? so you can have an ItemInfo class with a ToString Overload for it to display OR you define a DataTemplate in your Window resource that has ItemInfo as DataType. 因此,您可以使用带有ToString重载的ItemInfo类来显示它,或者在Window资源中定义一个以ItemInfo为DataType的DataTemplate。

<DataTemplate DataType="{x:Type l:ItemInfo}">
        <TextBlock Text="{Binding ItemText}" />
</DataTemplate>  

and in your code you use, MyItemList beeing now an ObervableList(Of ItemInfo) : MyList.Add(New ItemInfo(" some text", ...) ) 在您使用的代码中,MyItemList现在变成了ObervableList(Of ItemInfo):MyList.Add(New ItemInfo(“ some text”,...))
so you never have twice same item. 所以您永远不会有两次相同的项目

More work, but here we have more solid start to add data/functions after. 需要做更多的工作,但是在此之后,我们在开始添加数据/功能方面有了更扎实的开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM