在按下按钮之前如何执行代码?

时间:2014-03-11 03:57:11

标签: .net vb.net do-loops

我正在制作一个Rock-Paper-Scissors游戏。我需要循环代码,直到玩家选择退出。我怎样才能做到这一点。

Imports System.Random

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label3.Visible = False
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a As New Random()
        Dim b As Integer = a.Next(1, 3)
        Dim played As Integer = 0
        Dim won As Integer = 0
        Dim lost As Integer = 0
        Dim draw As Integer = 0
        Dim percent As Single = 0.0

        If b = 1 Then
            Label3.Text = "Rock"
            Label3.Visible = True

            Select Case ComboBox1.SelectedIndex
                Case ComboBox1.SelectedIndex = 1
                    MsgBox("Draw.")
                    draw += 1
                Case ComboBox1.SelectedIndex = 2
                    MsgBox("Paper Covers Rock. You win!")
                    won += 1
                Case ComboBox1.SelectedIndex = 3
                    MsgBox("Rock Crushes Scissors. You lose.")
                    lost += 1
            End Select

        ElseIf b = 2 Then
            Label3.Text = "Paper"
            Label3.Visible = True

            Select Case ComboBox1.SelectedIndex
                Case ComboBox1.SelectedIndex = 1
                    MsgBox("Paper Covers Rock. You lose.")
                    lost += 1
                Case ComboBox1.SelectedIndex = 2
                    MsgBox("Draw.")
                    draw += 1
                Case ComboBox1.SelectedIndex = 3
                    MsgBox("Scissors Cuts Paper. You win!")
                    won += 1
             End Select

        ElseIf b = 3 Then
            Label3.Text = "Scissors"
            Label3.Visible = True

            Select Case ComboBox1.SelectedIndex
                Case ComboBox1.SelectedIndex = 1
                    MsgBox("Rock Crushes Scissors. You win!")
                    won += 1
                Case ComboBox1.SelectedIndex = 2
                    MsgBox("Scissors Cuts Paper. You lose.")
                    lost += 1
                Case ComboBox1.SelectedIndex = 3
                    MsgBox("Draw.")
                    draw += 1
            End Select

        End If

        played += 1
        percent = won / played

        PlayedText.Text = played.ToString
        WonText.Text = won.ToString
        LostText.Text = lost.ToString
        DrawText.Text = draw.ToString
        PercentText.Text = percent.ToString


    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub
End Class

2 个答案:

答案 0 :(得分:1)

您的问题与变量范围有关。由于您定义了playedwonlostdraw和&按钮单击处理程序中的percent个变量,每次单击按钮时变量都是新的。您需要移动这些变量,以便在Button1_Click之外定义它们。

MSDN上的此页面描述了变量范围的工作原理:http://msdn.microsoft.com/en-us/library/1t0wsc67.aspx

现在这些变量都在“程序范围”中,但您需要将它们设置为“模块范围”,以便它们在按钮点击时保留其值。

答案 1 :(得分:0)

我不确定这是否是你需要的。另外我假设你正在研究winforms。我已经用c#编写了代码,但将其转换为vb.net对你来说不是一个很大的挑战。

OnMouseDown事件的事件处理程序应该收到MouseEventArgs,它应该告诉您是否按下了左键。但是你需要创建一个条件来终止它。例如,创建一个布尔变量,说isMouseDown,并在第一次将其设置为true。然后,当MouseUp事件发生时,您将其设置为false。如果在mouseDownEventHandler内为false,也将其设置为true。示例如下。

我没有测试过,所以请这样做。

bool isMouseDown = true;

private void mouseDownEventHandler(object sender, MouseEventArgs e)
{
   if(!isMouseDown)
       isMouseDown = true;

   if(e.Button == MouseButtons.Left)
   {
     //do left stuff
     if(isMouseDown)
     {
         //Call mouseDownEventHandler again
     }
   }
   else 
   {
     // do other stuff
   }
}

private void mouseUpEventHandler(object sender, MouseEventArgs e)
{
   isMouseDown = false;
}

希望这有帮助。