Show form in C++?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Show form in C++?

  1. #1
    Join Date
    Apr 2011
    Posts
    1

    Question Show form in C++?

    Hey, I'm Tyler. I'm new to CodeGuru. I've been programming in Visual Basic .NET for about a year and have decided to start exploring C++. I download VC++ and am trying to use it, but one thing I can't seem to do is show a form. I know this will do it:

    Form2 myForm = gcnew Form2;
    myForm->Show();

    but it makes multiple instances of the same form. In VB.NET, Form2.Show() makes a single instance of the form and only one Form2 is allowed to be open. If you want to make multiple forms you'd do:

    Dim myForm as New Form2
    myForm.Show()

    How do I make a single instance of Form2? I used a converter and it told me

    Form2::Show();

    I tried that and I got an error saying something about an illegal call of a non-static member function. How do I do it??

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Show form in C++?

    First, this is not C++, but C++/CLI.

    Second, I believe it should be
    Code:
    Form2^ myForm = gcnew Form2();
    myForm->Show();
    Third, I'm not sure I understand the instances problem. You have a single instance there (you called gcnew once), but each time you call Show/ShowDialog a new window is created. How exactly would you describe the behavior of this "single instance"?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Show form in C++?

    [ redirected ]
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,594

    Re: Show form in C++?

    To clarify, this creates the form instance:

    Quote Originally Posted by RadXPictures View Post
    Code:
    Form2 ^myForm = gcnew Form2;
    (The symbol marked red was missing in your original snippet, as already pointed out by cilu.)

    And this shows the form:

    Code:
    myForm->Show();
    Yes, it's that simple, but the implications this has aren't necessarily.

    When you execute the gcnew line a second time without closing (or explicitly destroying - IOW disposing - using delete) the first form instance, a new form instance is is created and it's tracking handle is assigned to myForm, thereby overwriting the tracking handle of the first form instance. Once you've lost that handle, there's no way to close the form programmatically anymore. At best you can close it by user interaction or wait for the garbage collector to reclaim the fom's resources, but you have no way to tell when it'll do that.

    Best practice is to simply treat these code lines as what they actually are. There's no necessity to immideately show the form after it has been created. If you want a single instance of Form2 that is to be reused, make the Form2 tracking handle a class member of Form1, instantiate the form in advance (the Form1 constructor would be a natural place to do that, e.g.) or on demand, and show and hide it as needed.

    Please use code tags when posting code. It didn't make that much a difference with your two-liners above, but it will do with larger code snippets.

    Ah, and... Welcome to CodeGuru!

    Quote Originally Posted by cilu View Post
    Third, I'm not sure I understand the instances problem. You have a single instance there (you called gcnew once), but each time you call Show/ShowDialog a new window is created. How exactly would you describe the behavior of this "single instance"?
    Form::Show() and Form::ShowDialog() don't create the form instance, they just display an instance that's already there. (However, this may differ for message boxes or common dialogs but that's another topic.)

    At any rate, thanks for stopping by and giving advice. I appreciate everyone who does that in this section.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #5
    Join Date
    Jul 2002
    Posts
    2,526

    Re: Show form in C++?

    Quote Originally Posted by RadXPictures View Post
    I've been programming in Visual Basic .NET for about a year and have decided to start exploring C++.
    If you want to start working with C++, learn native C++. You are wasting your time learning C++/CLI - it is much better to use VB .NET of C# for writing user interface.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center