Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Raise Event in Form A, respond in Form B 2

Status
Not open for further replies.

Elysium

Programmer
Aug 7, 2002
212
US
I have two forms that I need to communicate between. The first form (Form A) will take several entries into a listbox. After each entry into the listbox, I need to raise an event that will tell Form B how many entries have been added currently. Here's what I have so far (it doesn't work):

Form A:
Code:
Public Event addedNumber(CurrentCount As Integer)

Private Sub txtTrackingNumber_LostFocus()
               
    With Me.txtTrackingNumber
        If .Text <> "" Then
            frmMain.lbxTrackingNumbers.AddItem Me.txtTrackingNumber
            RaiseEvent addedTrackingNumber(TrackingNumberCounter)
            .Text = ""
            .SetFocus
            TrackingNumberCounter = TrackingNumberCounter + 1
        End If
    End With

End Sub

Form B:
Code:
Dim WithEvents ftn As frmTrackingNumber

Private Sub Form_Activate()

    Set ftn = frmTrackingNumber
    
End Sub

Private Sub Form_Deactivate()

    Set ftn = Nothing
    
End Sub

Private Sub ftn_addedTrackingNumber(CurrentCount As Integer)

    MsgBox CurrentCount

End Sub


Thanks,

Randy
[afro]
 
The implication of that is that a form (FormB in your case) can exist as an object in another form. I doubt that such a thing is possible. You can try this though

Form A
Code:
Private Sub txtTrackingNumber_LostFocus()
               
    With Me.txtTrackingNumber
        If .Text <> "" Then
            frmMain.lbxTrackingNumbers.AddItem Me.txtTrackingNumber
            [COLOR=blue]FormB.addedTrackingNumber(TrackingNumberCounter)[/color]
            .Text = ""
            .SetFocus
            TrackingNumberCounter = TrackingNumberCounter + 1
        End If
    End With

End Sub

Form B
Code:
Public Property Let addedTrackingNumber(CurrentCount As Integer)
    MsgBox CurrentCount
End Property


[small]No! No! You're not thinking ... you're only being logical.
- Neils Bohr[/small]
 
Thanks for the insight. I have it working as it should now.

Randy
[afro]
 
Well, I think I disagree with Golom regarding one form setting a reference to another, I think you could do it this way. I think your problem is that you declared your event with a different name than how you are calling it:
dim addedNumber
vs.
RaiseEvent addedTrackingNumber(TrackingNumberCounter)

Golom's method might be a good alternative, although instead of using a property I think I would declare it as a public sub (in effect making it a custom method of FormB). I choose a sub instead of a property because you are getting FormB to do something rather than setting one of its attributes.

Whether you use events or a public sub depends mostly on how many other forms need to be informed of the tracking number increase, and how often. If it's lots of forms, and they need to know about it every time an event is probably best. If it's only one form, and it really only needs to know about the tracking number count occasionally, a public sub (aka "method") would probably be better.
 
You're not adding your "Handles" statement to the end of your event procedure

Add "Handles ftn.addedTrackingNumber" to the end of your procedure in Form B.
 
No, "Handles" is for VB.NET, not VB6.

This is why I chose C# as my .NET language, so I wouldn't get confused going back and forth between them VB6 and VB.NET. :)
 
Hi,
you should keep form instance in ftn variable (and match event name with event handler procedure name). So, in formB code, try

Private Sub Form_Activate()
Set ftn = New frmTrackingNumber
ftn.Show
End Sub

combo
 
I think the OP has it working, now. The reason that it wasn't working is because he didn't declare the same event as he raised. Also, probably because he destroyed the object reference every time he deactivated the form.

This works:
Code:
'2 forms, form1 and form2, command button on form2
'form1's code:
Option Explicit
Dim WithEvents f2 As Form2

Private Sub f2_TestEvent()
MsgBox "Event Works"
End Sub

Private Sub Form_Load()
Set f2 = New Form2
f2.Show
End Sub

'form2's code
Public Event TestEvent()

Private Sub Command1_Click()
RaiseEvent TestEvent
End Sub

There are situations where you can't recognize events directly, though. In those cases, you may be able to write a brokering class. To see how to do this, check faq222-3666, although if you do, I believe you will be the first to have read it. It talks about exposing events raised by constituent controls of an activex control through a container of that control (DataRepeater in this case), to the container of the container (the form). That's the sort of situation where you might not be able to directly handle a raised event.

HTH

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top