Results 1 to 2 of 2

Thread: Form inheritance

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2004
    Posts
    222

    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?

  2. #2
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    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");
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width