cmd button array in

this is my code in vb and i wanted to convert it to 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
        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
                cmdButton(1).Enabled = True
            End If
            cmdButton(0).Enabled = True
            cmdButton(2).Visible = False
            If oQuestions(CLng(lblQuestion.Tag)).Index = oQuestions.Count Then   ' Next
                'Last question
                cmdButton(0).Enabled = False
                cmdButton(2).Visible = True
                cmdButton(0).Enabled = True
                cmdButton(2).Visible = False
            End If
            cmdButton(1).Enabled = True
        End If
    End If


I'd suggest having a look at a few tutorials on creating Buttons in 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

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

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

Need Your Help

Python overflowerror

python python-2.7

I am using Python 2.7 and I am getting the following error. How can I fix it?

Create a mobile site for already existing Wordpress site

wordpress wordpress-plugin wordpress-theming mobile-website

I want to build a mobile website for an already existing Wordpress site. I prefer to redirect the users to the mobile site automatically if the user is browsing from a mobile device. I will also ne...