gpt4 book ai didi

.net - 列表框事件 SelectedIndexChanged 在索引未更改时触发

转载 作者:行者123 更新时间:2023-12-04 05:14:41 25 4
gpt4 key购买 nike

我在一个只有 listbox 的 winforms 后面有这个代码作为其唯一控制:

Imports System.Windows.Forms
Public Class Form1

Public Sub New()

' This call is required by the designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.
ListBoxX.Items.Add("hello")
ListBoxX.Items.Add("world")
ListBoxX.Items.Add("foo")
ListBoxX.Items.Add("bar")

End Sub

Private Sub ListBoxX_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBoxX.SelectedIndexChanged
MessageBox.Show("change registered")
End Sub

Private Sub ListBoxX_SelectedValueChanged(sender As Object, e As System.EventArgs) Handles ListBoxX.SelectedValueChanged
MessageBox.Show("change registered X")
End Sub
End Class

如果选择了“hello”,然后单击“hello”,则会出现消息框 - 当然,如果我单击同一项目,则 SelectedIndex 具有 没变 - 那么为什么会触发这个事件?如何确保它仅在索引更改时触发?

最佳答案

检查事件内部是否与之前选择的项目相同,

每次单击时保存最后选择的项目。

Private Sub ListBoxX_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBoxX.SelectedIndexChanged
if Not LastItem = ListBoxX.SelectedItem Then
MessageBox.Show("change registered")
LastItem = ListBoxX.SelectedItem
end if
End Sub

我已经有一段时间没有用 VB 编码了,所以我希望这段代码没问题。

我假设无论选择哪个项目,该事件都将始终触发。

另一种解决方案是覆盖 ListBox 用户控件,但我认为您不需要那样。

关于.net - 列表框事件 SelectedIndexChanged 在索引未更改时触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14446693/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com