Is it possible to open the same form multiple times and not have variable conflicts? I have a program controlling 4 different spectrum analyzers and I want to use the same form so I only update changes once. The issue I am having is the labs I am writing this for use different spectrum analyzers so the XML file has setting for each that differ. I noticed that I am actually using the variable settings from the last form that was opened. I fixed this by copy the form and renaming it 4 times, but this seems like I am going to miss changes throughout. The main form called this from buttons.
Spec_AN_form SA_form1 = new Spec_AN_form();
SA_form1.Show();
Spec_AN_form SA_form2 = new Spec_AN_form();
SA_form2.Show();
Spec_AN_form SA_form3 = new Spec_AN_form();
SA_form3.Show();
Spec_AN_form SA_form4 = new Spec_AN_form();
SA_form4.Show();
This did not work correctly so now I have 4 duplicate forms and open each. i.e. Spec_AN_form1 SA_form1 = new Spec_AN_form1(); This does work but now I have to maintain 4 sets of duplicate code. There has to be a better way.
Abhishek YadavPosted Apr 27, 2024, 5:11 AM
One way to achieve this is by passing the settings for each spectrum analyzer as parameters to the constructor of the form when you create a new instance. This way, each instance of the form will have its own separate set of settings and there won't be any conflicts.
For example:
This way, you only need to maintain one set of code in the form, and each instance will have its own set of settings.
Phillip SmilskiPosted Apr 29, 2024, 2:50 PM
It boiled down to incorrect vairible declarations, I was using static and wthat was incorrect.
Phillip SmilskiPosted Apr 29, 2024, 1:36 PM
I will look into MDI, at first glance it is just organizing forms. Hopefully there is more to it that will solve the issue. I was at home when I was asking the question so here are the actual calls.The settings were being passed, sorry for the bad example I had.
Was:
Form level declaration:
SpecAnControls SA1Form;
SpecAnControls SA2Form;
SpecAnControls SA3Form;
SpecAnControls SA4Form;
Inside Main_form()
SA1Form = new SpecAnControls(strSA1_name, strSA1_IP, intSA1_type_num);
SA2Form = new SpecAnControls(strSA2_name, strSA2_IP, intSA2_type_num);
SA3Form = new SpecAnControls(strSA3_name, strSA3_IP, intSA3_type_num);
SA4Form = new SpecAnControls(strSA4_name, strSA4_IP, intSA4_type_num);
Since I load a lot of setting from XML, the last one to load were the settings from the XML. It was strange since the passed through variables were correct and it looked like it was working, however the XML filled varibles overwrote each other. Now I am using:
Form level declaration:
SpecAnControls1 SA1Form;
SpecAnControls2 SA2Form;
SpecAnControls3 SA3Form;
SpecAnControls4 SA4Form;
Inside Main_form()
SA1Form = new SpecAnControls1(strSA1_name, strSA1_IP, intSA1_type_num);
SA2Form = new SpecAnControls2(strSA2_name, strSA2_IP, intSA2_type_num);
SA3Form = new SpecAnControls3(strSA3_name, strSA3_IP, intSA3_type_num);
SA4Form = new SpecAnControls4(strSA4_name, strSA4_IP, intSA4_type_num);
Sam HobbsPosted Apr 28, 2024, 10:40 PM
The data can be refered to as a record. Does that sound right? In other words, the definition of the data is the same but just the actual data varies, right? And you need a separate window (form) for each instance, right?
That is what Multiple Document Interface (MDI) is designed for. You can search for documentation and articles about that. Beginners often use MDI for purposes that MDI was not designed for but your requirements might be perfect for it.