Hello,

I'm a beginner in Visual Basic and I was wondering if it is possible to shorten twenty subs into only 1 sub.

example:

Private Sub box1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles kist1.Click
        Dim box1Click As Boolean = True
    End Sub

Private Sub box2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles kist1.Click
        Dim box2Click As Boolean = True
    End Sub

...

Private Sub box20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles kist1.Click
        Dim box20Click As Boolean = True
    End Sub

Thank you

No. Each sub in a VB .NET program handles a specific event e.g a click on a button. You cannot collect all the events to be handled in only one sub therefore.

Regards

actually it is possible. not very sexy but possible...

Private Sub box1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles box1.Click, box2.Click, box3.Click

        Select Case sender.Name
            Case "box1"
                'code for box1
            Case "box2"
                'code for box2
            Case "box3"
                'code for box3
            Case Else

        End Select

    End Sub

Hello,

I'm a beginner in Visual Basic and I was wondering if it is possible to shorten twenty subs into only 1 sub.

example:

Private Sub box1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles kist1.Click
        Dim box1Click As Boolean = True
    End Sub

Private Sub box2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles kist1.Click
        Dim box2Click As Boolean = True
    End Sub

...

Private Sub box20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles kist1.Click
        Dim box20Click As Boolean = True
    End Sub

Thank you

1. Your example has "Handles object.Click" where the event is the same object (kist1) When you click on the kist1 object, I believe all the routines would be called just as well as one object. (I've never done so and see no purpose in doing so.)
2. Your examples creates a variable, sets its value, and then discards it without doing anything with it. That doesn't make sense, but you can do multiple things that don't make sense in one routine just as well as 20.
3. Since it passes "ByVal sender As System.Object", this routine is designed to handle multiple events, and is fairly easy to do in C#, but since I am a beginner, I too don't know how to direct multiple objects' one event type to one subroutine. I've got 9 routines that call one routine passing a Byte that tells me which object was called and it does the work. It would have been helpful to know how to do this in VB.NET as well.
PS You can put common logic in one routine and call it 20 times:

Private Sub General_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'Do something more complex like
        Dim btn As System.Windows.Forms.Button
        btn = sender
        If btn.Name = "Calculate_btn" Then
            'do something
        End If
    End Sub
    Private Sub Calculate1_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calculate_btn1.Click
        General_btn_Click(sender, e)
    End Sub

PPS ByVal is a misnomer, no simple object has event handling so ALL senders are passed ByRef.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.