I have a small issue I wanted to clear in my head. I have a integer that I increment every second. I pass this integer as a reference to another form and want to display it. So through a button click I instanciate the second form and point the reference towards a local integer in my second form.
I display the value every second on the second form but it will only update when I recreate a new form2 instance.
public partial class Form1 : Form
{
private static int test = 0;
public Form1()
{
InitializeComponent();
TestClass.Init();
Timer t = new Timer();
t.Interval = 1000;
t.Tick += new EventHandler(tick);
t.Start();
}
private void tick(object sender, EventArgs e)
{
++test;
}
public delegate void TestEventHandler(ref int test);
public static event TestEventHandler TestEvent;
internal static void TestEventRaised(ref int test)
{
TestEvent?.Invoke(ref test);
}
private void button1_Click(object sender, EventArgs e)
{
TestEventRaised(ref test);
}
}
public static class TestClass
{
private static Form2 form2;
public static void Init()
{
Form1.TestEvent += new Form1.TestEventHandler(Event);
}
private static void Event(ref int test)
{
if (form2 != null)
{
form2.Close();
form2 = null;
}
form2 = new Form2(ref test);
form2.ShowDialog();
}
}
public partial class Form2 : Form
{
int _test = 0;
public Form2(ref int test)
{
InitializeComponent();
_test = test;
Timer t = new Timer();
t.Interval = 1000;
t.Tick += new EventHandler(tick);
t.Start();
}
private void tick(object sender, EventArgs e)
{
label1.Text = _test.ToString();
}
}
I do not understand why this is not working since when the constructor of form2 is called, I link _test to test. TestClass has its purpose in my "real" code which is to link both Form1 and Form2 that are DLLs.
Do you have any idea why this is not working ?
Thanks !