RaiseEvent from a class

ChrisG

New member
Joined
Mar 11, 2009
Messages
2
Programming Experience
10+
Hi, I'm trying to raise an event from a class. It looks ok but it did not raise the event as expected...? Anyone as a solution ?

Steps
1 - Load the form2
2 - Boutton1 call the form1 and load the class animation
3 - Class animation raise the event for the form1


Here is the code !

Public Class Form1
Private WithEvents AnimationTextChange As Animation

Public Sub mdiMainEvent_StatusTextChange(ByVal _text As String) Handles AnimationTextChange.StatusTextChange
MessageBox.Show(_text)
End Sub
End Class

Public Class Form2

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Show the form
Dim _Form As New Form1
_Form.Show()
'Call the class
Dim x As New Animation
x.ChangeAnimationText(TextBox1.Text)

End Sub
End Class

Public Class Animation
Public Event StatusTextChange(ByVal text As String)

Sub ChangeAnimationText(ByVal text As String)
RaiseEvent StatusTextChange(text)
End Sub
End Class
 
The RaiseEvent call is located where the 'text' variable is made then pass it to the RaiseEvent, your 'text' variable contains no value.
VB.NET:
'Animation Class
'Class Declaration
Public Event <yourEventName>(Byval text As String)
'...Sub routine
Dim txt As String = <something>
RaiseEvent <yourEventName>(txt)
You then need to access it by Addhandler method
VB.NET:
'In Form1_Load Event
AddHandler Animation.<yourEventName>, AddressOf <yourRoutedSub>
Private Sub <yourRoutedSubName>(Byval text As String)
If Not text Is Nothing Then
   'do something
End If
End Sub
In your Routed Sub your signature need to be the same-(Byval text As String) The Name of the Sub is not important. The text value is being passed here.

Hope this helps.
 
Last edited:
After rereading your post it looks like you might are asking the Sub ChangeAnimationText to do something with the text from textbox1, in this case you aren't changing the text so it looks the same. To check if a sub routine is firing - use a messageBox.Show(text).

Also you don't need an Event to use a Sub/Function(diff.form) - your probably needing the later to return a value.

Call the function:
VB.NET:
Dim x As new Amination
Dim str As String
str = x.ChangeAminationText(textbox1.text)
If it is a function it will return the value you tell it to.
 
Last edited:
Back
Top