how to expose an event in user control
-
i have 2 user controls and a main form. On form load, it will load usercontrol1. this is what i did.
public partial class main4 : Form { private UserControl1 userControl1 = new UserControl1(); public main4() { InitializeComponent(); this.Controls.Add(userControl1); } }
In userControl1, i've got a button that when clicked will navigate to userControl2. My question is, how do i expose the button click event in userControl1 to the main form so that when the button is clicked, it will tell the main form to remove userControl1 and then add userControl2?? -
i have 2 user controls and a main form. On form load, it will load usercontrol1. this is what i did.
public partial class main4 : Form { private UserControl1 userControl1 = new UserControl1(); public main4() { InitializeComponent(); this.Controls.Add(userControl1); } }
In userControl1, i've got a button that when clicked will navigate to userControl2. My question is, how do i expose the button click event in userControl1 to the main form so that when the button is clicked, it will tell the main form to remove userControl1 and then add userControl2??Well, your user control is private, so you'll have to implement a custom event that will reflect the button press back to the main form. Alternatively, just make the user control public, and then you can add an event to your mainform:
myMain4.userControl1.myButton.Click += ...
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
i have 2 user controls and a main form. On form load, it will load usercontrol1. this is what i did.
public partial class main4 : Form { private UserControl1 userControl1 = new UserControl1(); public main4() { InitializeComponent(); this.Controls.Add(userControl1); } }
In userControl1, i've got a button that when clicked will navigate to userControl2. My question is, how do i expose the button click event in userControl1 to the main form so that when the button is clicked, it will tell the main form to remove userControl1 and then add userControl2??Handle the Button's
Click
event in the user control and raise your own event:public partial class UserControl1 : UserControl { public event EventHandler Button1Click; public UserControl1() { InitializeComponent(); button1.Click += new EventHandler(button1\_Click); } void button1\_Click(object sender, EventArgs e) { OnButton1Click(e); } protected virtual void OnButton1Click(EventArgs e) { EventHandler eh = Button1Click; if (eh != null) eh(this, e); } }
Now the new Button1Click event is available for the Form/Container and can be handled like any other event.
Dave
Tip: Passing values between objects using events (C#) BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
Handle the Button's
Click
event in the user control and raise your own event:public partial class UserControl1 : UserControl { public event EventHandler Button1Click; public UserControl1() { InitializeComponent(); button1.Click += new EventHandler(button1\_Click); } void button1\_Click(object sender, EventArgs e) { OnButton1Click(e); } protected virtual void OnButton1Click(EventArgs e) { EventHandler eh = Button1Click; if (eh != null) eh(this, e); } }
Now the new Button1Click event is available for the Form/Container and can be handled like any other event.
Dave
Tip: Passing values between objects using events (C#) BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus)hey both. tks i shall try and get back again :-D