Ajakaiye Taiwo Peter
Ajakaiye Taiwo Peter

Reputation: 21

cmd button array in vb.net

this is my code in vb and i wanted to convert it to vb.net...i.e. i want three > buttons to access the same code on them same form when i click them

Private Sub cmdButton_Click(Index As Integer)
    Dim iAnswer As Long
    Dim Ret As Long
    Dim WrongAnswer(70000) As Long
    Dim rss As ADODB.Recordset
    Dim oDatabase As ADODB.Connection : oDatabase = New ADODB.Connection
    Dim oRs As ADODB.Recordset
    Dim oQuestion As mcQuestion

    If oQuestions(CLng(lblQuestion.Tag)).Multiple Then
        If chkAnswer(1).Value Then iAnswer = iAnswer Or 1
        If chkAnswer(2).Value Then iAnswer = iAnswer Or 2
        If chkAnswer(4).Value Then iAnswer = iAnswer Or 4
        If chkAnswer(8).Value Then iAnswer = iAnswer Or 8
        If chkAnswer(16).Value Then iAnswer = iAnswer Or 16
    Else
        If optAnswer(1).Value Then iAnswer = 1
        If optAnswer(2).Value Then iAnswer = 2
        If optAnswer(4).Value Then iAnswer = 4
        If optAnswer(8).Value Then iAnswer = 8
        If optAnswer(16).Value Then iAnswer = 16
    End If

    oQuestions(CLng(lblQuestion.Tag)).UserAnswer = iAnswer

    Ret = GetQuestion(Index) ' Index is 0 or 1 - (cmdButton_Click(0) or        cmdButton_Click(1))


    If Ret > 0 Then
        ShowQuestion(Ret) 'Ret = Question number
        If Index = 1 Then
            If oQuestions(CLng(lblQuestion.Tag)).Index = 1 Then ' Previous
                cmdButton(1).Enabled = False
            Else
                cmdButton(1).Enabled = True
            End If
            cmdButton(0).Enabled = True
            cmdButton(2).Visible = False
        Else
            If oQuestions(CLng(lblQuestion.Tag)).Index = oQuestions.Count Then   ' Next
                'Last question
                cmdButton(0).Enabled = False
                cmdButton(2).Visible = True
            Else
                cmdButton(0).Enabled = True
                cmdButton(2).Visible = False
            End If
            cmdButton(1).Enabled = True
        End If
    End If

Upvotes: 0

Views: 1071

Answers (3)

Guru Josh
Guru Josh

Reputation: 575

As @Matt Wilko suggests, you can handle the button click events for multiple buttons with the one procedure. If you really wanted to reference button indices, you can add the following to form level:

Private _myButtons() As Button = New Button() {Button1, Button2, Button3}

and your button click event procedure could look like this:

Private Sub Button_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click

Dim sButtonName As String = CType(sender, Button).Name
Dim iButtonIndex As Integer 
Dim Ret As Long 

For i As Integer = 0 to _myButtons.GetUpperBound(0) 
    If _myButtons(i).Name = sButtonName Then 
        iButtonIndex = i
        Exit For
    End If
Next i

Ret = GetQuestion(iButtonIndex)

'Etc, etc...

End Sub

Upvotes: 0

Matt Wilko
Matt Wilko

Reputation: 27322

In VB.NET you can modify the Handles statement so that one event handler handles more than one button click:

Private Sub Button_Click(sender As System.Object, e As System.EventArgs) _
    Handles Button22.Click, Button23.Click, Button24.Click

    If CType(sender, Button).Text = "Button22" Then
        'button 22 was clicked
    ElseIf CType(sender, Button).Text = "Button23" Then
        'button 23 was clicked
    ElseIf CType(sender, Button).Text = "Button24" Then
        'button 24 was clicked
    End If

End Sub

Upvotes: 2

user1518101
user1518101

Reputation: 111

I'd suggest having a look at a few tutorials on creating Buttons in VB.net. Here are 2 great links:

to add a click method for the Button:

Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
    //do something when clicked
End Sub

Upvotes: 2

Related Questions