Irakli Lekishvili
Irakli Lekishvili

Reputation: 34198

Click button from another form c# winForms

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?

Upvotes: 1

Views: 10987

Answers (2)

WraithNath
WraithNath

Reputation: 18013

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

Upvotes: 0

user492238
user492238

Reputation: 4084

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.

Upvotes: 2

Related Questions