I am having a bit of trouble passing variable between forms. I have created a button array and want to pass the button text to the next form. But this is just returning a Null value
In the first form
private string staffmem;
public string Staffmem
{
get
{
return staffmem;
}
}
public void ClickButton(Object sender, EventArgs e)
{
Button btn = (Button)sender;
staffmem = btn.Text;
MessageBox.Show("Welcome " + staffmem);
MainScreen ms = new MainScreen();
ms.Show();
}
and then in the second form
private void MainScreen_Load(object sender, EventArgs e)
{
Form1 f1 = new Form1();
staffmem = f1.Staffmem;
Any help would be much appreciated. Thanks in advance
You are not passing anything, you are creating a new Form
Form1 f1 = new Form1();
staffmem = f1.Staffmem;
If the data is to be shared - a really poor way is to make the string static
private static string staffmem;
You are creating new forms in both ends of the communication: when the button is clicked and when you want to retrieve the text -- this results in accessing the Staffmem
field on a newly created object, which field has not been set to anything, hence the returned null
value.
In order to be able to retrieve the text, you need to have the same Form1
object to MainScreen
when it is created:
MainScreen ms = new MainScreen(form1);
where form1
is the actual Form1
object, and store it in MainScreen
as a member variable
// in MainScreen class
private Form1 f1;
Then access Staffmem
on that object on the stored member variable:
// in MainScreen_Load()
staffmem = f1.Staffmem;
Note : depending on your need, you might not want to create a new MainScreen
from every time the button is clicked either. In that case (if you already have a created MainScreen
that you want to communicate with), you need to pass the MainScreen
object to the Form1
object as well, when the Form1
object is created (following the above outlined techniqe)
You're creating a whole new Form1
- wherein, you wish to use the existing instance.
What you need to do, is define Staffmem
on your MainScreen
object, and set it after you create it.
MainScreen ms = new MainScreen();
ms.Staffmem = btn.Text;
Then, in your MainScreen_Load: Set the value.
You are working with 2 different objects, first you create a Form1 object and click the button will set the property value on that single object, however in your mainscreen you create a new instance of Form1.
You could pass the form object as a reference to MainScreen itself.
MainScreen ms = new MainScreen(this);
ms.Show();
And then in the MainScreen class update the constructor to.
public MainScreen(Form1 form)
{
this.form1 = form;
}
And keep the reference in a field, like so.
private Form1 form1;
After this you can do this again,
private void MainScreen_Load(object sender, EventArgs e)
{
staffmem = form1.Staffmem;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.