【问题标题】:Listbox index display on Label or Textbox标签或文本框上的列表框索引显示
【发布时间】:2013-03-02 19:42:28
【问题描述】:

有谁知道如何在 Listbox SelectedIndexChanged 事件中使用以下命令。请告诉我。我从互联网上拿了这个例子,我只是想知道如何将它与列表框而不是 Listview 一起使用。

当我在 listbox 上的选定项 [0] 之后没有获得索引选项时。请帮我。 谢谢大家

 private void listView1_SelectedIndexChanged(object sender, EventArgs e)
  {
    textBox1.Text = people[listView1.SelectedItems[0].Index].Name;
  }
}

【问题讨论】:

    标签: c# listview listbox


    【解决方案1】:

    也许不完全准确,但工作正常。

    public partial class Form1 : Form
    {
        List<People> people = new List<People>();
    
        public Form1()
        {
            InitializeComponent();
            people.Add(new People("Joe Montana"));
            people.Add(new People("Alex Smith"));
            people.Add(new People("Colin Kaepernick"));
    
            foreach (People p in people)
            {
                this.listBox1.Items.Add(p.Name);
            }
        }
    
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.textBox1.Text = people[listBox1.SelectedIndices[0]].Name;
        }
    }
    
    class People
    {
        public People(string Name)
        {
            this.Name = Name;
        }
    
        public string Name
        {
            get;
            set;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-16
      • 1970-01-01
      • 2018-03-13
      • 1970-01-01
      • 2014-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多