473,387 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

how to send message from form2 to form1



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
5 3768
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: | last post by:
i have a form name Form1 i want to create a copy of Form2 which know th refrence of Form1. example: private: System::Void button1_Click(System::Object * sender, System::EventArgs * e) { Form2...
4
by: Maarten | last post by:
i have a parrent form, form1 and i child form, form2 when i press a button in form2, tha data of the text1 of form2 must be places in text1 of form1 something like form1.text1 =...
0
by: msxkim | last post by:
I would like to know how to send messages to other form when one form is closed. For example, I have Form1. Form2 is opened from Form 1. When you close Form2, can you access Textbox control on...
5
by: John | last post by:
Hi, I can't find a simple example for a simple(?) problem. I am working on an application with a variable in form1, that variable is needed in form2 for a calculation but i can't get that...
9
by: Marcel Saucier | last post by:
Hello everybody, I want to declare an array in form1, load it in form1 and then access that array in form2. New in VB, It took me a while to understand that declaring variables right under class...
3
by: Karan | last post by:
I am calling finalize when form2 loads and deactivates form1 which closes form1. However, same thing is not happening in form2 because finalize is already called. Does anybody has solution to it....
5
by: GoGs | last post by:
HEEEELLLLLPP I hawe two Open form... Form1 and Form2 How I can from Form2 send text to textbox of Form1 //public System.Windows.Forms.TextBox textBox1; //Form1 fo = new Form1();...
7
by: Boki | last post by:
Hi All, I want to change WindowState of form1 from form2. I tried these two methods, but no luck on both. (1) Declare a public method: /* function of form1 */ public void...
5
by: lukasmazur | last post by:
Hi I have a problem with using listBox1. I have a two forms form1 and form2. In form1 are controls listBox1, textBox1 and button witch creating object of class Form2. In class Form2 I create a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.