How to use a variable in form1--> form2

  • Thread starter Thread starter John
  • Start date Start date
J

John

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
 
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,

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.
 
Tnx for the fast response,
however, still missing the point.
Based on a topic @
http://msdn.microsoft.com/library/d...wlkcodegettingvaluefromanotherformvisualc.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
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top