Multi Forms

bsktcase

Newcomer
Joined
Apr 29, 2005
Messages
1
Excuse me for being a bit of a newbie but I am having trouble with multiple forms. I use a code to open my form to be sure that only one instance of the form is open at a time but it is not working in the sitiuation I need it now. Basically I open a main menu form which can open a search form making a selection on the search form then opens the final form. My issue is I don't want 2 final forms open at the same time and I also want the search form closed when the final form is open and all this needs to be done while leaving the main menu form open. I understand the issue I am using a variable for the form and checking if that variable is nothing to see if the form is open or closed but when I close the form that contains the variable it becomes nothing and when it reopens it thinks the form is closed. Hope that wasn't too confusing. Any ideas are appreciated and thanks for the patience.

Basically when you make a selection on the search form (2nd form) I run the following which will open the final form (3rd Form)and close the search form leaving the original Main Menu open. I don't want the user clicking on the Main Menu opening the search form again and selecting a new search to open a second instance of the Final Form:

Dim FinalForm as frmFinalForm


me.close

If Not IsNothing(FinalForm) Then
If Not FinalForm.IsDisposed Then
FinalForm.WindowState = FormWindowState.Normal
FinalForm.BringToFront()
Else
FinalForm = New frmFinalForm
FinalForm.Show()
Else
FinalForm = New frmFinalForm
FinalForm.Show()
End If
 
Last edited:
If your app is in MDI format then try this:

*note that frmContainer is whatever your actual form is

Visual Basic:
[size=2][color=#0000ff]Dim[/color][/size][size=2] frmContainer [/size][size=2][color=#0000ff]As[/color][/size][size=2] frmContainer = [/size][size=2][color=#0000ff]Nothing
 
[/color][/size][size=2][color=#0000ff]For[/color][/size][size=2][color=#0000ff]Each[/color][/size][size=2] f [/size][size=2][color=#0000ff]As[/color][/size][size=2] Form [/size][size=2][color=#0000ff]In [/color][/size][size=2][color=#0000ff]Me[/color][/size][size=2].MdiChildren
[/size]
[size=2][color=#0000ff]If[/color][/size][size=2][color=#0000ff]TypeOf[/color][/size][size=2] f [/size][size=2][color=#0000ff]Is[/color][/size][size=2] frmContainer [/size][size=2][color=#0000ff]Then[/color][/size]
[size=2]frmContainer = [/size][size=2][color=#0000ff]CType[/color][/size][size=2](f, frmContainer)
[/size]
[size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
[/color][/size]
[size=2][color=#0000ff]Next
[/color][/size]
[size=2][color=#0000ff]If[/color][/size][size=2][color=#0000ff]Not[/color][/size][size=2] (frmContainer [/size][size=2][color=#0000ff]Is[/color][/size][size=2][color=#0000ff]Nothing[/color][/size][size=2]) [/size][size=2][color=#0000ff]Then[/color][/size]
 
[size=2]frmContainer.Show()[/size]
[size=2]frmContainer.WindowState = FormWindowState.Maximized[/size]
[size=2]frmContainer.Focus()
[/size]
[size=2][color=#0000ff]Else[/color][/size]
[size=2]frmContainer = [/size][size=2][color=#0000ff]New[/color][/size][size=2] frmContainer[/size]
[size=2]frmContainer.MdiParent = [/size][size=2][color=#0000ff]Me[/color][/size]
[size=2]frmContainer.Show()[/size]
[size=2]frmContainer.Focus()[/size]
[size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If[/color][/size]
[size=2][color=#0000ff]
[/color][/size]
 
Another approach not involving MDI's.

Form A is main form; Form B is search form. Button on Form A opens Form B. Selection on Form B needs to be passed back to Form A. Form B must be closed after selection is made; only one instance of Form B can be opened at any time.

In FormB:
public passingvalue as string (or whatever type you are passing back from B to A)
event Selected

when selection is made in Form B:
passvalue = selected value
raiseevent Selected

In Form A:
Dim withevents frmB as new formB

Define ButtonA1 Click event as follows
frmB = new FormB
frmB.showdialog
frmB = Nothing

Define an event handler in Form A, e.g. ValueSelected Handles frmB.Selected
dim passedvalue as string = frmB.PassingValue

In Form B, you can call Me.close immediately after the selection is made and the event has been raised, or you can let the user choose when to close. Either way, the passingvalue will be sent back to FormA ("handled by the event handler") after the selection is made. Since you used .ShowDialog rather than just .Show when you called FormB, no action can be taken on any other form while Form B remains open.

The key concept is Events; I wish I had learned about them earlier n my VB.Net career.

Don't forget Dim WithEvents.....
 
Back
Top