Joshua
Joshua

Reputation: 371

How do you open a new window in C#

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();
}

Upvotes: 5

Views: 67468

Answers (3)

Drew Noakes
Drew Noakes

Reputation: 311305

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();
}

Upvotes: 11

Tigran
Tigran

Reputation: 62265

If you need your own, just add to the project a new form, or create your own from the stratch and call

myForm.Show()

Upvotes: 0

brunnerh
brunnerh

Reputation: 185300

I don't understand why you run the application there but usually you open a window by creating an instance and showing it.

var window = new Help(); // Help being the help window class
window.Show();

Also as this on the background thread it may cause trouble in terms of inter-control communication. Usually you will want to create and access UI-elements on the UI thread only. To move any operation to the UI-thread you can use the Dispatcher of the UI-thread. See also: Threading Model

Upvotes: 2

Related Questions