简体   繁体   中英

How to call an event handler from one control to the another control?

In Visual C# Form Application, When I Click on the button I want to add to the other controls(like listboxes,labels,textboxes) in same form. How do I do this?

I have no idea what "to come to the other controls" might mean. But the event handlers in your Form derived class is the switchboard. Implement the button's Click event and have it do whatever you want done with any other controls. A trivial example:

    private void button1_Click(object sender, EventArgs e) {
        label1.Text = "You clicked the button!";
    }

In the form designer, add an event handler to the button's Click event.

The form designer will give you a new method like this; add your code into this method:

private void button_Click(object sender, EventArgs e)
{
    // Write some code that uses list boxes, labels, text boxes etc.
}

You question is somewhat unclear, but if you simply want to access other controls on the form, just go ahead and do so:

private void YourButton_Click(object sender, EventArgs e)
{
    string someValue = yourTextBox.Text;
    // do something with the value
}

If you want to add one event handler to many controls, you can do it.

Just go to properties of control you wish to subscribe, find appropriate event from list (ex: onClick) and choise your existed handler.

But this method will be sutable if events compotable.

Describe your task more detail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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