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

How to use a variable in form1--> form2

P: n/a
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 variable in
form2.
Is there a simple method (in VSexpress2005) to get that specific
variable in form2?

TIA,
John
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
You have to work off of instances of the form, therefore is you want FormA
to access any information from FormB, then FormA has to have a reference to
FormB.

For instance (this is just one way you might do it),

MyFormB formB = new FormB();
MyFormA formA = new Form( formA );

Once a reference to the form is made available, then you can access any
public methods or properties.

"John @hotmaill.com>" <JReinders<nospam> wrote in message
news:uh********************************@4ax.com...
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 variable in
form2.
Is there a simple method (in VSexpress2005) to get that specific
variable in form2?

TIA,
John

Nov 21 '05 #2

P: n/a
John,

In .NET, forms are object, just like everything else. VB6 would always
create an instance of a form which you could access by the form's type name.

This was never good practice. Instead, make the variable public to form
2 (internal or public if they are in the same assembly), and then pass the
instance of form 1 to form 2 through a method, by setting a property, or
through the constructor. Then, you can store the form as a field in form2
(or just the value you need, you don't have to pass the whole form) and get
the value when you need it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"John @hotmaill.com>" <JReinders<nospam> wrote in message
news:uh********************************@4ax.com...
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 variable in
form2.
Is there a simple method (in VSexpress2005) to get that specific
variable in form2?

TIA,
John

Nov 21 '05 #3

P: n/a
Tnx for the fast response,
however, still missing the point.
Based on a topic @
http://msdn.microsoft.com/library/de...ormvisualc.asp
I created a Form1 with a readonly textBox1 and a button to open Form2.
Form2 contains a textBox1 to insert something and a button to close
Form2.

The code for Form1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Form1toForm2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Form2 getForm2 = new Form2();
getForm2.ShowDialog();
}

public TextBox TextBox1
{
get
{
return textBox1;
}
}
}
}

and the Code for Form2:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Form1toForm2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Close();
}

private Form1 otherForm;
private void GetOtherFormTextBox()
{
textBox1.Text = otherForm.TextBox1.Text;
}
}
}
The code above is not working because an event is missing to copy
Form2 textBox text to Form1 textBox
So, what is needed?

TIA,
Greetings,
John


On Mon, 21 Nov 2005 11:44:56 -0800, "Peter Rilling"
<pe***@nospam.rilling.net> wrote:
You have to work off of instances of the form, therefore is you want FormA
to access any information from FormB, then FormA has to have a reference to
FormB.

For instance (this is just one way you might do it),

MyFormB formB = new FormB();
MyFormA formA = new Form( formA );

Once a reference to the form is made available, then you can access any
public methods or properties.

"John @hotmaill.com>" <JReinders<nospam> wrote in message
news:uh********************************@4ax.com.. .
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 variable in
form2.
Is there a simple method (in VSexpress2005) to get that specific
variable in form2?

TIA,
John


Nov 23 '05 #4

P: n/a
In order to access the variables of Form1 from Form2, you must *pass a
reference* of Form1 into Form2:
The code for Form1:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
**Note the change in the following line:
Form2 getForm2 = new Form2(this);
getForm2.ShowDialog();
}

public TextBox TextBox1
{
get
{
return textBox1;
}
}
}

and the Code for Form2:

public partial class Form2 : Form
{
**Note the change in the constructor for Form2
public Form2(Form1 form1)
{
InitializeComponent(); otherForm = form1; }

private void button1_Click(object sender, EventArgs e)
{
Close();
}

private Form1 otherForm;
private void GetOtherFormTextBox()
{
textBox1.Text = otherForm.TextBox1.Text;
}
}


Form2's constructor was altered to take a reference to Form1. This was
assigned to your otherForm variable. There are other ways to do this,
this is one way.

Nov 23 '05 #5

P: n/a
Okay after probing your added code it didn't work...
until I found something @
http://www.publicjoe.f9.co.uk/csharp/csharp19.html
where to set the textBox1 from privat (default) to public, that did
the job...
anyway, thanks for the support on this (simple) brainteaser...

John.
On 22 Nov 2005 07:08:41 -0800, "Chris Dunaway" <du******@gmail.com>
wrote:
In order to access the variables of Form1 from Form2, you must *pass a
reference* of Form1 into Form2:
The code for Form1:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{


**Note the change in the following line:
Form2 getForm2 = new Form2(this);
getForm2.ShowDialog();
}

public TextBox TextBox1
{
get
{
return textBox1;
}
}
}



and the Code for Form2:

public partial class Form2 : Form
{


**Note the change in the constructor for Form2
public Form2(Form1 form1)
{
InitializeComponent();

otherForm = form1;
}

private void button1_Click(object sender, EventArgs e)
{
Close();
}

private Form1 otherForm;
private void GetOtherFormTextBox()
{
textBox1.Text = otherForm.TextBox1.Text;
}
}


Form2's constructor was altered to take a reference to Form1. This was
assigned to your otherForm variable. There are other ways to do this,
this is one way.


Nov 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.