Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# Handles?

Status
Not open for further replies.

robertfah

Programmer
Mar 20, 2006
380
US
Is there a way to have 1 click event handle multiple controls (of the same type) like VB.NET does?

VB.NET code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click

End Sub

C#???

Thanks!
 
Yes, but you have to edit the designer file yourself.

Go into the Designer.cs file, find where the controls are, and manually change the click event hookup to point to your new event handler (using copy/paste, most likely)

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Ok, my solution explorer doesn't show the designer.cs files....how can I make them visible in the solution explorer?
 
Nevermind....I used the following code to itterate through the controls collection:

Code:
    private void BindClickEvents(Control Page)
    {
        foreach (Control ctrl in Page.Controls)
        {
            if (ctrl is CheckBox && ctrl.ID.StartsWith("NA") && ctrl.ID != "NA1")
                ((CheckBox)(ctrl)).CheckedChanged += new EventHandler(NA1_CheckedChanged);
            else
                if (ctrl.Controls.Count > 0)
                    BindClickEvents(ctrl);
        }
    }
 
do you mean something like this?
Code:
public myCtor()
{
   mybutton1.click += DoSomething;
   mybutton2.click += DoSomething;
   mybutton3.click += DoSomething;
}

public void DoSomething(object sender, EventArgs e)
{
}
or this?
Code:
public myCtor()
{
   mybutton.click += DoThis;
   mybutton.click += DoThat;
}

public void DoThis(object sender, EventArgs e)
{
}

public void DoThat(object sender, EventArgs e)
{
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
you dont have to manually edit the designer.cs file.

Just select all controls in the designer window, then in the events pane of the properties window, type a generic funciton name for the click event (i.e. "btn_Click") and hit return. A stub will automatically be generated for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top