VB.NET中ListBox选中项索引变化时,如何更新变量赋值?

2026-05-06 10:131阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计423个文字,预计阅读时间需要2分钟。

VB.NET中ListBox选中项索引变化时,如何更新变量赋值?

您好,将所选索引值从列表框分配给变量的正确方法是什么?用户选择列表框中的项目后,输出将根据其选择进行更新。我使用:variablename=listBox.text 在listBox_SelectedIndexChanged事件中,这样做的原理是什么?

嗨,您好
将所选索引的值从列表框分配给变量的正确方法是什么?用户选择列表框中的项目,然后输出会根据其选择而更改.

我用:

variablename = listbox.text

在listBox_SelectedIndexChanged事件中,这是有效的.

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

variablename = listbox.selectedindex

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

如果可以像我上面那样使用它,或者如果我遇到问题以及为什么你不能使用selectedindex方法,请你告诉我.

谢谢!

答:听起来你的变量是一个字符串,但你试图将SelectedIndex属性返回的值赋给它,这是一个整数.

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

VB.NET中ListBox选中项索引变化时,如何更新变量赋值?

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

本文共计423个文字,预计阅读时间需要2分钟。

VB.NET中ListBox选中项索引变化时,如何更新变量赋值?

您好,将所选索引值从列表框分配给变量的正确方法是什么?用户选择列表框中的项目后,输出将根据其选择进行更新。我使用:variablename=listBox.text 在listBox_SelectedIndexChanged事件中,这样做的原理是什么?

嗨,您好
将所选索引的值从列表框分配给变量的正确方法是什么?用户选择列表框中的项目,然后输出会根据其选择而更改.

我用:

variablename = listbox.text

在listBox_SelectedIndexChanged事件中,这是有效的.

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

variablename = listbox.selectedindex

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

如果可以像我上面那样使用它,或者如果我遇到问题以及为什么你不能使用selectedindex方法,请你告诉我.

谢谢!

答:听起来你的变量是一个字符串,但你试图将SelectedIndex属性返回的值赋给它,这是一个整数.

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

VB.NET中ListBox选中项索引变化时,如何更新变量赋值?

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