I've been trying to make multiform programs for quite some time now, but I run into the same 2 problems again and again:
- Form2 (Or however you call it) treats already set variables as null
- Form1 doesn't accept, when Form2 changes a variable from Form1
It's been so annoying lately, that I've decided to ask here, what I may (or may not) be doing wrong. Here's my quick reproductuion - A program, where you can set a given value using another form:
valueForm
public partial class valueForm : Form
{
public int value;
private changeValueForm change;
public valueForm()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
value = 2;
valueLabel.Text = value.ToString();
change = new changeValueForm();
}
private void change_Click(object sender, EventArgs e)
{
change.Show();
}
private void updatin_Tick(object sender, EventArgs e)
{
valueLabel.Text = change.thisValue.ToString();
}
}
changeValueForm
public partial class changeValueForm : Form
{
private valueForm valueForm;
public int thisValue;
private string[] digits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
public changeValueForm()
{
InitializeComponent();
}
private void changeValueForm_Load(object sender, EventArgs e)
{
valueForm = new valueForm();
thisValue = valueForm.value;
valueBox.Text = thisValue.ToString();
}
private void closeButton_Click(object sender, EventArgs e)
{
if (digits.Contains(valueBox.Text.ToString()))
{
thisValue = Int32.Parse(valueBox.Text);
valueForm.value = thisValue;
}
}
}
So, summa summarum, what is wrong with this code?
P.S. How to pass values between forms in c# windows application? Might have hepled with the 1. problem, but valueForm
still doesn't accept changes made in changeValueForm
- It just stays as it was.