Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello every1...

my prob is whenever i try to run a form from program.cs in my windows form application project , i previously running form opens. I can't run any form rather than that form.........
can any one help me pls. I am using C# in VS 2008.
Posted
Updated 6-Aug-11 19:57pm
v2

If you want to run any other form rather then Form1 as John refers!
You can create an object of that Form on an event link Button click and then use Show() or ShowDialog() method.

C#
private void approveButton_Click(object source,EventArgs e)
    {
    Form2 f2=new Form2();
    f2.Show();
    }
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 7-Aug-11 0:19am    
Basically correct, but there is a lot more to it, OP won't be able to use your advice well before considering related issues; so I voted 4.

I credited your answer in my solution, please see.
--SA
In your Program.cs file, change the form being loaded to the one you want to actually use.

C#
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    // change this line to use the desired form object
    Application.Run(new Form1());
}
 
Share this answer
 
Comments
Ehtesam Ahmed 9-Aug-11 6:57am    
I do this .... but still the previous form runs....
In addition to what Una advised I will advice to

1) Make the new form owned by the main form (main form is the one use in Application.Run). To do it, see http://msdn.microsoft.com/en-us/library/system.windows.forms.form.addownedform.aspx[^]; doing so is important for application integrity; when all forms are owned by the main one, the activation of any form looks like activation of the application, no other application's form cannot be put in Z-order between the forms of your application; which is really good.

2) I would set the property ShowInTaskbar to false for all forms except main; the reason is the same as explained above: the main form will represent the whole application in the task bar.

3) Create a new form in a lazy manner: when the variable is null, create a form, otherwise show already created form.

4) You won't be able to work with non-main form when a user close it. To solve this problem, hide the form on attempt to close it; to do that, handle the event FormClosing; see the code sample here: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.closing.aspx[^]; in the code, write form2.Hide() before e.Cancel = true.

5) Try to avoid more than one form per application, at least limit the number of non-main forms by a very few (and perhaps few modal forms); you can leave with just one main form using TabControl and other similar means to show and hide groups of controls, all within one form.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900