views:

124

answers:

3

Hello,

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..

+2  A: 

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.

Jon Grant
Thanks this i know to put the filter but i dont know how to put the condition above it, the condition is when the rdb1 is selected in form1 den opf.filter = "xml|(*.xml)";How to write dis condition?
crazy_itgal
Hmm this depends on a lot on your project structure. Make sure that you keep your reference to Form2 in a class instance variable, not a local variable to the function you create it. Also set the Modifiers property to Public or Internal on the openFileDialog1 in the Form2 designer. Would show you a code example but they don't work in comments :(
Jon Grant
A: 

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.

frou
+1  A: 

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!)

Will
Thanks but cant it be changed through form1_Load event?
crazy_itgal