-
Form inheritance
Suppose, I have a Form "Form1" and a button "btn1" on it in an application
on click event of btn1 I Wrote
MessageBox.Show(self.Name);
Now I inherite "Form2" from "Form1".
and on click event of btn1 I Wrote
MessageBox.Show("Hello");
Now when I press button of Form2 it fires both events.
How to restrict firing event of form1?
-
Re: Form inheritance
The click event is based on delegates - a list of method pointers. So, what's happening here, is you're attaching multiple event handlers to a single event - two messages displayed.
Top of my head [i.e. untested code]:
Form1:
Code:
private void btnClick(object sender, EventArgs e) {
this.OnButtonClicked(sender, e);
}
protected virtual void OnButtonClicked(object sender, EventArgs e) {
MessageBox.Show("Form 1");
}
Form2:
Code:
protected override void OnButtonClicked(object sender, EventArgs e) {
MessageBox.Show("Form 1");
}