0

这是我在 vb 中的代码,我想将其转换为 vb.net...即我想要三个 > 按钮在我单击它们时以相同的形式访问它们上的相同代码

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
4

3 回答 3

2

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
于 2012-07-16T08:41:41.567 回答
2

在 VB.NET 中,您可以修改Handles语句,以便一个事件处理程序处理多个按钮单击:

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
于 2012-07-16T08:49:35.760 回答
0

正如@Matt Wilko 建议的那样,您可以使用一个过程处理多个按钮的按钮单击事件。如果您真的想引用按钮索引,可以将以下内容添加到表单级别:

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

您的按钮单击事件过程可能如下所示:

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
于 2014-05-11T05:20:08.900 回答