Events

Anjumnagpal

Member
Joined
Feb 4, 2009
Messages
15
Programming Experience
Beginner
I have a windows applications.

Form 2 is child form of Form1 . Basically when button on form2 (child form) is clicked, it raise an events, which writes something in testbox on form1 (parent form). This is my code

parent form form1 code:

Public Class Form1
Public mycount As Integer

Private WithEvents obj As New Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim newdoc As Form2
mycount = mycount + 1

newdoc = New Form2()
newdoc.Text = "Doc" + Str(mycount)

newdoc.MdiParent = Me.Parent
newdoc.Show()

End Sub

Private Sub obj_textfill(ByVal txt As String) Handles obj.textfill
TextBox1.Text = txt

End Sub
End Class

form2 child form code:

Public Class Form2
Public Event textfill(ByVal txt As String)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
RaiseEvent textfill("hi")

End Sub
End Class



This is the first time I am tring to use events.. I am not sure what's going wrong.. the textfill event is never called from form1. Please help
 
In the form1_Load Event:
VB.NET:
Addhandler form2.FillText, AddressOf FillText
Then Make a Sub named FillText
VB.NET:
Private Sub FillText(ByVal txt As String)
  If Not txt Is Nothing Then
     Textbox1.Text = txt
  End If
End Sub
So when you raise the event and pass the string thru it - will be handled in form1 with this sub. So delete 'WithEvent obj As Form2 ' and your obj.filltext event.
 
Tried the above code, still doesnt work:

Here's the code again after the changes:

form2 (child form):

Public Class Form2
Public Event textfill(ByVal txt As String)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

RaiseEvent textfill("hi")
End Sub
End Class

and form1 (parent form):

Public Class Form1
Public mycount As Integer

'Private WithEvents obj As New Form2
Public Sub form_load()
AddHandler Form2.textfill, AddressOf Textfill
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim newdoc As Form2
mycount = mycount + 1

newdoc = New Form2()
newdoc.Text = "Doc" + Str(mycount)

newdoc.MdiParent = Me.Parent
newdoc.Show()

End Sub

Private Sub Textfill(ByVal txt As String)
If Not txt Is Nothing Then
TextBox1.Text = txt
End If
End Sub

End Class

please look at the code and tell me where is it doing wrong??
 
Your load sub should look like :
VB.NET:
Private Sub Form1_Load(byval sender as object, byval e as system.eventargs) Handles Mybase.Load
 
Thanks.. Still the sub textfill is not being called..

Here's the corrected code:

Form 2 (child form)

Public Class Form2
Public Event textfill(ByVal txt As String)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
RaiseEvent textfill("hi")
End Sub
End Class


Form 1 (Parent Form)

Public Class Form1
Public mycount As Integer

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler Form2.textfill, AddressOf Textfill
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim newdoc As Form2
mycount = mycount + 1

newdoc = New Form2()
newdoc.Text = "Doc" + Str(mycount)

newdoc.MdiParent = Me.Parent
newdoc.Show()

End Sub

Private Sub Textfill(ByVal txt As String)
If Not txt Is Nothing Then
TextBox1.Text = txt
End If
End Sub

End Class
 
Sorry since you form instance is newform it will have to add the handler...
VB.NET:
WithEvents newForm as New Form2
'load event
Addhandler newForm.filltext, AddressOf filltext
 
not working again :(.. here's the code again
form 2 child code:

Public Class Form2
Public Event textfill(ByVal txt As String)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
RaiseEvent textfill("hi")
End Sub

End Class

form 1 parent class:

Public Class Form1
Public mycount As Integer

WithEvents newForm As New Form2
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler newForm.textfill, AddressOf Textfill
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim newdoc As Form2
mycount = mycount + 1

newdoc = New Form2()
newdoc.Text = "Doc" + Str(mycount)

newdoc.MdiParent = Me.Parent
newdoc.Show()

End Sub

Private Sub Textfill(ByVal txt As String)

If Not txt Is Nothing Then
Me.TextBox1.Text = txt
End If
End Sub

End Class
 
You have two objects as New form2, make them the same! newForm or newDoc which one you want, then you should be good...
 
No problem, when in doubt, check all lines, and 1 line of code out of place will screw up the works. Enjoy...
 
Why dont you just do this

VB.NET:
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Form1.TextBox1.Text = "Hello"
End Sub

Just a much simpler way of getting the same answer...

I think thats what you ment anyway
 
Last edited:
Back
Top