Listbox.selected索引改变变量赋值
你好呀 将列表框中选定索引的值分配给变量的正确方法是什么?用户在列表框中选择一个项目,然后输出根据他们的选择而变化。
使用:
variablename = listbox.text
我在 listBox_SelectedIndexChanged
事件中
并且这有效。当我使用 button_click
事件时,我使用:
variablename = listbox.selectedindex
但这在 listbox_selectedindexchanged
事件中不起作用。
请您告诉我是否可以像我上面那样使用它,或者我是否会遇到问题以及为什么您不能使用 selectedindex 方法。
谢谢!
Hi there
What is the correct way of assigning the selected index's value from a listbox to a variable? The user selects an item in a listbox and then the output changes depending on their selection.
I use:
variablename = listbox.text
in the listBox_SelectedIndexChanged
event and this works.
When I use the button_click
event I use:
variablename = listbox.selectedindex
But this does not work in the listbox_selectedindexchanged
event.
Please could you let me know if it is okay to use it like I did above or if I will run into problems and why you cannot use the selectedindex method.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
答:听起来您的变量是一个字符串,但您试图将 SelectedIndex 属性返回的值分配给它,该值是一个整数。
B. 如果您尝试检索与列表框的 SelectedINDex 关联的项目的值,请使用索引返回对象本身(列表框是对象列表,这些对象通常(但并非总是)是字符串)。
这更直接一点,使用 SelectedItem 属性:
A. It sounds like your variable is a string, and yet you are trying to assign to it the value returned by the SelectedIndex Property, which is an integer.
B. If you are trying to retrieve the value of the item associated with the SelectedINdex of the Listbox, use the Index to return the Object itself (the listbox is a list of Objects, which are often, but not always, going to be strings).
THIS is a little more direct, using the SelectedItem Property:
这取决于您想从列表框的所选项目中实现什么目标。
有几种可能的方法,让我尝试为您的作业解释其中一些方法。
假设您有一个包含两列及其行的数据表...
并且您将此数据表绑定到列表框,如下所示,
如果用户从列表框中选择第二项。
现在,如果您想获取所选项目的显示值(标题),那么您可以执行
OR 操作以获得相同的结果。
要获取所选项目的值成员,您需要执行
现在有些情况,您希望允许用户从列表框中选择多个项目,因此您然后设置
OR
现在假设用户选择两个项目;第二和第三。
因此,您只需迭代
SelectedItems
即可获取显示值,如果您想获取
ID's
而不是Title's
那么...我是我也在翻阅它,如果找到我会发布。
我希望您能加深理解。
祝你好运!
Well it depends what you want to achieve from the listbox's selected item.
There are a couple of possible ways, let me try to explain some of these for your homework.
Suppose you have a data table with two columns, and their rows...
And you bind this data table to your list box as,
If user selects second item from the list box.
Now if you want to get the display value (Title) of the selected item, then you can do
OR even this to get same results.
And to get the value member against the selected item, you need to do
Now there are situations when you want to allow user to select more than one item from the list box, so you then set
OR
Now suppose if user selects two items; second and third.
So you can get the display values by simply iterating through the
SelectedItems
And if you want to get
ID's
instead ofTitle's
then...I am also looking through it, I will post if found.
I hope your understandings build.
Good Luck!