Share via


how to fire event in other form

Question

Monday, March 15, 2010 9:58 AM

hello every one

i want to fire the button click event from form1 in form2

how can i do this?

i want to do this because i load data the user selects from datagridview in form1 to datagridview in form2

and i want the data to be updated in time just when the user chooses some rows and press the button submit

thank in advance for any replies

All replies (7)

Wednesday, March 17, 2010 6:27 PM âś…Answered

Basically, the proper way to do this is by events.

In Form2 create an event:
public event EventHandler PerformForm1Click;

Then in form2, where you want to perform the click do this:

private void InvokeForm1Example()
{
     EventHandler handler = this.PerformForm1Click;
     if (handler != null)
          handler(this, EventArgs.Empty);
}

//meanwhile in Form1, you would do this:
Form2 frm2 = new Form2();
frm2.PerformForm1Click += new EventHandler(frm2_PerformForm1Click)
frm2.ShowDialog();

//in Form1
private void frm2_PerformForm1Click(object sender, EventArgs e)
{
     btn1.PerformClick();
     btn2.PerformClick();
}

John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com


Monday, March 15, 2010 10:07 AM

You can call Button's PerformClick() method.Miha Markic [MVP C#] http://blog.rthand.com


Monday, March 15, 2010 10:08 AM

Hi,

Here i give small sample Algorithm, May it will help you,

form1
{
     form2 frm2;
     datgridview dt1;
     private void button_click()

     {
           frm2 = new form2();
           object obj  = GetdatgridviewValue(dt1);
           frm2.SetDataGridValue(obj);
           this.hide();
           frm2.Show();
      }
}

form2
{
      public void SetDataGridValue(object obj1)
      {
             Assign DataGridview1 value to DataGridview2 value
      }
}

Regards,
S.Subashselvan


Monday, March 15, 2010 10:11 AM | 1 vote

Hi Esso,

I belive you are calling your form 2 from within form 1, therefore the easiest approach i would recommend as long it wouldn't contradict your design principal is as follows,

Assuming you have a button named btnButton1 in Form1 , pass the refference of this button to your Form2 (using the constructor or  a property.) and store it as a refference in Form2 . Assuming you have a btnButton2 in Form2 and need to trigger the button in form 1. use the following method.

private void Form2button1_Click(object sender, EventArgs e)
{
    form1Button1.PerformClick();
}

Hope this helps you.

Best Regards,
Praneeth


Monday, March 15, 2010 10:19 AM

hello Praneeth Wickramasinghe,

i see that ur reply is the most appropriate for me, but i'm sorry that i can't code this idea my own.

////////////////////////////////

Assuming you have a button named btnButton1 in Form1 , pass the refference of this button to your Form2 (using the constructor or  a property.) and store it as a refference in Form2 . Assuming you have a btnButton2 in Form2 and need to trigger the button in form 1. use the following method.

////////////////////////////////

i don't know how to make this

if u can, please write me the code that i can use to implement this idea

thank again for ur reply


Monday, March 15, 2010 10:36 AM

He Esso,

This will be what you are required to do.

Form1 code:

Assume you already have a button named btnShowForm2 winch will call Form2.
Assume you already have a button named btnSave in this form that performs the action you require when the button in form 2 (btnClickForm1Button) is clicked.

Form1
{
   // This button will invoke Form 2 and display it.
   private void btnShowForm2_Clic(object sender, EventArgs e)
   {
        Form2 form2 = new Form2(btnSave);
        form2.Show();
   }
   
   // This is the even that will fire when you click on the Form2 button
   btnSave_Click(object sender, EventArgs e)
   {
        MessageBox.Show("Data Saved");
   }
}

Form2 code:

Assume you have a button named btnClickForm1Button in this form

Form2
{
   Button btnForm1Button;

   //Form2 constructor
   public Form2(Button form1Button)
   {
        InitializeComponent();
        this.btnForm1Button = form1Button;
   }
   // This is the even that will fire the button event in Form1.
   btnClickForm1Button _Click(object sender, EventArgs e)
   {
        btnForm1Button.PerformClick();
   }
}

Hope this helps you,

Best Regards,
Praneeth


Wednesday, March 17, 2010 5:38 PM

Hi Praneeth,

 i want to tell you about another method to fire event from one form to another 

i have made this in form2

 Form2 Code : 

public PreDefinedDatabase()

 { 

 InitializeComponent();

 } 

 public void button1_Form2_Click(object sender, EventArgs e)

 { 

 // code

 }

 and in form 1 : 

PreDefinedDatabase f = new PreDefinedDatabase(); 

f.button1_Click(f, e);

 it worked with me properly, but the question is will this method affect the application any way? thank you for your interest Esso