How to handle events, triggered by contorls in owned form, in owner

J

Jerry Wei

Dear all:
I have a problem about how to handle the events. For example, there
are two forms, Form1 and Form2. Form2 is owned by Form1 and there is a
button,Button1, in Form2. It is no problem for Form2 to handle the
event "Button1.Click" . My problem is how to handle the Button1.click
event in Form1 as Form2's owner. Is there any way to achieve this
demand???
=================================================================
owner and owned form relationship what i mean is Below :

Me.AddOwnedForm(Form2) 'Me is Form1 instance
Form2.Show()


thx for your suggestion and reply
Jerry Wei
 
K

Ken Tucker [MVP]

Hi,

Use addhandler.

Dim frm As New Form2

frm.Owner = Me

frm.Show()

AddHandler frm.Button1.Click, AddressOf buttonclick



Private Sub ButtonClick(ByVal sender As System.Object, ByVal e As
System.EventArgs)

MessageBox.Show("ME")

End Sub



Ken

-------------------------

Dear all:
I have a problem about how to handle the events. For example, there
are two forms, Form1 and Form2. Form2 is owned by Form1 and there is a
button,Button1, in Form2. It is no problem for Form2 to handle the
event "Button1.Click" . My problem is how to handle the Button1.click
event in Form1 as Form2's owner. Is there any way to achieve this
demand???
=================================================================
owner and owned form relationship what i mean is Below :

Me.AddOwnedForm(Form2) 'Me is Form1 instance
Form2.Show()


thx for your suggestion and reply
Jerry Wei
 
M

Mike McIntyre

Jerry,

Here is one approach:

Private Sub OpenForm2Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenForm2Button.Click
Dim newForm2 As New Form2()
newForm2.Show()
' Associate the HandleForm2Button1Click Sub in this form with
' newForm2's Button1 click event.
AddHandler newForm2.Button1.Click, AddressOf HandleForm2Button1Click
End Sub

' This Sub in Form1 will now handle a click on Button1 on Form2.
Private Sub HandleForm2Button1Click(ByVal sender As Object, ByVal e As System.EventArgs)
MessageBox.Show("You clicked Button1 on Form2")
End Sub
This example assumes Form1 has a button named OpenForm2Button. When the button is clicked the OpenForm2Button_Click Sub instantiates and shows Form2. It also associates a Sub in Form1 with Form2's Button1 click event.


--
Mike

Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com
 
C

Cor Ligthert

Jerry,

You should not even have to know the owner, just set a public event in your
form2. Raise it in the buttonclick, and catch it in form1.

Something like this

\\\Form1
Friend WithEvents frm As New Form2
Private Sub Button1_Click(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
frm.Show()
End Sub
Private Sub Buttonform2_Click(ByVal sender _
As System.Object, ByVal e As System.EventArgs) Handles frm.myevent
MessageBox.Show(DirectCast(sender, Button).Name & " I am pushed")
End Sub
///
\\\Form2
Friend Event myevent(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Button1.Name = "buttonform2"
RaiseEvent myevent(sender, e)
End Sub
///

I hope this helps?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top