gpt4 book ai didi

vb.net - Listbox.selected索引改变变量赋值

转载 作者:行者123 更新时间:2023-12-02 01:27:43 25 4
gpt4 key购买 nike

你好将列表框中选定索引的值分配给变量的正确方法是什么?用户在列表框中选择一个项目,然后输出根据他们的选择而变化。

我使用:

variablename = listbox.text

listBox_SelectedIndexChanged 事件中,这有效。

当我使用 button_click 事件时,我使用:

variablename = listbox.selectedindex 

但这在 listbox_selectedindexchanged 事件中不起作用。

请告诉我是否可以像我上面那样使用它,或者我是否会遇到问题以及为什么不能使用 selectedindex 方法。

谢谢!

最佳答案

A.听起来您的变量是一个字符串,但您试图将 SelectedIndex 属性返回的值分配给它,该值是一个整数。

B.如果您尝试检索与列表框的 SelectedINDex 关联的项目的值,请使用索引返回对象本身(列表框是对象列表,通常但不总是字符串)。

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
'THIS retrieves the Object referenced by the SelectedIndex Property (Note that you can populate
'the list with types other than String, so it is not a guarantee that you will get a string
'return when using someone else's code!):
SelectedName = ListBox1.Items(ListBox1.SelectedIndex).ToString
MsgBox(SelectedName)
End Sub

这更直接一些,使用 SelectedItem 属性:

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

'This returns the SelectedItem more directly, by using the SelectedItem Property
'in the event handler for SelectedIndexChanged:
SelectedName = ListBox1.SelectedItem.ToString
MsgBox(SelectedName)

End Sub

关于vb.net - Listbox.selected索引改变变量赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5530007/

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