I my game on the WF I have a main form (left) and it's child form (right). In the child form I can change value of the progress bar and it works fine. But I have the same progress bar on the main form and I want to update it too. I can't do it.
Code of the child form:
private MainForm mainForm = new MainForm();
// Button "Close"
private void button2_Click(object sender, EventArgs e)
{
mainForm.MyInitializeComponent(); // This code must update value on the main form
this.Close();
}
Code of the main form:
public void MyInitializeComponent()
{
label33.Text = indicators.Food + "%";
progressBar1.Value = indicators.Food;
}
If I'll open child form after I closed it, I see the changed value, ie it is stored. In both forms, the value is taken from a single variable.
Indicators.cs:
public sealed class Indicators
{
public Indicators()
{
Indicators.food = 75;
}
private static int food;
public int Food
{
get { return food; }
set { food = value; }
}
}
Have the main form add a form closed handler to the child form to update it's UI when the child is closed:
var childForm = new ChildForm();
childForm.FormClosed += (s, args) => MyInitializeComponent();
//...
childForm.Show();
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments