CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Hybrid View

  1. #1
    Join Date
    Dec 2007
    Posts
    40

    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!

  2. #2
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Opening a new window in a Windows Application

    Quote Originally Posted by EVS View Post
    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
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  3. #3
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    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.

  4. #4
    Join Date
    Dec 2007
    Posts
    40

    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.

  5. #5
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Opening a new window in a Windows Application

    it's not easy to help you without seeing your masterpiece (you should already know that). so, what about posting some code?
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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.

  7. #7
    Join Date
    Dec 2007
    Posts
    40

    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.

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Opening a new window in a Windows Application

    Quote Originally Posted by EVS View Post
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured