Elias Wick
Elias Wick

Reputation: 493

VB.Net - Call a Sub with events

Is it possible to Call a Sub with events?

I have the following sub, and when I press a button, I will trigger a Msgbox.

Public Class SelectLevel

    Public Shared Sub Button_Start_Click(sender As Object, e As EventArgs) Handles Button_Start.Click
        MsgBox("Test")
    End Sub

End Class

Can I Call this sub in another Class? Something along the lines of:

Public Class TestClass

    Private Sub Testing
        Call Button_Start_Click ' I tried this first, but it didn't work.
        Call SelectLevel.Button_Start_Click ' I tried this aswell but didn't work.
    End Sub

End Class

Thank you!

Upvotes: 0

Views: 5954

Answers (4)

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim hitung As Integer = 0
        Do While hitung < 10
            ListBox1.Items.Add("Halo Selamat Siang")
            hitung += 1
        Loop
    End Sub
End Class

Upvotes: 0

Suleyman DOGAN
Suleyman DOGAN

Reputation: 185

If the event you need is only click event then there is an easy way PerformClick function

Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Button1.PerformClick()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    MsgBox("pressed")
End Sub 
End Class

Upvotes: 0

Subaz
Subaz

Reputation: 917

Your code is correct except in one place. While calling, you have to give the required parameters

Your Sub Procedure is : Public Shared Sub Button_Start_Click(sender As Object, e As EventArgs) Handles Button_Start.Click

But you're calling as :Call SelectLevel.Button_Start_Click (without any values to be sent)

Call like this:

Private Sub Testing()
    SelectLevel.Button_Start_Click(Button_Start, Nothing) ' I tried this aswell but didn't work.
End Sub

This will do your work

Upvotes: 1

MatSnow
MatSnow

Reputation: 7537

You need to pass the parameters too.

This should work:

Public Class TestClass

    Private Sub Testing
        Call SelectLevel.Button_Start_Click(Nothing, System.EventArgs.Empty)
    End Sub

End Class

Upvotes: 2

Related Questions