How To Open An Existing Form From Another Form...

UCM

Centurion
Joined
Jan 1, 2003
Messages
135
Location
Colorado, usa
I have 2 forms, form1 and form2...Not like above, these have textboxes on them, both have a textbox1 and I manually created each form in vs...

I see where using the code:
Visual Basic:
Dim Boo AS New form2
can create a new instance of the form2 object...

However, what I would like to acheive is form1 and form2 both existing in memory and when you click on a button in either one, then it will hide and the other will show...

The trick is that if I do not have to create a 'New' instance of it then the data in both text boxes would be preserved as you switch back and forth between the two forms...

Any ideas on how to acomplish this?
 
This seems like a very odd way of doing things...

You could only keep two instances around by placing a reference to them in shared fields of the two forms I suppose. Your message loop will have to be such that it doesn't rely on one form, i.e. use Application.Run() from your startup method.

If you get really stuck, I'll knock up an example.
 
It sounds like you've got one of two different forms that needs to be shown based on some toggle (like a button, checkbox, etc.)? Swapping out two different forms isn't that standard - could you use a Tabbed form instead? Or possibly use two Panel controls that become Visible/Invisible based on your button/checkbox/etc.?

Swapping out two forms is going to have lots of issues such as "flashing" as the forms swap, getting the positions right, keeping the forms the exact same size so as to not alert the user. It really sounds like you'd be better off with using other controls...

-Nerseus
 
Not quite what I had in mind...

Normally, I would do just that, Nerseus, with a tab control...

However, the form example I gave in this thread is a basic example of what I want to do so that hopefully someone would figure out how to do it...

Basically, I want to use 1 form to control the other one...Kind of like a control panel that would disappear when you select what you want to do and then bring up the form that would be used for what you chose to do...

It would sound much more complex than the basic example I gave...

Come to think of it, is there a way to access property values and subs on 1 form from the other? ( I forgot all about that )
 
Basically, I want to access existing instances of a form ( form1 ) from another form ( form2 ) or sub ( Sub Main )...

I would like to be able to hide/show them and if possible access the entire form and it's contents just like if I were to use the
Visual Basic:
ME.
keyword...
 
i think this is probably the same way you are doing it
Dim x As New frmTrace()
x = New frmTrace()
x.Show()
thats how i have been using it, but like you say it probably wont retain the text in the memory, in vb6 using Me.Hide and Me.Show seemed so straight forwards.
 
There's no longer a global variable for each form like there was in VB6 (and earlier). You can do this yourself in a couple of ways. In your Main class you can declare your two form variables and create them both, but show just one. From each form, you'd have a global reference to the other form. I wouldn't go this route, but then again, I probably wouldn't swap out forms either :)

You can also accomplish the same thing by using Static methods on each form that would control creating an instance of the form if it didn't exist or returning the existing reference if it did.

As for referencing controls on Form1 from Form2, simply declare the controls or variables as Public. You can do it through the designer by changing the Modifiers property or manually type it in. You could also get fancy and expose them as custom properties, to make them ReadOnly. It depends on what you're doing with them and how robust you want to be.

-Nerseus
 
Heres what I have so far...

Here's what I have so far on the prog...


Sorry I didn't upload it before, I was at work and didn't get a lot of sleep last night....

[edit]Removed binaries from attachment[/edit]
 

Attachments

Last edited by a moderator:
You're close, but you'll need to create an "infinite" loop to keep at least one form open at all times. Like I said, this is totally non-standard :)

Here's some code to replace in your Module.
NOTE: You'll have to change the project properties to use "Sub Main" as the startup (instead of a form)

Changes include:
1. Create an instance of each form in Sub Main
2. Create an "infinite" loop in Sub Main. It actually runs until both forms are invisible (which occurs if you ever close one form)
3. In each of your "SwitchTo..." methods, I change .Visible=True to .Show since you may never have shown a form before.

Visual Basic:
Module Module1

    Dim MyF1 As ControlingASingleInstanceOfMultipleForms.Form1
    Dim MyF2 As ControlingASingleInstanceOfMultipleForms.Form2

    Public Sub SwitchToForm1()
        'NOTE: If This Sub is decared 'Public' then it can be called from other forms and modules
        'NOTE: If This Sub is decared 'Private' then it can be called ONLY from THIS form or module
        MyF2.Visible = False
        MyF1.Show()
    End Sub
    Public Sub SwitchToForm2()
        'NOTE: If This Sub is decared 'Public' then it can be called from other forms and modules
        'NOTE: If This Sub is decared 'Private' then it can be called ONLY from THIS form or module
        MyF1.Visible = False
        MyF2.Show()
    End Sub

    Public Sub Main()
        MyF1 = New ControlingASingleInstanceOfMultipleForms.Form1()
        MyF2 = New ControlingASingleInstanceOfMultipleForms.Form2()

        ' Default to showing the first form
        MyF1.Show()

        While (MyF1.Visible Or MyF2.Visible)
            Application.DoEvents()
        End While
    End Sub
End Module

-Nerseus
 
Rock On...

Thanx, Nerseus!!

That's exactly what I was going after....

=)



1 ? though...
What are the negitive aspects of using forms like this?
 
The negative effects are (as I said earlier :)):
...lots of issues such as "flashing" as the forms swap, getting the positions right, keeping the forms the exact same size so as to not alert the user. It really sounds like you'd be better off with using other controls...

Maybe you just want to swap out panel controls or use a tabbed dialog?

-Nerseus
 
Back
Top