6
Answers

how to identify which form's button is clicked- still not cleared

Photo of sachi vasishta

sachi vasishta

16y
6.9k
1

Hi,

I have three forms form1, form2, and form3. In form1 I have a button and datagridview. The button on clicking takes control into form3. Similarly in form2 also I have a button and a dgv. The button on click takes control to form3. Now in form3 I want to identify which forms button is pressed. I mean if I have clicked form1's button then I will access form1's dgv or if I have clicked form2's button then I will access form2's dgv. How to know which form's button I have clicked in form3?

thanks

Answers (6)

0
Photo of Vimal Kandasamy
631 1.5k 209.6k 16y
Sachi,you have to pass current object of form1 or form2 to form3.Then only changes made in form3 will affect form1 & form2.

like
in form1

Form3 f=new Form3("form1",this);
f.showDialog();

and in form3

String formname;
Form1 f1;
public Form3(String fname,Form1 obj)
{
 formname=fname;
 f1=obj;
}

after that access it like
f1.dataGridView1.DataSource


0
Photo of sachi vasishta
NA 252 0 16y

thanks vimal.

Vimal I have a small doubt, In form3 if I want to access form1 or form2's dgv or other controls like form1.dataGridView1.DataSource  then I have to use form1 and form2's objects right , but Iwant to know whether I have to just declare form1 and form2's object such as

Form1 form1;

Form2 form2;

in form3, or I have to do something more to access dgv or other contols of form1 or form2 in form3.

Thanks

0
Photo of Suchit Khanna
NA 2.7k 409.8k 16y
Sachi I think Vimal clarified it with code, but If still there is some issue, could you please share the code you are using, may be it could give a clear picture.
0
Photo of Vimal Kandasamy
631 1.5k 209.6k 16y
Sachi,you can achieve this by passing info via overloaded constructor.thats enough.

I have used below code to identify which form for my applications.

in form1

Form3 f=new Form3("form1");
f.showDialog();

in form2

Form3 f=new Form3("form2");
f.showDialog();

and in form3

String formname;
public Form3(String fname)
{
 formname=fname;
}

and in form3's loading event or other events
just check like

if(formname=="form1")
{
  //code to access form1's dgv and others
}
if(formname=="form2")
{
//code to access form2's dgv and others
}
whenever you need, just refer formname varible to identify which form's button is clicked.

0
Photo of sachi vasishta
NA 252 0 16y

suchit I know how to write overloaded constructor and also I know to set form1 and form2's button's  modifer property to public, but I want to know which property I have to use to know which form's button is clicked? like

form1.button1.? - here I am not getting the solution

 

0
Photo of Suchit Khanna
NA 2.7k 409.8k 16y
One possible solution would be to create an overloaded constructor for Form3 and wherever you are instantiating the Form3 in your code pass the button clicked as a parameter to Form3 overloaded constructor, which you could catch in the Form3 and process accordingly.