开发者

How can I create a new C# Window based on my existing Window?

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 kn开发者_高级运维ow 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:

  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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜