VB.NET 列表框选定索引

发布于 2024-12-27 12:53:37 字数 725 浏览 5 评论 0原文

我似乎对列表框中选定的索引有问题。

列表框根据用户的选择插入了各种项目。一个例子是:

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")

这一切都可以正常工作和显示。我遇到的问题是,如果我选择两个 AND 中的第二个。如果我单击列表中的第二个“AND”,然后单击一个按钮来触发方法,则所选索引始终是第一个“AND”的索引。

Dim listIndex as integer = ListBox1.SelectedIndex

我不明白为什么,列表框本身总是显示第二个已选中的列表框,但该操作将针对第一个列表框发生。

任何关于我哪里出错的想法将不胜感激。

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. 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".

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一腔孤↑勇 2025-01-03 12:53:37

看起来不错,但我认为您创建的索引是错误的,或者您可能在单击按钮或其他内容时重置或取消选择列表框...

我这样做并且它有效,选择第二个“时我得到索引= 3” AND”(并且具有更清晰的语法)

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")

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)

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")
猥琐帝 2025-01-03 12:53:37

无论您想实现什么目标,直接处理 ListBox 项目都不是一个好的开始。
您应该使用 ObservableList(Of String) 作为代码的属性并绑定列表
在 xaml 中。
之后您的代码变为: MyItemList.Add("My Item")
该问题可能来自于在代码中使用 SelectedItem 或者来自您正在显示的事实
相同的对象两次(我曾经在显示两次相同对象的复选框中出现奇怪的行为)
您可以通过定义/使用类来存储数据来摆脱它:无论如何,它不仅仅是
关于一个字符串,不是吗?所以你可以有一个带有 ToString 重载的 ItemInfo 类来显示
或者您在 Window 资源中定义一个 DataTemplate,将 ItemInfo 作为 DataType。

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

在您使用的代码中, MyItemList 现在是 ObervableList(Of ItemInfo) : MyList.Add(New ItemInfo(" some text", ...) )
所以你永远不会拥有两次相同的物品。

更多工作,但在这里我们有更坚实的开始来添加数据/功能。

Wathever you want to achieve, handling the ListBox items directly is not a good place to start.
You should use an ObservableList(Of String) as a property of your code and bind the list
in xaml.
After that your code becomes : 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 ? 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.

<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", ...) )
so you never have twice same item.

More work, but here we have more solid start to add data/functions after.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文