I am writing a Rock-Paper-Scissors game and have to use a ComboBox to get the user's selection. The problem is that no matter what I select, the selected index always reverts to 1, thus the user always selects rock. How can I fix this?
Imports System.Random
Public Class Form1
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
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, 4)
Dim selection As Integer
ComboBox1.SelectedIndex = selection
If b = 1 Then
Label3.Text = "Rock"
Label3.Visible = True
Select Case selection
Case selection = 1
MsgBox("Draw.")
draw += 1
Case selection = 2
MsgBox("Paper Covers Rock. You win!")
won += 1
Case selection = 3
MsgBox("Rock Crushes Scissors. You lose.")
lost += 1
End Select
ElseIf b = 2 Then
Label3.Text = "Paper"
Label3.Visible = True
Select Case selection
Case selection = 1
MsgBox("Paper Covers Rock. You lose.")
lost += 1
Case selection = 2
MsgBox("Draw.")
draw += 1
Case selection = 3
MsgBox("Scissors Cuts Paper. You win!")
won += 1
End Select
ElseIf b = 3 Then
Label3.Text = "Scissors"
Label3.Visible = True
Select Case selection
Case selection = 1
MsgBox("Rock Crushes Scissors. You win!")
won += 1
Case selection = 2
MsgBox("Scissors Cuts Paper. You lose.")
lost += 1
Case selection = 3
MsgBox("Draw.")
draw += 1
End Select
End If
played += 1
percent = (won / played) * 100
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
I think you need to change this line to
Dim selection As Integer
selection = ComboBox1.SelectedIndex
and the syntax for the SELECT....CASE is
Select Case selection
Case 1
MsgBox("Draw.")
draw += 1
Case 2
MsgBox("Paper Covers Rock. You win!")
won += 1
Case 3
MsgBox("Rock Crushes Scissors. You lose.")
lost += 1
End Select
Related
I am trying to create slide show I loaded the picture in picture box 2 through form load and in the timer options left him enabled here is my code and for some reason it is not changing pictures I also tried to do it with out loading a picture through form load same thing not changing just blank
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim max As Integer = 10000
Dim rnd As New Random
Dim rand As Integer = rnd.Next(10, max + 1)
Dim i As Integer = 1
Dim number(max - 1) As Integer
For i = 0 To max - 1
If number(i) = rand Then
rand = rnd.Next(1, max + 1)
i = -1
ElseIf number(i) = 0 Then
number(i) = rand
rand = rnd.Next(1, max + 1)
If i = max - 1 Then
Exit For
End If
i = -1
End If
Next
Timer1.Interval = number(i)
i += 1
ChangeImage()
End Sub
Private Sub ChangeImage()
Static Dim iImage1 As Integer
Select Case iImage1
Case 0
PictureBox2.Image = My.Resources.Image2
iImage1 += 1
Case 1
PictureBox2.Image = My.Resources.Classic_Burger_SpendWithPennies__2
iImage1 += 1
Case 2
PictureBox2.Image = My.Resources.Image4
iImage1 += 1
Case 3
PictureBox2.Image = My.Resources.Image5
iImage1 += 1
Case 4
PictureBox2.Image = My.Resources.Classic_Burger_SpendWithPennies__2
iImage1 += 1
End Select
I want just to change a picture every 10 seconds in picture box2
Make sure the Interval property of your Timer is set to 10,000.
Then use code like this:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Static rnd As New Random
Dim i As Integer = rnd.Next(5) ' 0 to 4 inclusive
Select Case i
Case 0
PictureBox2.Image = My.Resources.Image2
Case 1
PictureBox2.Image = My.Resources.Classic_Burger_SpendWithPennies__2
Case 2
PictureBox2.Image = My.Resources.Image4
Case 3
PictureBox2.Image = My.Resources.Image5
Case 4
PictureBox2.Image = My.Resources.Classic_Burger_SpendWithPennies__2
End Select
End Sub
But I suspect there is more to this story. What were you trying to do with the Interval?
It's because you dont pass the parameter.
In your sub ChangeImage, you set a new variable.
Change like this :
ChangeImage(i)
End Sub
Private Sub ChangeImage(iImage1)
Select Case iImage1
Private Sub Btn_Cast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Cast.Click
Dim Counter As Integer = 0
If ChkBox_Barton.CheckState = 1 And ChkBox_Martin.CheckState = 1 And ChkBox_Richards.CheckState = 1 Then
MsgBox("Don't vote for more than 2")
End If
Dim Count_Barton As Integer
Dim Count_Martin As Integer
Dim Count_Richards As Integer
If ChkBox_Barton.Checked Then Count_Barton += 1
If ChkBox_Martin.Checked = 1 Then Count_Martin += 1
If ChkBox_Richards.CheckState = 1 Then Count_Richards += 1
End Sub
Problem is, I'm trying to count it everytime, then let it reset and count again.
Example. I select Barton one time, click vote, then i should be able to select someone new and click vote and it should keep counting.
what can I do?
I need to then display my results. Should I just hold the number in a text or Integer file then display it that way?
I quickly set up your application myself.
Following Code applies to this GUI:
Code:
Public Class VoteCounter
Dim intCountBarton As Integer
Dim intCountMartin As Integer
Dim intCountRichards As Integer
Private Sub ButtonVote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonVote.Click
If CheckBoxBarton.CheckState = 1 And CheckBoxMartin.CheckState = 1 And CheckBoxRichards.CheckState = 1 Then
MsgBox("Don't vote for more than 2")
CheckBoxBarton.Checked = False
CheckBoxMartin.Checked = False
CheckBoxRichards.Checked = False
End If
If CheckBoxBarton.Checked Then
intCountBarton += 1
End If
If CheckBoxMartin.Checked Then
intCountMartin = intCountMartin + 1
End If
If CheckBoxRichards.Checked Then
intCountRichards = intCountRichards + 1
End If
CheckBoxBarton.Checked = False
CheckBoxMartin.Checked = False
CheckBoxRichards.Checked = False
End Sub
Private Sub ButtonResult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonResult.Click
MsgBox("Barton: " & intCountBarton & vbNewLine & "Martin: " & intCountMartin & vbNewLine & "Richards: " & intCountRichards)
End Sub
Private Sub ButtonReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonReset.Click
CheckBoxBarton.Checked = False
CheckBoxMartin.Checked = False
CheckBoxRichards.Checked = False
intCountBarton = 0
intCountMartin = 0
intCountRichards = 0
End Sub
End Class
Dim Count_Barton As Integer = 0
Dim Count_Martin As Integer = 0
Dim Count_Richards As Integer = 0
Private Sub Btn_Cast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Cast.Click
Dim Counter As Integer = 0 'NOT SURE WHAT THIS IS DOING... NOT BEING USED
If ChkBox_Barton.CheckState = 1 And ChkBox_Martin.CheckState = 1 And ChkBox_Richards.CheckState = 1 Then
MsgBox("Don't vote for more than 2")
Else
If ChkBox_Barton.Checked Then Count_Barton += 1
If ChkBox_Martin.Checked = 1 Then Count_Martin += 1
If ChkBox_Richards.CheckState = 1 Then Count_Richards += 1
End If
End Sub
We are currently doing a student monitoring attendance , and we want to count the total number of days each student is present and absent .
Subject LN FN MI 05/21/14 05/20/14 05/21/14 05/22/14 05/23/14 P A
Comp101 Yu Erick C (checked|(unchecked)|(checked)|(checked)|(checked)|4 | 1
"This is what we wanted to do but instead of counting horizontally it counts vertically.Is there anyone who can help us solving this problem?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Present As Integer = 0
Dim Absent As Integer = 0
For a = 0 To DataGridView1.RowCount - 1
For b = 0 To DataGridView1.ColumnCount - 8
If DataGridView1.Rows(a).Cells(b + 5).Value = True Then
Present += 1
Else
Absent += 1
End If
Next
DataGridView1.Rows(a).Cells(10).Value = Present
DataGridView1.Rows(a).Cells(11).Value = Absent
Present = 0
Absent = 0
Next
End Sub
Try This ....
I Think this is what you want...
This Code Actually Check every Cell that contains Checkbox Or DatePresented
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim count As Integer = 0
For a = 0 To DataGridView1.RowCount - 1
For b = 0 To DataGridView1.ColumnCount - 2
If DataGridView1.Rows(a).Cells(b + 1).Value = True Then
count += 1
End If
Next
DataGridView1.Rows(a).Cells(6).Value = count
count = 0
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim count As Integer = 0
For a = 0 To DataGridView1.RowCount - 1
For b = 0 To DataGridView1.ColumnCount - 6
If DataGridView1.Rows(a).Cells(b + 5).Value = True Then
count += 1
End If
Next
DataGridView1.Rows(a).Cells(4).Value = count
count = 0
Next
End Sub
Leave the Attendance Column Blank cause the Total will be insert there...
Wew, I Think it solves your Problem =D
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Present As Integer = 0
Dim Absent As Integer = 0
For a = 0 To DataGridView1.RowCount - 1
For b = 0 To DataGridView1.ColumnCount - 6
If DataGridView1.Rows(a).Cells(b + 5).Value = True Then
Present += 1
Else
Absent += 1
End If
Next
DataGridView1.Rows(a).Cells(4).Value = "Present: " & Present & " Absent: " & Absent
Present = 0
Absent = 0
Next
End Sub
Okay Here it is all done...
The code below runs and keeps repeating over and over again. I only pasted the up_timer but others do the same. Any ideas on how to make it run once, then repeat to the random loop and so on?
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Application.DoEvents()
Randomize()
Dim value As Integer = CInt(Int((4 * Rnd()) + 1))
If value = 1 Then
MsgBox("Up")
up.Start()
ElseIf value = 2 Then
MsgBox("Down")
down.Start()
ElseIf value = 3 Then
MsgBox("Left")
left.Start()
ElseIf value = 4 Then
MsgBox("Right")
right.Start()
End If
Timer1.Stop()
End Sub
Private Sub up_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles up.Tick
Static moveCount As Integer = 1
If Me.mob2.Location.Y > 12 Then
Me.mob2.Location = New Point(Me.mob2.Location.X, Me.mob2.Location.Y - 5)
End If
moveCount += 1
If moveCount = 10 Then
moveCount = 1
Me.Timer1.Start()
Me.up.Stop()
End If
End Sub
Looks like this is what you need, based on your complaint that timer1 repeats same numbers
Dim rand1 As New Random(CInt(Date.Now.Ticks And &h0000FFFF))
Dim value As Integer = rand1.Next(1, 4)
This will not repeat
Plus, you have to move Timer1.Stop() to beginning of the method
I'm trying to re-create the classic game "Simon" for a project. The code I have here should hopefully create a random number, translate that to a colour change for a random button, wait a short time, and then do the same for another random button. I can't spot any problems, but on execution the buttons remain uchanged.
Public Class MenuForm
Dim failure As Boolean
Dim pattern() As Integer
Dim maincounter As Integer = 1
Dim diff As Integer
Dim sender As Object
Dim e As EventArgs
Dim timewaited As Integer
Dim timefinished As Boolean
Private Sub Menuform_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Get the difficulty level from the player
Dim InputtedDifficulty As Integer = InputBox("Please enter difficulty. 1-Easy 2-Medium 3-Hard")
'Validate difficulty choice
Do While InputtedDifficulty > 3 Or InputtedDifficulty < 1
InputtedDifficulty = InputBox("Input incorrect. Please re-enter selection. 1-Easy 2-Medium 3-Hard")
Loop
'Set speed of blinking based on difficulty choice
Select Case InputtedDifficulty
Case 1
diff = 1000
Case 2
diff = 500
Case 3
diff = 20
End Select
End Sub
Private Sub run_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles run.Click
Call GameController()
End Sub
Private Sub GameController()
Dim buttonRepeater As Integer
'Call checkFail()
Do While failure = False
maincounter = maincounter + 1
Call Pattern_creator(sender, e)
For buttonRepeater = 1 To maincounter
Call button_controller(sender, e)
timewaited = 0
timefinished = False
ButtonTimer.Enabled = True
If timefinished = True Then
End If
Button1.BackColor = Color.Blue
Button2.BackColor = Color.Blue
Button3.BackColor = Color.Blue
Button4.BackColor = Color.Blue
Next buttonRepeater
Loop
End Sub
Private Sub Pattern_creator(ByVal sender As System.Object, ByVal e As System.EventArgs)
ReDim Preserve pattern(maincounter)
Randomize()
pattern(maincounter) = Int((Rnd() * 4) + 1)
ReDim Preserve pattern(maincounter + 1)
End Sub
Private Sub button_controller(ByVal sender As System.Object, ByVal e As System.EventArgs)
'Ths case statement takes the random number generated earlier and translates that to
'a button flash
Select Case pattern(maincounter)
Case 1
Button1.BackColor = Color.Red
Case 2
Button2.BackColor = Color.Red
Case 3
Button3.BackColor = Color.Red
Case 4
Button4.BackColor = Color.Red
End Select
End Sub
Private Sub ButtonTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonTimer.Tick
If timewaited = 5 Then
ButtonTimer.Enabled = False
timefinished = True
Else
timewaited = timewaited + 1
End If
End Sub
End Class
Any help would be very much appreciated, I've been staring at this for ages with no progress.