按钮事件中选择案例中的意外行为

时间:2014-12-13 17:41:53

标签: .net vb.net winforms

所以我正在尝试制作一个程序来决定两个玩家中的哪一个通过每人掷骰2次并且将这些玩家的价值加在一起并进行比较。我目前的问题是我的案例选择语句给我错误“错误1运算符'='没有为类型'System.Windows.Forms.PictureBox'和'整数'定义”我已经盯着这三个小时。谁能帮我? (是的,我是VB新手)

    Private Sub btnRollDice_Click(sender As Object, e As EventArgs) Handles btnRollDice.Click
    Dim intRandGen As New Random
    Dim intPlayer1Num1 As Integer
    Dim intPlayer1Num2 As Integer
    Dim intPlayer2Num1 As Integer
    Dim intplayer2Num2 As Integer
    Static intPlayer1Total As Integer
    Static intPlayer2Total As Integer
    Static intTieTotal As Integer

    'assign random integer from 1 through 6
    intPlayer1Num1 = intRandGen.Next(1, 7)
    intPlayer1Num2 = intRandGen.Next(1, 7)
    intPlayer2Num1 = intRandGen.Next(1, 7)
    intplayer2Num2 = intRandGen.Next(1, 7)

    'display appropriate image in picDie1 for player one
    Select Case player2PicDie1
        Case 1
            player2PicDie1.Image = picOneDot.Image
        Case 2
            player2PicDie1.Image = picTwoDots.Image
        Case 3
            player2PicDie1.Image = picThreeDots.Image
        Case 4
            player2PicDie1.Image = picFourDots.Image
        Case 5
            player2PicDie1.Image = picFiveDots.Image
        Case 6
            player2PicDie1.Image = picSixDots.Image
    End Select

    'display appropriate image in picDie2 for player one
    Select Case Player1PicDie2
        Case 1
            Player1PicDie2.Image = picOneDot.Image
        Case 2
            Player1PicDie2.Image = picTwoDots.Image
        Case 3
            Player1PicDie2.Image = picThreeDots.Image
        Case 4
            Player1PicDie2.Image = picFourDots.Image
        Case 5
            Player1PicDie2.Image = picFiveDots.Image
        Case 6
            Player1PicDie2.Image = picSixDots.Image
    End Select

    'display appropriate image in picDie1 for player two
    Select Case player2PicDie1
        Case 1
            player2PicDie1.Image = picOneDot.Image
        Case 2
            player2PicDie1.Image = picTwoDots.Image
        Case 3
            player2PicDie1.Image = picThreeDots.Image
        Case 4
            player2PicDie1.Image = picFourDots.Image
        Case 5
            player2PicDie1.Image = picFiveDots.Image
        Case 6
            player2PicDie1.Image = picSixDots.Image
    End Select

    'display appropriate image in picDie2 for player two
    Select Case Player2PicDie2
        Case 1
            Player2PicDie2.Image = picOneDot.Image
        Case 2
            Player2PicDie2.Image = picTwoDots.Image
        Case 3
            Player2PicDie2.Image = picThreeDots.Image
        Case 4
            Player2PicDie2.Image = picFourDots.Image
        Case 5
            Player2PicDie2.Image = picFiveDots.Image
        Case 6
            Player2PicDie2.Image = picSixDots.Image
    End Select

    'Calculate and display total number of dots for player one
    intPlayer1Total = intPlayer1Num1 + intPlayer1Num2
    lblPlayer1Total.Text = intPlayer1Total.ToString()

    'Calculate and display total number of dots for player two
    intPlayer2Total = intPlayer2Num1 + intplayer2Num2
    lblPlayer2Total.Text = intPlayer2Total.ToString()


    If lblPlayer1Total.Text = lblPlayer2Total.Text Then
        MessageBox.Show("You have tied")
    End If
End Sub

2 个答案:

答案 0 :(得分:1)

如果我理解你的代码,你试图在选择中分支图片类型而不是整数类型值...案例

根据程序上下文,我认为你应该分支一个整数,如:

Select Case intPlayer1Num1  'in place of intPlayer1Num1, you can use appropriate integer variable/value
Case 1 
      player2PicDie1.Image = picOneDot.Image 
Case 2 
      player2PicDie1.Image = picTwoDots.Image

...

答案 1 :(得分:0)

您正在创建4个随机数并将其分配给整数变量:

intPlayer1Num1 = intRandGen.Next(1, 7)
intPlayer1Num2 = intRandGen.Next(1, 7)
intPlayer2Num1 = intRandGen.Next(1, 7)
intplayer2Num2 = intRandGen.Next(1, 7)

因此,您应该在Select Case语句中使用这些整数变量而不是PictureBox变量。并且在您选择的情况下,您的图片图像也不正确:

'display appropriate image in picDie1 for player one
Select Case intPlayer1Num1 
    Case 1
        Player1PicDie1.Image = picOneDot.Image
    Case 2
        Player1PicDie1.Image = picTwoDots.Image
    Case 3
        Player1PicDie1.Image = picThreeDots.Image
    Case 4
        Player1PicDie1.Image = picFourDots.Image
    Case 5
        Player1PicDie1.Image = picFiveDots.Image
    Case 6
        Player1PicDie1.Image = picSixDots.Image
End Select

'display appropriate image in picDie2 for player one
Select Case intPlayer1Num2 
    Case 1
        Player1PicDie2.Image = picOneDot.Image
    Case 2
        Player1PicDie2.Image = picTwoDots.Image
    Case 3
        Player1PicDie2.Image = picThreeDots.Image
    Case 4
        Player1PicDie2.Image = picFourDots.Image
    Case 5
        Player1PicDie2.Image = picFiveDots.Image
    Case 6
        Player1PicDie2.Image = picSixDots.Image
End Select

'display appropriate image in picDie1 for player two
Select Case intPlayer2Num1 
    Case 1
        player2PicDie1.Image = picOneDot.Image
    Case 2
        player2PicDie1.Image = picTwoDots.Image
    Case 3
        player2PicDie1.Image = picThreeDots.Image
    Case 4
        player2PicDie1.Image = picFourDots.Image
    Case 5
        player2PicDie1.Image = picFiveDots.Image
    Case 6
        player2PicDie1.Image = picSixDots.Image
End Select

'display appropriate image in picDie2 for player two
Select Case intplayer2Num2 
    Case 1
        Player2PicDie2.Image = picOneDot.Image
    Case 2
        Player2PicDie2.Image = picTwoDots.Image
    Case 3
        Player2PicDie2.Image = picThreeDots.Image
    Case 4
        Player2PicDie2.Image = picFourDots.Image
    Case 5
        Player2PicDie2.Image = picFiveDots.Image
    Case 6
        Player2PicDie2.Image = picSixDots.Image
End Select