
Synchronized forms, or Companion Forms
I've solved my own problem. The key was to define a public event in the
main form, then have each form replace themselves as the handler of that
event.
main form: 3 buttions, btnDoit, btnForm1, and btnForm2
btnDoit - when you click this it changes textbox1 in form1, or form2
which ever
was last active
btnform1 - launches form1
btnform2 - launches form2
Public Class frmMain
Inherits System.Windows.Forms.Form
Public Event DoIt()
Private _Doit As DoItEventHandler
Property DoItHanlder() As DoItEventHandler
Get
Return _Doit
End Get
Set(ByVal Value As DoItEventHandler)
If Not (_Doit Is Nothing) Then
RemoveHandler DoIt, _Doit
End If
_Doit = Value
AddHandler DoIt, _Doit
End Set
End Property
Public Sub btnDoIt_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnDoIt.Click
If _Doit Is Nothing Then
MsgBox("Main Menu")
End If
RaiseEvent DoIt()
End Sub
Private Sub btnForm1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnForm1.Click
Dim frmForm1 As New Form1()
frmForm1.Show()
End Sub
Private Sub btnForm2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnForm2.Click
Dim frmForm2 As New Form2()
frmForm2.Show()
End Sub
End Class
Form2
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub frmForm1_doit()
Static i As Integer = 0
i += 1
TextBox1.Text = "Did it " & i.ToString & ", times"
End Sub
Private Sub Form1_Activated(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Activated
InitHandlers()
End Sub
Private Sub Form1_Closed(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Closed
frmMainMenu.DoItHanlder = Nothing
End Sub
Private Sub InitHandlers()
frmMainMenu.DoItHanlder = AddressOf frmForm1_doit
End Sub
End Class
Form2:
Public Class Form2
Inherits System.Windows.Forms.Form
Private Sub frmForm2_doit()
Static i As Integer = Asc("A") - 1
i += 1
If i > Asc("Z") Then
i = Asc("A")
End If
TextBox1.Text = "Form2, Did it " & Chr(i) & ", times"
End Sub
Private Sub Form2_Activated(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Activated
InitHandlers()
End Sub
Private Sub Form2_Closed(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Closed
frmMainMenu.DoItHanlder = Nothing
End Sub
Private Sub InitHandlers()
frmMainMenu.DoItHanlder = AddressOf frmForm2_doit
End Sub
End Class
Quote:
> I'll admit upfront that I'm a newbee to VB.NET. So.
> I want to create a user interface that allows for a "Menu Form" (frmMenu)
to
> always exist. This form launches other forms.
> For example:
> frmMenu has 3 buttons, btnDoit, btnLaunchForm1, and btnLaunchForm2. So
> frmMenu launches frmForm1, then frmForm2. frmForm1, and frmForm2 each
have
> 1 textbox on them.
> I want to be able to click btnDoIt on frmMenu and have that display
letters
> in frmForm2 when it was the last active form and numbers in frmForm1 when
it
> was the last active form.
> I tried creating a public variable for frmMenu so that I could have an
> addhandler in each forms activate events, but I only seem to be able to
> addhandlers to local classes.
> Can I create a collection of disparate forms? It looks like collections
> need to be typed exactly too.
> Any help appreciated, Thanks in advance
> Ben Hunsberger