what i need is
call the 'buttonclick event' for a button on form2,from form1
or call 'perform_click' for the button on form2,from form 1.

i am able to call PERFORMCLICK when the button is in the same form..but when it ison a different form,nothing happens..

please help me with this..
thanks a lot in advance :)

I would rather put the code which is not in the button Click event into a new method. Then you call this method in button clck in that form, and from another form, like:

//other form whih has a button click event:
private void button_Click()
{
     MethodToExecute();
}

public void MethodToExecute()
{
    //put that code in here
}

//on ther from, from which you want to call the method (instead of button click event);
private void CallMethodOnOtherForm()
{
     OtherForm of = new OtherForm();
     of.MethodToExecute();
     of.Show();
}

This is way better from one important reason - to keep the controls private which belong to forms. If you want to access it, you need to make the pubic. And this is not a good way of coding.

mmm..i dont know whats wrong..it just opens the form..the method is not being called.. :) but thanks a lot for posting the code

i made the button public.the method is already public..there is no error..but nothing happens...mmm..

Do you have a handle to the other form? Here's how I have done it (when I first used .NET).

class FormOne{
   public void SomeEvent(){ FormTwo f2 = new FormTwo(this); }
}

class FormTwo{
   public FormTwo(FormOne ofm) {
     ofm.SomeEvent();
   }
}

Edited 5 Years Ago by pseudorandom21: n/a

what i need is...

i have a 'print' button on FORM1.FORM2 is already open.we give different values in the text boxes of FORM2.
when i click the PRINT button on FORM1,details in FORM2 need to get printed..
i tried
FORM2 f=new FORM2();
f.printmethod();


what happens is,the FORM2 details are getting printed as it was in the design phase..that is,the text box values are empty.
i need to find out how to print the values that is in the Textboxes of opened form FORM2,when we click the print button on FORM1..

THANKS IN ADVANCE

pleasssssssssssssssssssssssssssssssssssssssseee..help...................

Mate, my code works fine, you did something wrong. Ok, let me rephrase it a bit:

//on ther from, from which you want to call the method (instead of button click event);
private void CallMethodOnOtherForm() //this is maybe some of your buttons from where you wanna open 2nd form!!
{
     OtherForm of = new OtherForm();
     of.MethodToExecute();
     of.Show();
}

//other form which has a button click event:
private void button_Click()
{
     //instead of having code in here, I created new method, which you an call from this form..
     MethodToExecute();
}

public void MethodToExecute() //call this method
{
    //... or from other form!!
 
    //the method to execute:
    int a = 1 + 2;
    MessageBox.Show("Sum is: " + a.ToStirng());
}

thanks again for your help Mitja..
the method can be called...
but here,the problem is,at the run time FORM2 textboxes are getting values which user types.When PRINT button is clicked from FORM1,FORM2 is getting printed with its default values,and not the one which is given by the user when the FORM2 had focus...
mmmm...
its ok...
i dont know if delegates need to be used when both the forms are open... but i dont know to use delegates for communication..i will try

thanks a lot again.. :)

This article has been dead for over six months. Start a new discussion instead.