Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I m new to .NET framework . In a single project I hav designd 2 form , wen i try to run form it s running only 1st form , i m not able to run 2nd form . I am using Visual Studio 2008 . Pls help me to run selected forms.
Posted
Comments
[no name] 20-Sep-13 9:04am    
Open your "Program.cs" file and change the "Application.Run(new Form1());" to your "other form".
Raghavendra M 20-Sep-13 9:30am    
Thank u . . , I got it . .

In windows application only one form is allowed to run at a time until and unless you provided any link for the second form in the first.
To run the second form follow the steps

Go to Solution explorer->Click on My Project folder->one new window will open where at left side you will find one application tab->after clicking on that you will see one option called StartUp URI->click on that and select your second form->press F5

Your second form will run..

Hope it will help..
 
Share this answer
 
Comments
Raghavendra M 20-Sep-13 9:14am    
no i m not getting My Project folder in solution explorer
Raghavendra M 20-Sep-13 9:16am    
Actualy i design 2nd form first , after i came to knw this shd not be first . .
Please check below article,

Main Method in C#[^]

Check question

"What if we have more classes with Main() method?"
 
Share this answer
 
Comments
Raghavendra M 20-Sep-13 9:25am    
thts fine i have problem with running my forms. Actualy i design 2nd form first , after i came to knw this shd not be first . . So i design another form , wen i try to run d 2nd form which i created first is running. .
Open your "Program.cs" file and change the "Application.Run(new Form1());" to your "other form".
 
Share this answer
 
In WinForms you run a single Application instance at a time, but that Application can show any number of Forms.

When you create a WinForms project in Visual Studio: it creates a default application structure that starts the Application by invoking a static method 'Main in the static Class 'Program.cs that shows one Form, the Main Form.

Take a look inside your Program.cs file, in the Main method: you'll see something like this:

Application.Run(new Form1());

That starts the Application, and tells it to create a new instance of Form1, and make it visible.

Then, within the Main Form you can create instances of, and show, other Forms. Those other Forms created are not (technically) "child Forms" of your Main Form, but, the default behavior is: when the Main Form is closed, all other created Form instances are closed along with it.

That is the standard WinForms model, and it's a good idea for you to get familiar with it.

In fact, any Class in WinForms can create a new instance of any Form you created the template for at design-time, or create a new Form "from nothing," which you did not create at design-time.

But, as long as you are using the "standard model" WinForms structure, all Forms created anywhere in your code will be closed when the Main Form is closed.

So, you've defined two Forms, the first, Form1, is your main Form, and the second, Form2, is your other Form. In your code for Form1.cs:
C#
// create a variable that holds an instance of Form2
private Form2 theSecondForm = new Form2();

// now create a "raw" Form you didn't create at design-time
private Form theRawForm =new Form();

// assuming you've "wired-up" an EventHandler for Form1's
// Load Event at design-time
private void Form1_Load(object sender, EventArgs e)
{
    // show the instance of Form2
    theSecondForm.Show();
    
    // set the Location of the instance of Form2
    // so that it's aligned with Form1's right, top
    theSecondForm.Location = new Point(Right, Top);

    // show the instance of the "raw" Form
    theRawForm.Show();
    
    // the poor orphan needs a Name !
    theRawForm.Name = "aRawForm";
    
    // and a Title
    theRawForm.Text = "I came from nothing";

    // set the Location of the instance of rawForm
    // so that it's aligned at Form1's right, and
    // underneath the second Form
    theRawForm.Location = new Point(Right, Top + theSecondForm.Height);

}
There are ways you can alter the structure of WinForms applications so they do not conform to the "standard model," but before you explore those, it's good, imho, to have a sound understanding, and competency with the standard model.
 
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