CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2003
    Posts
    124

    Raise an event on a child form from another form

    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.

  2. #2
    Join Date
    Apr 2007
    Posts
    81

    Re: Raise an event on a child form from another form

    Coolboylp,

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

    Kerry Moorman

  3. #3
    Join Date
    Apr 2003
    Posts
    124

    Re: Raise an event on a child form from another form

    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.

  4. #4
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: Raise an event on a child form from another form

    Try something like this.

    form1 Add 2 buttons and a text box

    form2 Add a button

    Code for form1

    Code:
    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
    Code:
    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.

  5. #5
    Join Date
    Apr 2003
    Posts
    124

    Re: Raise an event on a child form from another form

    Hello Sotoasty,

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

    Thank you for the code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured