8 Replies - 415 Views - Last Post: 17 January 2013 - 05:35 AM Rate Topic: -----

#1 Untalented  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 136
  • Joined: 31-January 12

Restart Code

Posted 17 January 2013 - 01:55 AM

Hi, im trying to make a very simple game, the first one to make it to zero wins, only 1,2,3 are only accepted.

The problem is after the number goes down to zero and declares a winner i dont know how to make it go to start again. "Retry" in short, and im trying to make the button be pressed using enter.

Here is my code
Public Class Form1

    Dim p1, p2 As Integer

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        Dim x As Integer = Asc(e.KeyChar)
        Select Case x
            Case 33 To 48
                e.Handled = True
            Case 52 To 255
                e.Handled = True

        End Select

    End Sub

    Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        Dim x As Integer = Asc(e.KeyChar)
        Select Case x
            Case 33 To 48
                e.Handled = True
            Case 52 To 255
                e.Handled = True

        End Select

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Button1.Enabled = False
        p1 = TextBox1.Text


        If p1 <= Val(Label3.Text) Then
            Label3.Text = Val(Label3.Text) - p1

        End If
        Button2.Enabled = True
        If Label3.Text = 0 Then
            TextBox1.Clear()
            Button1.Enabled = False
            MsgBox("PlAYER 1 WINS")

            'Button2.Enabled = False




        End If
        TextBox1.Clear()
        TextBox2.Focus()







    End Sub

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

        Button2.Enabled = False
        p2 = TextBox2.Text

        If p2 <= Val(Label3.Text) Then
            Label3.Text = Val(Label3.Text) - p2

        End If
        Button1.Enabled = True

        If Label3.Text = 0 Then
            TextBox2.Clear()
            Button1.Enabled = False
            MsgBox("PlAYER 2 WINS!")

            'Button2.Enabled = False



        End If
        TextBox2.Clear()
        TextBox1.Focus()








    End Sub
End Class





Thanks in advance.

This post has been edited by Untalented: 17 January 2013 - 02:09 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Restart Code

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3490
  • View blog
  • Posts: 11,902
  • Joined: 12-December 12

Re: Restart Code

Posted 17 January 2013 - 04:11 AM

Double-clicking an empty area of the form will create the Form_Load event. Adding the following code will make a button respond to the Enter key (as though the button were clicked); similarly if you want a button to respond to the Escape key.

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.AcceptButton = Button1      'on Enter
        Me.CancelButton = Button2      'on Esc
    End Sub


To restart the game you would just set relevant values (Label.Text, TextBox.Text, TextBox.Clear) to their original (default) values.

BTW Your code will create errors at certain points as you are comparing Text to numerical values (0); VB will not automatically convert empty-text to 0. Use code such as:

        If String.IsNullOrEmpty(Label1.Text) Then

        End If

This post has been edited by andrewsw: 17 January 2013 - 04:13 AM

Was This Post Helpful? 1
  • +
  • -

#3 Untalented  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 136
  • Joined: 31-January 12

Re: Restart Code

Posted 17 January 2013 - 04:30 AM

 Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        Me.AcceptButton = Button1 'on Enter
        'Me.CancelButton = Button2      on Esc
        Me.AcceptButton = Button2
    End Sub


Im trying to add this codes but as soon as i press enter the button 2 locks (disabled)

This is how the game works, you can only enter 1,2,3 the goal of the game is to make this zero.

After the user for example entered "3" the button 1 will be locked and then it's player 2's turn to enter number, then after entering his/her button will be locked and then the player 1 can enter his/her digit again.

Public Class Form1

    Dim p1, p2 As Integer
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        Me.AcceptButton = Button1 'on Enter
        'Me.CancelButton = Button2      on Esc
        Me.AcceptButton = Button2
    End Sub

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        Dim x As Integer = Asc(e.KeyChar)
        Select Case x
            Case 33 To 48
                e.Handled = True
            Case 52 To 255
                e.Handled = True

        End Select

    End Sub

    Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        Dim x As Integer = Asc(e.KeyChar)
        Select Case x
            Case 33 To 48
                e.Handled = True
            Case 52 To 255
                e.Handled = True

        End Select

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Button1.Enabled = False
        p1 = TextBox1.Text


        If p1 <= Val(Label3.Text) Then
            Label3.Text = Val(Label3.Text) - p1

        End If
        TextBox1.Clear()
        TextBox2.Focus()
        Button2.Enabled = True
        'If Label3.Text = 0 Then
        If String.IsNullOrEmpty(Label3.Text) Then

            TextBox1.Clear()
            Button1.Enabled = False
            MsgBox("PLAYER 1 WINS")

        End If


    End Sub

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

        Button2.Enabled = False
        p2 = TextBox2.Text

        If p2 <= Val(Label3.Text) Then
            Label3.Text = Val(Label3.Text) - p2

        End If
        Button1.Enabled = True
        TextBox2.Clear()
        TextBox1.Focus()

        'If Label3.Text = 0 Then
        If String.IsNullOrEmpty(Label3.Text) Then

            TextBox2.Clear()
            Button1.Enabled = False
            MsgBox("PlAYER 2 WINS!")

        End If

    End Sub

 
End Class





And i have no idea how to do this

Quote

To restart the game you would just set relevant values (Label.Text, TextBox.Text, TextBox.Clear) to their original (default) values.

This post has been edited by Untalented: 17 January 2013 - 06:37 AM

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3490
  • View blog
  • Posts: 11,902
  • Joined: 12-December 12

Re: Restart Code

Posted 17 January 2013 - 04:57 AM

Me.AcceptButton = Button1
Me.AcceptButton = Button2

A form only has one AcceptButton, so this code will leave Button2 as the AcceptButton. Delete the first (or second) line.

Quote

Im trying to add this codes but as soon as i press enter the button 2 locks (disabled)


That is what you have told it to do. When you press Enter it runs the code that you've added to Button2's click event.

Quote

And i have no idea how to do this


Eh? You've already done it in your code. E.g. TextBox1.Clear(). You can use Label1.Text = "".
Was This Post Helpful? 1
  • +
  • -

#5 Untalented  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 136
  • Joined: 31-January 12

Re: Restart Code

Posted 17 January 2013 - 04:57 AM

 
            'If Label3.Text = 0 Then
            If String.IsNullOrEmpty(Label3.Text) Then

            TextBox1.Clear()
            Button1.Enabled = False
            MsgBox("PLAYER 1 WINS")

        End If


This doesnt work, it doesnt display the msgbox after the label3(number) becomes 0
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3490
  • View blog
  • Posts: 11,902
  • Joined: 12-December 12

Re: Restart Code

Posted 17 January 2013 - 05:03 AM

This doesnt work, it doesnt display the msgbox after the label3(number) becomes 0

This doesn't work?? If the label text were "0" then you would compare it like this:

If Label3.Text = "0" Then
Was This Post Helpful? 0
  • +
  • -

#7 Untalented  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 136
  • Joined: 31-January 12

Re: Restart Code

Posted 17 January 2013 - 05:17 AM

I thought you want me to use this
If String.IsNullOrEmpty(Label3.Text) Then
instead of
If Label3.Text = 0 Then


Quote

A form only has one AcceptButton, so this code will leave Button2 as the AcceptButton. Delete the first (or second) line.


Thanks, i thought i can make 2 acceptbutttons just by assigning the other button to be acceptbutton, so the other player can press enter and the other one can press esc.



-Thanks

This post has been edited by Untalented: 17 January 2013 - 06:48 AM

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3490
  • View blog
  • Posts: 11,902
  • Joined: 12-December 12

Re: Restart Code

Posted 17 January 2013 - 05:29 AM

To clarify (hopefully):

A label's text is always text, so cannot be set to a number 0; it can be set to the text "0", or empty text "", or "any other text".

So, to check the label, you would test either Label1.Text = "0" or Label1.Text = "" (or equal to "some other text"). [Testing .Text = 0 as you had originally is incorrect.]

A textbox's text can either be "some text", "" (empty text) or occasionally Null. So to test if a textbox is empty:

If String.IsNullOrEmpty(TextBox1.Text) Then

Optionally, there is IsNullOrWhiteSpace which would test for empty text, Null or just white-spaces (spaces, newlines, tabs).

This post has been edited by andrewsw: 17 January 2013 - 05:32 AM

Was This Post Helpful? 1
  • +
  • -

#9 Untalented  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 136
  • Joined: 31-January 12

Re: Restart Code

Posted 17 January 2013 - 05:35 AM

Thanks andrewsw

Im done, it is working the way i wanted.

Thank you very much!

This post has been edited by Untalented: 17 January 2013 - 06:36 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1