Opening a new window in a Windows Application
Hello!
I am using Visual Studio 2008 Pro. I have a WPF form and a button. When the user clicks the button, I want to open another window, which is more complex than a simple messagebox - it would query a database to get data and such.
So, what do I write in the
Code:
private void OpenTheOtherWindow_Click(object sender, RoutedEventArgs e)
{
//open window, please
}
procedure?
Surprisingly, I was not able to find anything on this topic anywhere on the net. All reference webish apps, but this is a Windows application.
Thanks!
Re: Opening a new window in a Windows Application
Quote:
Originally Posted by
EVS
All reference webish apps, but this is a Windows application.
this is not true.
http://www.google.com/search?hl=en&n...&aq=f&oq=&aqi=
http://social.msdn.microsoft.com/For...4-967a361cedd2
Re: Opening a new window in a Windows Application
You need to create a new Window in your application. Then, just .Show() it when you're ready to create an instance of it.
Re: Opening a new window in a Windows Application
That's all good, but the problem is that when I close the window and try to show it again, an exception is thrown saying that I cannot open a closing window. If I override the onclosing function and hide the window instead of closing it, the program wouldn't shut down, when I close the main window, because the other window has never been closed.... creating a new instance of the window object every time when I want to show it, wouldn't cut it as well, because then I cannot reference the window object from the main window as it is local to that button_click function.
I am using WPF.
Re: Opening a new window in a Windows Application
it's not easy to help you without seeing your masterpiece :D (you should already know that). so, what about posting some code?
Re: Opening a new window in a Windows Application
Assuming you want a modal dialog type of window, it's pretty simple if you use ShowDialog.
To make this even simpler when invoking the dialog, I typically create a static factory method. The code below shows an About dialog (which is a WPF window).
Code:
publicpartialclassAboutDlg : Window
{
publicstaticbool? ShowDialog( Window owner )
{
AboutDlg dlg = newAboutDlg( owner );
return dlg.ShowDialog( );
}
private AboutDlg( Window owner )
{
InitializeComponent( );
Owner = owner;
}
// other code here
}
The from another window, I call it like this:
Code:
privatevoid OnHelpAbout( object sender, RoutedEventArgs e )
{
AboutDlg.ShowDialog( this );
}
If you need to do something differently, then post your code so we can help you with it.
Re: Opening a new window in a Windows Application
I'd like to have a window that would have a matrix which would be updated with data from an underlying database.
Re: Opening a new window in a Windows Application
Quote:
Originally Posted by
EVS
I'd like to have a window that would have a matrix which would be updated with data from an underlying database.
We got that part. The question remains if whether you are looking for a modal dialog approach (as I've given you) or something else. What the window (or form) contains is irrelevent at this point if you are having trouble getting the window to display.