Results 1 to 18 of 18

Thread: Open WinForm

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Open WinForm

    How can i open a WinForm Class from a button in my WPF project?

    I've added my WinForm to my solutions explorer via add existing class.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Open WinForm

    First, why would you? Why not recreate the same functionality in a WPF Window?

    As for the question, presumably you would show a Windows Form exactly the same way as you always do. There's no reason it should be any different.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Open WinForm

    Tested and confirmed. The only difference is that there is no default instance. Default instances are a function of the application framework, which is set up to support Windows in a WPF app, not Forms.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: Open WinForm

    So how do I do it?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Open WinForm

    So, you're saying that you've never opened a form in a WinForms app before?

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: Open WinForm

    I do it this way

    MyFormName.Show

    When I type my form name in my WPF project I don't get the Show options showing

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Open WinForm

    As I said, there's no default instance. You need to create a new object of that type first, just as you do for any other type. Once you have an object, then you can access its members, e.g. call its Show method.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: Open WinForm

    So how do I do that?

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Open WinForm

    This is the part where you get to think. You've created objects before. How did you do it then? Maybe you should take a look at some of your own code.

    http://www.vbforums.com/showthread.php?t=616468

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: Open WinForm

    I've been trying that

    Code:
            Dim winform As New userwindow
            DirectCast(winfor, MyFormName)
            winform.ShowDialog()
    This is not working, am i close?

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Open WinForm

    What's the second line for?

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: Open WinForm

    I'm converting MyFormName into winfor ??

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Open WinForm

    What EXACTLY is the type of the form you want to display? Create that type of form and display it. That's all. There's no converting to be done because it already is exactly what it's supposed to be.

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: Open WinForm

    It's a Windows Form. Created in VB 2008


    Create that type? okay how?

    Dim winform as new Window.Form

    Thanks not an option

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Open WinForm

    You're missing some of the basics here. When you add a form to your project you are creating a new class. That new class inherits System.Windows.Forms.Form, which is what makes it a Windows Form. It inherits all the form functionality from its base class but it is a new class and you add new functionality to it. The new class might be MainForm or OptionsDialog or AboutBox or whatever you choose to name it. What is yours?

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: Open WinForm

    I'm about to give up, I can't simply import an existing item and view it. It's got all sorts of errors and when i continue it's just a blank form. i take it it's not possible to import a form from another project

  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Open WinForm

    Quote Originally Posted by AirlineSim View Post
    I'm about to give up, I can't simply import an existing item and view it. It's got all sorts of errors and when i continue it's just a blank form. i take it it's not possible to import a form from another project
    When I tested I added a new item in the Solution Explorer, which probably added all the necessary references and imports automatically. In your case, because you're importing an existing item, you may well need to add those automatically.

    Open or create a WinForms project and go to the References page of the project properties. Whatever references and imports you see there, make sure they are also present in your WPF project containing the WinForm. Add any references that are missing and then tick any imports that are unticked. I'm guessing that you'll need at least System.Windows.Forms and System.Drawing in each case.

  18. #18
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Re: Open WinForm

    Am I still missing something here, why are you using the same winform window?

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