Listbox.selected索引改变变量赋值

发布于 2024-10-30 17:52:46 字数 447 浏览 1 评论 0原文

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

使用:

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 技术交流群。

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

发布评论

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

评论(2

谜兔 2024-11-06 17:52:46

答:听起来您的变量是一个字符串,但您试图将 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

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

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

THIS is a little more direct, using the SelectedItem Property:

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
oО清风挽发oО 2024-11-06 17:52:46

这取决于您想从列表框的所选项目中实现什么目标。

有几种可能的方法,让我尝试为您的作业解释其中一些方法。

假设您有一个包含两列及其行的数据表...

ID    Title
_________________________
1     First item's title
2     Second item's title
3     Third item's title

并且您将此数据表绑定到列表框,如下所示,

ListBox1.DisplayMember = "ID";
ListBox1.ValueMember = "Title";

如果用户从列表框中选择第二项。

现在,如果您想获取所选项目的显示值(标题),那么您可以执行

string displayValue = ListBox1.Text;   // displayValue = Second item's title

OR 操作以获得相同的结果。

// displayValue = Second item's title
string displayValue = ListBox1.SelectedItem.ToString();

要获取所选项目的值成员,您需要执行

string selectedValue = ListBox1.SelectedValue;    // selectedValue = 2

现在有些情况,您希望允许用户从列表框中选择多个项目,因此您然后设置

ListBox1.SelectionMode = SelectionMode.MultiSimple;

OR

ListBox1.SelectionMode = SelectionMode.MultiExtended;

现在假设用户选择两个项目;第二和第三。

因此,您只需迭代 SelectedItems 即可获取显示值

string displayValues = string.Empty;
foreach (object selection in ListBox1.SelectedItems)
{
    displayValues += selection.ToString() + ",";
}

// so displayValues = Second item's title, Third item's title,

,如果您想获取 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...

ID    Title
_________________________
1     First item's title
2     Second item's title
3     Third item's title

And you bind this data table to your list box as,

ListBox1.DisplayMember = "ID";
ListBox1.ValueMember = "Title";

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

string displayValue = ListBox1.Text;   // displayValue = Second item's title

OR even this to get same results.

// displayValue = Second item's title
string displayValue = ListBox1.SelectedItem.ToString();

And to get the value member against the selected item, you need to do

string selectedValue = ListBox1.SelectedValue;    // selectedValue = 2

Now there are situations when you want to allow user to select more than one item from the list box, so you then set

ListBox1.SelectionMode = SelectionMode.MultiSimple;

OR

ListBox1.SelectionMode = SelectionMode.MultiExtended;

Now suppose if user selects two items; second and third.

So you can get the display values by simply iterating through the SelectedItems

string displayValues = string.Empty;
foreach (object selection in ListBox1.SelectedItems)
{
    displayValues += selection.ToString() + ",";
}

// so displayValues = Second item's title, Third item's title,

And if you want to get ID's instead of Title's then...

I am also looking through it, I will post if found.

I hope your understandings build.

Good Luck!

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