1

I have button on form2. i want when user click this button make something and after that call form1.button click event how can i do that?

2 Answers 2

2

Make the event handler in Form1 public and call that Form1.button_click handler directly. You may give dummy parameters (this, null) if you do not use those in the handler.

Consider rethinking your design. At best, your event handlers should only call some functions of another layer, which actually "does something". That functions you may just as well utilize from form1 or from form2 or from everywhere.

Sign up to request clarification or add additional context in comments.

3 Comments

Or better yet, make the button control public or friend accessible, then call its PerformClick method. Just ever so slightly better than calling event handler methods directly. +1 for "rethink your design".
and your +1 for PerformClick. didn't know that yet.
It's provided by the IButtonControl interface (which, obviously, the Button control implements). It doesn't exist for most other controls or even for any other events, but calling a button's Click event handler is much more common than the other cases.
0

Why do you want to click a button on form one. Have you considered this approach?


Class form 1
{

   button click()
   {
     using (form2 = new form 2)
     {
       if (form2.showdialog()==dialogresult.OK)
       {
          data odata = form2.Data;

          //do work
       }
     }
    }
}


Class form2
{

public property Data
{
    get;
}

   button click()
   {
     if (form valid)
      {
      this.dialogresult = dialogresult.ok;
      }
      else
      {
         this.dialogresult = dialogresult.cancel;
      }
      this.close();
   }
}

2 Comments

I don't even know what this means. Obviously it doesn't compile, but I'm not even sure what it's intended to show. Does the setter for the Data property call the button's Click event handler method? Because that's just wrong.
I was assuming you were opening a second form to get some data or do some work. then using the data in form 1? You didnt really say what you were doing in the question so its open to interpretation! I would use the example above when form 1 is a sales order entry screen and form 2 is a new customer form. button click on form 1 is enter new customer, form 2 takes the customer details, creates the record and the data property is the saved customer. Then if form 2 was saved and not cancelled(thus setting dialog result to OK) then form 1 can set the customer on the sales order to the data property

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.