【问题标题】:Listbox.selected index change variable assignmentListbox.selected 索引改变变量赋值
【发布时间】:2011-04-03 14:01:33
【问题描述】:

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

我用:

variablename = listbox.text

listBox_SelectedIndexChanged 事件中,这有效。

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

variablename = listbox.selectedindex 

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

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

谢谢!

【问题讨论】:

    标签: vb.net selectedindexchanged


    【解决方案1】:

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

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

    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
    

    【讨论】:

      【解决方案2】:

      这取决于你想从列表框的选定项目中获得什么。

      有几种可能的方法,让我试着为你的家庭作业解释其中的一些。

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

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

      然后你将这个数据表绑定到你的列表框,

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

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

      现在如果你想获取选中项的显示值(Title),那么你可以这样做

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

      或者即使这样也能得到相同的结果。

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

      并且要针对所选项目获取值成员,您需要这样做

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

      现在有些情况是你想让用户从列表框中选择多个项目,所以你然后设置

      ListBox1.SelectionMode = SelectionMode.MultiSimple;
      

      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 那么......

      我也在看,如果找到我会发帖的。

      我希望你能理解。

      祝你好运!

      【讨论】:

        猜你喜欢
        • 2021-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-21
        • 1970-01-01
        相关资源
        最近更新 更多