简体   繁体   English

ListBox1_SelectedIndexChanged 未触发

[英]ListBox1_SelectedIndexChanged not firing

I'm trying to make a windows forms app for the first time.我第一次尝试制作 Windows 窗体应用程序。 I have two list boxes.我有两个列表框。 The first one is being populated from a .txt-file when starting the program:第一个是在启动程序时从 .txt 文件填充的:

 public Form1()
        {
                InitializeComponent();
                string[] lines = File.ReadAllLines("C:\\Users\\Mitarbeiter.txt");
                ListBox listBoxMitarbeiter = new ListBox();                           
                listBoxMitarbeiter.Size = new System.Drawing.Size(200, 350);
                listBoxMitarbeiter.Location = new System.Drawing.Point(30, 100);
                this.Controls.Add(listBoxMitarbeiter);
                listBoxMitarbeiter.SelectionMode = SelectionMode.MultiExtended;
                listBoxMitarbeiter.BeginUpdate();
                int c = 0;
                foreach (string line in lines)
                {
                    listBoxMitarbeiter.Items.Insert(c, line);
                    c++;
                }
                listBoxMitarbeiter.EndUpdate();
         }

Now I want the second list box to be created and populated when a item/index is selected in the first one:现在我希望在第一个列表框中选择项目/索引时创建并填充第二个列表框:

private void listBoxMitarbeiter_SelectedIndexChanged(object sender, System.EventArgs e)
        {               
                ListBox listBox2 = new ListBox();
                listBox2.Size = new System.Drawing.Size(200, 350);
                listBox2.Location = new System.Drawing.Point(200, 100);
                this.Controls.Add(listBox2);
                listBox2.SelectionMode = SelectionMode.MultiExtended;
                listBox2.BeginUpdate();
                listBox2.Items.Insert(2,"it works");               
                listBox2.EndUpdate();
        }

I've read that you are supposed to somehow link the event with the listbox.我读过您应该以某种方式将事件与列表框联系起来。 How?如何? Or is it something else entirely.或者它是完全不同的东西。 Please help.请帮忙。

You need to add an event handler to the listbox:您需要向列表框添加一个事件处理程序:

listBoxMitarbeiter.SelectedIndexChanged +=
    new EventHandler(listBoxMitarbeiter_SelectedIndexChanged);

This way, the listBoxMitarbeiter_SelectedIndexChanged() method will be called on each SelectedIndexChanged event.这样,将在每个SelectedIndexChanged事件上调用listBoxMitarbeiter_SelectedIndexChanged()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 listBox1_SelectedIndexChanged - listBox1_SelectedIndexChanged 为什么不能正确显示listBox1_SelectedIndexChanged - why wont this display correctly listBox1_SelectedIndexChanged JS 中 C# 的 listBox1_SelectedIndexChanged 的对应物 - counterpart of C#'s listBox1_SelectedIndexChanged in JS 如何从另一个类中获取listBox1_SelectedIndexChanged事件的值? - How to get the value from a listBox1_SelectedIndexChanged event from another class? 当我从列表中选择项目时,为什么甚至没有出现“ ListBox1_SelectedIndexChanged”? - Why doesnt 'ListBox1_SelectedIndexChanged' even occur when i select item from the list? 私有无效listBox1_SelectedIndexChanged(对象发送者,EventArgs e)的第一行总是收到错误 - Fisrt line of private void listBox1_SelectedIndexChanged(object sender, EventArgs e) always gets an error “SelectedIndexChanged”在ListBox中的“Items.Clear()”之后没有触发 - “SelectedIndexChanged” not firing after “Items.Clear()” in ListBox 列表框SelectedValueChanged / SelectedIndexChanged在数据源更改时不触发 - ListBox SelectedValueChanged/SelectedIndexChanged not firing when data source changes RadioButtonList中的项目未触发SelectedIndexChanged - Item in RadioButtonList not firing SelectedIndexChanged ComboBox SelectedIndexChanged 事件未触发 - ComboBox SelectedIndexChanged event not firing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM