By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,544 Members | 2,065 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,544 IT Pros & Developers. It's quick & easy.

how to send message from form2 to form1

P: n/a


hi
I don't know how to change for exemple a form1 caption text from form2
don't forget that form2 has created from form1
I want sample code with c#

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
hi Nadir

Follow this.

in the form1 class declare put this statement.

public static Form1 staticVar = null;

i.e it shoudl look like the code below

public class Form1 : System.Windows.Forms.Form
{
public static Form1 myform1 = null;
................ otther code....
assuming that you are trying to open form2 in a button click event in Form1
... write the following code (openform2 is a button in form1)

private void openform2_Click(object sender, System.EventArgs e)
{

myform1 = this;

Form2 form2 = new Form2();
form2.Show();

}
Now in Form2 you can refer to this static Variable in teh following way

assuming you want to set teh caption of Form1 in a button click event in
Form2. (changeForm2Caption is a button in form2)

private void changeForm2Caption_Click(object sender, System.EventArgs e)
{
Form1.myform1 .Text = "test";
}
The above code will change the caption of form1.

hope this helps

cheers

pradeep TP


"nadir b" wrote:


hi
I don't know how to change for exemple a form1 caption text from form2
don't forget that form2 has created from form1
I want sample code with c#

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #2

P: n/a
in the code given by me please read the code

public static Form1 staticVar = null;

as

public static Form1 myform1= null;

i have wrongly typed staticVar in place of myform1

pradeep TP

"pradeep" wrote:
hi Nadir

Follow this.

in the form1 class declare put this statement.

public static Form1 staticVar = null;

i.e it shoudl look like the code below

public class Form1 : System.Windows.Forms.Form
{
public static Form1 myform1 = null;
............... otther code....
assuming that you are trying to open form2 in a button click event in Form1
.. write the following code (openform2 is a button in form1)

private void openform2_Click(object sender, System.EventArgs e)
{

myform1 = this;

Form2 form2 = new Form2();
form2.Show();

}
Now in Form2 you can refer to this static Variable in teh following way

assuming you want to set teh caption of Form1 in a button click event in
Form2. (changeForm2Caption is a button in form2)

private void changeForm2Caption_Click(object sender, System.EventArgs e)
{
Form1.myform1 .Text = "test";
}
The above code will change the caption of form1.

hope this helps

cheers

pradeep TP


"nadir b" wrote:


hi
I don't know how to change for exemple a form1 caption text from form2
don't forget that form2 has created from form1
I want sample code with c#

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #3

P: n/a
Why dont you just pass a reference of form1 to form2 then use it
within form2.

e.g public class Form2: System....
{
public Form frm;

private void changeForm1Caption()
{
frm.Text = "New caption";
}
}

public class Form1: System...
{
onclick_event()
{
Form2 f = new Form2();
f.frm = this;
f.Show();
}
}

On Sun, 20 Mar 2005 01:44:57 -0800, nadir b <na******@caramail.com>
wrote:


hi
I don't know how to change for exemple a form1 caption text from form2
don't forget that form2 has created from form1
I want sample code with c#

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 16 '05 #4

P: n/a
Or set the default Form2 parameterless constructor to be private so it can't
be instantiated. Create a new constructor:

// a Form1 field for the Form2 class
private Form1 form1;

// The new Form2 constructor takes a Form1 parameter
public Form2(Form1 frm1)
{
// the rest of your custructor code)
}

// a method that references the form1 field
private void someMethod(string newCaption)
{
form1.Text = newCaption;
}

// and in your Form1, when you create Form2:
Form2 form2 = new Form2(this);

Dale Preston
MCAD, MCDBA, MCSE
"QWERTY" <no@email.com> wrote in message
news:o0********************************@4ax.com...
Why dont you just pass a reference of form1 to form2 then use it
within form2.

e.g public class Form2: System....
{
public Form frm;

private void changeForm1Caption()
{
frm.Text = "New caption";
}
}

public class Form1: System...
{
onclick_event()
{
Form2 f = new Form2();
f.frm = this;
f.Show();
}
}

On Sun, 20 Mar 2005 01:44:57 -0800, nadir b <na******@caramail.com>
wrote:


hi
I don't know how to change for exemple a form1 caption text from form2
don't forget that form2 has created from form1
I want sample code with c#

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #5

P: n/a
I left a line out of the new Form2 constructor. Here's how it should have
read:

// The new Form2 constructor takes a Form1 parameter
public Form2(Form1 frm1)
{
this.form1 = frm1; // this is the line I left out.
// the rest of your custructor code)
}

"nadir b" <na******@caramail.com> wrote in message
news:#3*************@TK2MSFTNGP12.phx.gbl...


hi
I don't know how to change for exemple a form1 caption text from form2
don't forget that form2 has created from form1
I want sample code with c#

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.