I'm trying to pass data between forms using delegate. I'm using TrackBar and I'd like to send value from trackBar to second form. The problem is that I want to open two forms simultaneously. When I open first Form and when the slider value is change then show Form2 everything works fine. But when I open two forms simultaneosuly then nothing happen. I'll be grateful for any help :)
Program.cs
public class MultiFormContext : ApplicationContext
{
private int openForms;
public MultiFormContext(params Form[] forms)
{
openForms = forms.Length;
foreach (var form in forms)
{
form.FormClosed += (s, args) =>
{
if (System.Threading.Interlocked.Decrement(ref openForms) == 0)
ExitThread();
};
form.Show();
}
}
}
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MultiFormContext(new Form2(), new Form1()));
}
}
Form1.cs
public delegate void delPassData(TrackBar trackVal);
public partial class Form1 : Form
{
Form2 form2 = new Form2();
public Form1()
{
InitializeComponent();
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
delPassData delegat = new delPassData(form2.someValFromTrackBar);
//form2.Show();
delegat(this.trackBar1);
}
}
Form2.cs
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public void someValFromTrackBar(TrackBar valFromTrackBar)
{
label1.Text = valFromTrackBar.Value.ToString();
}
}