Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake a main form send data to subforms by using events
DescriptionThis example shows how to make a main form send data to subforms by using events in Visual Basic 6. The subforms declare a reference to the main form using the WithEvents keyword so they can watch for messages.
KeywordsForm, subform, secondary form, data
CategoriesControls
 
Thanks to James Hansen.

The main form declares the ParentEvent event. When it needs to send information to all of the child forms, it raises this event.

The main form also has a DataFromChild method that the child forms can call to send data to the main form.

 
Public Event ParentEvent(ByVal NewData As String)

Private Sub Timer1_Timer()
Dim iStr$

   iStr = CStr(Rnd * 1000)
   RaiseEvent ParentEvent(iStr)
End Sub

Public Function DataFromChild(ByVal hwnd As Long, ByVal _
    Data As String)
   Debug.Print "hWnd " & hwnd & " : " & Data
End Function
 
The child form declares a reference to the main form with the WithEvents keyword so it can catch the ParentEvent event. It initializes this reference in its Form_Load event handler.
 
Private WithEvents MDIFormEvents As MDIForm1

Private Sub Form_Load()
   Set MDIFormEvents = MDIForm1
End Sub

Private Sub MDIFormEvents_ParentEvent(ByVal NewData As _
    String)
   Label1.Caption = NewData
End Sub

Private Sub Timer1_Timer()
   Call MDIFormEvents.DataFromChild(Me.hwnd, CStr(Time))
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated