Click to See Complete Forum and Search --> : Raise an event on a child form from another form


Coolboylp
June 2nd, 2008, 03:09 AM
Hi,

I would like to know how to raise an event or trigger a _Keydown or _click event of a child form from another form.

I have a MDI form on which I open a login form (child form). Then I open another form from this child form which brings up my own numeric pad. I'm able to enter the numbers from this numeric pad and get it to update the password textbox which is in the child form.

My problem is I have an ENTER key in the numeric pad form, so when I press the ENTER button I want to trigger an event like txtpwb_keydown or button1.click event on the child form. How can I do this?

Thanks in advance.

kmoorman
June 2nd, 2008, 06:27 AM
Coolboylp,

Set the form's AcceptButton property to the button that should respond to the enter key.

Kerry Moorman

Coolboylp
June 2nd, 2008, 06:57 AM
Hello kmoorman,

Thanks for the quick response.

The thing is, the password textbox is on one child form (say Form2). My number pad is on another form (say Form3).

Form2 is a child form of Form1 which is a MDI form.

I want to call or raise or trigger the textbox keydown event of Form2 from Form3, upon pressing button1 which is in Form3.

Hope its clear.

Thanks.

sotoasty
June 2nd, 2008, 08:24 AM
Try something like this.

form1 Add 2 buttons and a text box

form2 Add a button

Code for form1


Option Strict On
Option Explicit On
Public Class Form1
Private WithEvents mvarForm2 As Form2
Private mvarForm2a As Form2

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
mvarForm2 = New Form2
mvarForm2.ShowDialog()
mvarForm2.Close()
mvarForm2 = Nothing
End Sub

Private Sub mvarForm2_EnterPressed(ByVal IntRet As Integer) Handles mvarForm2.EnterPressed
Me.TextBox1.Text = IntRet.ToString
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
mvarForm2a = New Form2
mvarForm2a.ShowDialog()
Me.TextBox1.Text = mvarForm2a.CalcValue.ToString
mvarForm2a.Close()
mvarForm2a = Nothing

End Sub
End Class


Code for Form2

Public Class Form2

Public CalcValue As Integer
Public Event EnterPressed(ByVal IntRet As Integer)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
CalcValue = 52
RaiseEvent EnterPressed(CalcValue)
Me.Hide()
End Sub
End Class


This code show how to set a variable that is accessible from a different form, and how to create your own event in that form.

Each code in the buttons on form1 shows a different strategy.

Button1 shows how to work it by raising an event.

Button2 shows how to work it by setting a public variable.

Coolboylp
June 3rd, 2008, 12:52 AM
Hello Sotoasty,

It worked. I modifed the code a tiny bit and its working fine.

Thank you for the code.