Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you open a new window in C#

Tags:

c#

.net

wpf

I am creating a Kinect application and want to open a new window called 'Help' from the 'MainWindow.xaml.cs' file.

I tried using the following code:

// The commented code is what I have tried.
public static void ThreadProc()
{
     // Window Help = new Window();
     //Application.Run(new Window(Help);    

     Application.Run(new Form()); 
}

private void button1_Click(object sender, EventArgs e)
{

    System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProc));

    t.Start();
}
like image 697
Joshua Avatar asked Dec 12 '22 05:12

Joshua


1 Answers

Showing a window just requires a call to its Show method.

However, keeping an application running requires a call to Application.Run. If you pass this method a form, it'll call Show for you.

However, if you already have a running application, you can just do something like new MyForm().Show().

I strongly suspect you don't need to create a new thread and Application for your new window. Can't you just use:

private void button1_Click(object sender, EventArgs e)
{
    new Form().Show();
}
like image 144
Drew Noakes Avatar answered Jan 01 '23 04:01

Drew Noakes