tags:

views:

64

answers:

2

I am trying to hide the window MainWindow from another window. I thought that the following code would work until I realised that it would just create another instance of MainWindow, and then hide that, and not the original window!

MainWindow MainWindow = new MainWindow();
MainWindow.Hide();

Then I tried to use just MainWindow.Hide() but that doesn't work. How can I do it?

+1  A: 

You need to get the actual instance of the Window you are trying to hide. If (guessing by the name) this is your main window, you could try:

Application.Current.MainWindow.Hide();

Otherwise, you need some means of getting the specific instance of the Window you wish to hide.

Reed Copsey
I just tried `CBR.MainWindow.Hide()` (CBR being the application name) and there is lots of properties for `CBR.MainWindow`, but there are no functions such as `Hide()` or `Close()`. Is it even possible to find the instance of the window when the window was created at startup? As it is the first window opened when you start the application.
Joel Kennedy
@Joel: You can't use the type (MainWindow) - that will only show you static method. My suggestion was to use the Application.MainWindow property. This will work if the window you're trying to hide is the main app window at startup. See: http://msdn.microsoft.com/en-us/library/system.windows.application.mainwindow.aspx
Reed Copsey
@Reed: Ahh I see, sorry I misunderstood. I am still having problems with that though. I followed your link and used the code `Window mainWindow = this.MainWindow`, and then on the next line used `mainWindow.Hide()` and it seemed to recognise that 'mainWindow' was a window, and gave me the option to hide it. When I ran the program though, I got the error "CBR.ErrorReportingForm does not contain a definition for 'MainWindow' and no extension method 'MainWindow' accepting a first argument of type 'CBR.ErrorReportingForm' could be found (are you missing a using directive or an assembly reference?"
Joel Kennedy
@Reed: Thanks so much for your help so far anyway, and if this baffles you as much as it does me, then I will work my way around Dave's method and use that. Thanks anyway!
Joel Kennedy
@Reed: I tried using your line, but I got an error: "An object reference is required for the non-static field, method, or property 'System.Windows.Application.MainWindow.get'".
Joel Kennedy
@Joel: Oops - It should have been "Application.Current.MainWindow.Hide();" - I edited my answer to adjust as well. (Sorry about that - just typing off the top of my head here...) Try that - it'll probably work for you :)
Reed Copsey
Thanks very much! The new code worked perfectly. Cheers!
Joel Kennedy
+1  A: 

If the window you want to do the hiding is opened from the MainWindow:

//Main Window
private void OpenChildWindow()
{
     ChildWindow child = new ChildWindow();
     child.Show();
     child.Owner = this;

}

//Child Window
private void CloseMainWindow()
{
     ((Window)Parent).Hide();
}
Dave Swersky