How to call a form when there are 3 forms in a project and data Transfer between the forms in C#?

StackOverflow https://stackoverflow.com/questions/1012631

  •  06-07-2019
  •  | 
  •  

Question

I have 3 forms in my project form1 , form2 , form3 and it was running smoothly now i have added one more in my project form4 . The first three forms are already linked up through ShowDialog().

I don't want to touch Program.cs file.

How can i call form 4 first as start up form ? Earlier form 1 was the first form to appear in my project.

Also i have 2 radio buttons in my form1 rdb1 and rdb2. In my form2 i have openFileDialog attached to a button Select . Now i want when the user selects rdb1 in form1 then the filter of openFileDialog in form2 should open files with only ".XML" as extension and when rdb2 is selected in Form1 then in Form2 only ".TXT" files can be opened.

I am unable to find the syntax for this in intellisense can you please help out?

Thanks in advance..

Was it helpful?

Solution

Can you explain why you don't want to touch your Program.cs file? This is exactly where you change the start-up form.

Change the:

Application.Run(new Form1());

to:

Application.Run(new Form4());

Secondly, you can set the filters on Open- and SaveFileDialog using the Filter property. Set it to a value like this:

XML Files|*.xml

Or for text:

Text Files|*.txt

Edited to add:

To do this from another form:

class Form1 {
  Form2 form2;

  void Form1_Load(object sender, EventArgs e) {
    form2 = new Form2();
  }

  void rdb1_CheckedChanged(object sender, EventArgs e) {
    if (rdb1.Checked) 
      form2.openFileDialog1.Filter = "XML Files|*.xml";
    else
      form2.openFileDialog1.Filter = "Text Files|*.txt";
  }
}

Make sure you have set the Modifiers property of the openFileDialog1 on the Form2 designer to "Public" or "Internal" to allow access to it from outside the class itself.

OTHER TIPS

You have to touch program.cs. That's where the initial form is created and run.

Create a new winforms project and open program.cs. You can see where Application.Run is called with an instance of the startup form. That's what you have to do.

Don't be scared. Everything will be alright.


In response to the comment:

The Form1_Load event is fired within Form1 by Form1. You must go back up the call chain and find where Form1 is instantiated and passed to Application.Run. And that's in program.cs.

Do it. And then go get a copy of CLR Via C#. You won't regret it (as long as you skip the first couple chapters!)

How can i call form 4 first as start up form ?

I don't want to touch Program.cs file.

Why don't you want to touch Program.cs? The form instance passed to Application.Run(..) determines the start up form.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top