简体   繁体   English

Visual C#:如何将click事件附加到列表框中的项目?

[英]Visual C#: How do I attach a click event to an item in listbox?

在此输入图像描述

See GUI Design above 请参阅上面的GUI设计

I am creating a program where the user enters contacts by name, address, phone, which are stored in parallel arrays. 我正在创建一个程序,用户通过名称,地址,电话输入联系人,这些程序存储在并行数组中。 The program then stores all of the entered contacts into a listbox. 然后程序将所有输入的联系人存储到列表框中。 I then want the user to be able to click on any name and have the complete contact info for that person displayed in an adjacent textbox. 然后,我希望用户能够点击任何名称,并在相邻的文本框中显示该人员的完整联系信息。 My question is how to i create an event where everytime a new item in the listbox is clicked on, their info will be displayed. 我的问题是如何创建一个事件,每次点击列表框中的新项目时,都会显示其信息。

Enter button which stores info into arrays and adds name to contact list: 输入将信息存储到数组中的按钮,并将名称添加到联系人列表:

private void button1_Click(object sender, EventArgs e)
    {
        first[mindex] = txtFirst.Text;
        last[mindex] = txtLast.Text;
        email[mindex] = txtEmail.Text;
        address[mindex] = txtAddress.Text;
        cell[mindex] = txtCell.Text;

        lstContacts.Items.Add(first[mindex] + " " + last[mindex]);

        mindex++;

        txtLast.Text = "";
        txtFirst.Text = "";
        txtEmail.Text = "";
        txtAddress.Text = "";
        txtCell.Text = "";
        txtLast.Focus();

    }

This is what i want to be executed each time a contact name is clicked on: 这是我想在每次点击联系人姓名时执行的内容:

private void DisplayContact()
    {   
        int dispIndex;
        dispIndex = lstContacts.SelectedIndex;

        txtOutput.Text = "Name: " + "\t\t" + first[dispIndex] + last[dispIndex] + Environment.NewLine +
                         "Address: " + "\t\t" + address[dispIndex] + Environment.NewLine +
                         "Cell: " + "\t\t" + cell[dispIndex] + Environment.NewLine +
                         "Email: " + "\t\t" + email[dispIndex];
    }

Just don't know how to connect these things. 只是不知道如何连接这些东西。 Any help appreciated 任何帮助赞赏

On SelectedIndexChanged Properties: 在SelectedIndexChanged属性上:

 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
 {
     var i = listBox1.Items[listBox1.SelectedIndex].ToString();
     MessageBox.Show(i.ToString());

 }

You can use the SelectionChanged event for this. 您可以使用SelectionChanged事件。 Add the SelectionChanged to your listbox SelectionChanged添加到列表框中

private void listBox_SelectionChanged(object sender, EventArgs e)
{
        int dispIndex;
        dispIndex = lstContacts.SelectedIndex;

        txtOutput.Text = "Name: " + "\t\t" + first[dispIndex] + last[dispIndex] + Environment.NewLine +
                         "Address: " + "\t\t" + address[dispIndex] + Environment.NewLine +
                         "Cell: " + "\t\t" + cell[dispIndex] + Environment.NewLine +
                         "Email: " + "\t\t" + email[dispIndex];
}

Hope it helps! 希望能帮助到你!

You want the ListBox to subscribe to the Click event - and from there you can call your DisplayContact method. 您希望ListBox订阅Click事件 - 然后您可以调用DisplayContact方法。

From your Design View, select the ListBox you would like to add this functionality to. 从“设计视图”中,选择要添加此功能的ListBox。 In the properties window, click the lightning-bolt icon to open the events tab. 在属性窗口中,单击闪电图标以打开事件选项卡。

事件标签

From here, scroll until you find the Click Action. 从此处滚动,直到找到“单击操作”。 Double Click the name (of the event, in this case: "Click") and Visual Studio will automatically subscribe this control to the click event and create a method. 双击名称(事件的名称,在本例中为“Click”),Visual Studio将自动将此控件订阅到click事件并创建方法。

单击事件

In the Form's .cs file, you'll find the generated method, which follows a format you're probably familiar with. 在Form的.cs文件中,您将找到生成的方法,该方法遵循您可能熟悉的格式。 But here is where you call your DisplayContact method: 但是这里是您调用DisplayContact方法的地方:

    private void listBox1_Click(object sender, EventArgs e)
    {
        DisplayContact();
    }

You can do this for any event you can think of - but simply adding a method to the Form's code is not enough to make this a success. 您可以为任何您能想到的事件执行此操作 - 但只是在Form的代码中添加方法并不足以使其成功。 Visual Studio automatically generates the code that tells your program the ListBox is waiting for such an event, and that happens in the form's Designer file: Visual Studio自动生成代码,告诉程序ListBox正在等待这样的事件,并且在表单的Designer文件中发生:

设计师代码

^^ That's from the FormName.Designer.cs file, in the InitializeComponent method. ^^这是来自Formize.Designer.cs文件,在InitializeComponent方法中。

Hope this helps. 希望这可以帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM