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
vcharlesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
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

Fernando SotoRetiredCommented:
This is a better way of doing it, make changes in Form2.
Public Class Form2

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ActivateLoadEvent()
    End Sub

    Public Sub ActivateLoadEvent()
        MessageBox.Show("Hello Wrold")
    End Sub

End Class

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vcharlesAuthor Commented:
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
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

vcharlesAuthor Commented:
Hi,

It works with Form2.close.

Thanks,

Victor
vcharlesAuthor Commented:
Thank You.
Fernando SotoRetiredCommented:
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.
vcharlesAuthor Commented:
Hi Fernando,
I understand.
Thanks,
Victor
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.