views:

46

answers:

3

I know this mite be a bit of a silly question but how do i create another window from my main window in c# windows application? I don't know where to look for this type of questions.

+3  A: 

You can use the following to create a new form. Note that I have provided two examples.

// This example creates a new Form control. While this Form is open, 
// you cannot gain focus of the parent form.

Form form = new Form();
form.ShowDialog();

// If you want to be able to use both Forms. Then this is what you want:

Form form = new Form();
form.Show();

Also, MSDN is your bestest friend: MSDN on Windows Forms.

...And Google.

lucifer
Just a note: `new Form()` will create a general form, without anything. You probably want `new WhatEverYourFormClassNameIs()`.
Abel
+1  A: 

What about:

YourForm newForm = new YourForm();
newForm.Show();

You have several methods of showing your form. I use YourForm as a name here, replace that with the classname of your own form.

Note that a form-class is nothing more than a regular class that can be instantiated like any other class using new and inherits all methods from it parent calls (Form in this case), which includes the methods Show and ShowDialog. You can create as many instances of your class (i.e., of your form) as you like.

Abel
You beat me to it! :P
lucifer
@j-t-s: thought *you* were actually 1 sec earlier lol
Abel
lol, well.. when i clicked answer question, yours was already on the page
lucifer
A: 

I will assume you are using winforms and will walk you through a simple example:

  1. In Solution Explorer, Right Click your project and select Add | New Item...
  2. Select the type About Box and you will see a new AboutBox1.cs get generated.
  3. Select View | Toolbox to get the Toolbox to appear.
  4. On your main form, drag a Button from the ToolBox | Common Controls onto the form.
  5. Double click the newly created button to create the Clicked Event.
  6. the clicked event type the following code:

    AboutBox1 aboutBox = new AboutBox1();
    aboutBox.ShowDialog();

This code will declare a variable aboutBox of type AboutBox1 and then instantiate it ( construct it ). Then you call the ShowDialog() method to get it to appear.

Christopher Painter