Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

Help with activating Forms and button click events using vb.net

Hi,

Using a button  click event, how do you activate a  Form Load event of another Form?

Thanks,
Victor
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Hi Victor;

To your question, "Using a button  click event, how do you activate a  Form Load event of another Form?", I am assuming that the other form is already created and you have an instance of the other form. I want to say that this is not the Best Practices to call another forms Load event. The below code is the answer to the question.
Public Class Form1

    Dim f2 As Form2

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        f2 = New Form2()
        f2.Show()

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        f2.ActivateLoadEvent()
    End Sub

End Class

'' The other form

Public Class Form2

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        MessageBox.Show("Hello Wrold")
    End Sub

    Public Sub ActivateLoadEvent()
        Me.Form2_Load(Nothing, Nothing)
    End Sub

End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Victor  Charles

ASKER

Hi Fernando,

I only need one button click event. When I click on the search button from Form1, I am displaying the results in Form2 because the code is activated in Form Load event of Form2 to display the data, problem is when I close Form2 by using Form2.visible = False and press the search button again on Form1 the code in Form2 Load event does not execute.

I tried your code but the code in Form2 Load event still does not execute when I press the search button in Form1 after closing Form2.

Thanks,

Victor
Hi,

It works with Form2.close.

Thanks,

Victor
Thank You.
Hi Victor;

To your previous statement, "I am displaying the results in Form2 because the code is activated in Form Load event of Form2 to display the data, problem is when I close Form2 by using Form2.visible = False and press the search button again on Form1 the code in Form2 Load event does not execute.", The Form_Load event only happens once in the life cycle that is just after the Form is create and initialized. Therefore if you were to make the Form invisible by changing its Visible property to False then you will not be able to see the Form but the Form has NOT been closed, closed is having the Form disposed of.
Hi Fernando,
I understand.
Thanks,
Victor