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.
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.
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.
I will assume you are using winforms and will walk you through a simple example:
- In Solution Explorer, Right Click your project and select Add | New Item...
- Select the type About Box and you will see a new AboutBox1.cs get generated.
- Select View | Toolbox to get the Toolbox to appear.
- On your main form, drag a Button from the ToolBox | Common Controls onto the form.
- Double click the newly created button to create the Clicked Event.
- 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.