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??
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"?
(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!
Originally Posted by cilu
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.
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.
Bookmarks