VB ComboBox Requires 2 Mouse Clicks to Select Index? - vb.net

For some reason with this combobox I have in my code, when I run the program, it does not change the Selected Index with one mouse click. Instead, I have to select the drop down of the combo box again, and then select the index. Any ideas on what is going on here? Here is my code, I've tinkered with it for hours.
Public Class Form1
'Mod-level variable
Dim TransactionList As New List(Of Transaction)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Text = "Mike Smith's Bank Account"
Lb1.Visible = False
Lb2.Visible = False
Lb4.Visible = False
Tb1.Visible = False
Tb2.Visible = False
SubmitTransactionButton.Visible = False
TransactionListBox.Visible = False
End Sub
Private Sub Cb1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Cb1.SelectedIndexChanged
If Tb1.Text = "" Then
Tb1.Text = "0.00"
ElseIf Cb1.Text = "" Then
Lb4.Visible = False
LbAction.Visible = False
Lb1.Visible = False
Lb2.Visible = False
Tb1.Visible = False
Tb2.Visible = False
TransactionListBox.Visible = False
SubmitTransactionButton.Visible = False
ElseIf Cb1.Text = "Credit" Then
Lb4.Visible = False
LbAction.Visible = False
Lb1.Visible = True
Lb2.Visible = True
LbAction.Visible = False
Tb1.Visible = True
Tb2.Visible = True
TransactionListBox.Visible = False
Lb1.Text = "Enter Credit Amount"
Lb2.Text = "Describe the Income"
SubmitTransactionButton.Visible = True
ElseIf Cb1.Text = "Debit" Then
LbAction.Visible = False
Lb1.Visible = True
Lb4.Visible = False
Lb2.Visible = True
LbAction.Visible = False
Tb1.Visible = True
Tb2.Visible = True
TransactionListBox.Visible = False
Lb1.Text = "Enter Debit Amount"
Lb2.Text = "Describe the Expense"
SubmitTransactionButton.Visible = True
ElseIf Cb1.Text = "Display Transactions" Then
TransactionListBox.Visible = True
Lb4.Visible = False
LbAction.Visible = False
Lb1.Visible = False
Lb2.Visible = False
Tb1.Visible = False
Tb2.Visible = False
SubmitTransactionButton.Visible = False
For Each transaction In TransactionList
TransactionListBox.Items.Add(
String.Format("{0}: {1:C}. {2}",
transaction.Type,
transaction.Amount,
transaction.Description))
Next
ElseIf Cb1.Text = "Display Balance" Then
Lb4.Visible = True
Lb1.Visible = False
Lb2.Visible = False
LbAction.Visible = False
Tb1.Visible = False
Tb2.Visible = False
TransactionListBox.Visible = False
Lb4.Text = "Current Balance: $"
End If
End Sub
Private Sub SubmitTransactionButton_Click(sender As Object, e As EventArgs) Handles SubmitTransactionButton.Click
Dim transactionTypeString As String = Cb1.Text
Dim transactionDescriptionstring As String = Tb2.Text
Dim transactionAmountDecimal As Decimal
If Tb1.Text IsNot "" Then
'Tb1.Text = "0.00"
Tb2.Text = ""
Cb1.Text = ""
Lb1.Visible = False
Lb2.Visible = False
Tb1.Visible = False
Tb2.Visible = False
SubmitTransactionButton.Visible = False
End If
Try
transactionAmountDecimal = Decimal.Parse(Tb1.Text)
TransactionList.Add(New Transaction(
transactionTypeString,
transactionAmountDecimal,
transactionDescriptionstring))
Catch FormatExceptionParameter As FormatException
transactionAmountDecimal = 0D
End Try
End Sub
End Class
Public Class Transaction
Public Sub New(type As String, amount As Decimal, description As String)
Me.Type = type
Me.Amount = amount
Me.Description = description
If Me.Type = "Credit" Then
Me.Amount *= -1
End If
End Sub
Public Property Type As String
Public Property Amount As Decimal
Public Property Description As String
End Class

Related

Form1_load code not working when starting the program

I have written all mode code out correctly but when the program runs it does work in the program! I do not know the issue here and was hoping an expert here on VB.net could help out as I have only taken one class of programming in my life...
It is a tic tac toe game for anyone wondering.
I have tried everything I've seen on here and other websites.
Also, the point of the code is so when someone wins it is supposed to display a message box saying they won and then add 1 to the text box in the top left or right.
Here is some code from the form_load
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Generate Random Start
If TurnResult = 1 Then
lblPlayerTurn.Text = "Red Player's Turn"
End If
If TurnResult = 2 Then
lblPlayerTurn.Text = "Blue Player's Turn"
End If
'Blue Winners
If btn1.Text = "X" And btn5.Text = "X" And btn9.Text = "X" Then
MsgBox("Blue Player Wins!")
txtBlue.Text += 1
btn1.Enabled = False
btn2.Enabled = False
btn3.Enabled = False
btn4.Enabled = False
btn5.Enabled = False
btn6.Enabled = False
btn7.Enabled = False
btn8.Enabled = False
btn9.Enabled = False
End If
If btn1.Text = "X" And btn2.Text = "X" And btn3.Text = "X" Then
MsgBox("Blue Player Wins!")
txtBlue.Text += 1
btn1.Enabled = False
btn2.Enabled = False
btn3.Enabled = False
btn4.Enabled = False
btn5.Enabled = False
btn6.Enabled = False
btn7.Enabled = False
btn8.Enabled = False
btn9.Enabled = False
End If
If btn4.Text = "X" And btn5.Text = "X" And btn6.Text = "X" Then
MsgBox("Blue Player Wins!")
txtBlue.Text += 1
btn1.Enabled = False
btn2.Enabled = False
btn3.Enabled = False
btn4.Enabled = False
btn5.Enabled = False
btn6.Enabled = False
btn7.Enabled = False
btn8.Enabled = False
btn9.Enabled = False
End If
If btn7.Text = "X" And btn8.Text = "X" And btn9.Text = "X" Then
MsgBox("Blue Player Wins!")
txtBlue.Text += 1
btn1.Enabled = False
btn2.Enabled = False
btn3.Enabled = False
btn4.Enabled = False
btn5.Enabled = False
btn6.Enabled = False
btn7.Enabled = False
btn8.Enabled = False
btn9.Enabled = False
End If
If btn1.Text = "X" And btn4.Text = "X" And btn7.Text = "X" Then
MsgBox("Blue Player Wins!")
txtBlue.Text += 1
btn1.Enabled = False
btn2.Enabled = False
btn3.Enabled = False
btn4.Enabled = False
btn5.Enabled = False
btn6.Enabled = False
btn7.Enabled = False
btn8.Enabled = False
btn9.Enabled = False
End If

if else Condition in vb.net

i want the user select duration before he/she continue click the checkbox(Cagayan) how to do that?
this is my code
Private Sub Rcagayan_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cagayan.CheckedChanged
If Duration.Text = "" Then
MessageBox.Show("Please Select Duration", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End if
If Cagayan.Checked = False Then
Abulug.Checked = False
Allacapan.Checked = False
Alcala.Checked = False
Amulung.Checked = False
Aparri.Checked = False
Baggao.Checked = False
Ballesteros.Checked = False
Buguey.Checked = False
Camal.Checked = False
Claveria.Checked = False
Enrile.Checked = False
Gattaran.Checked = False
Gonzaga.Checked = False
Iguig.Checked = False
Lasam.Checked = False
Lallo.Checked = False
Pamplona.Checked = False
Penablanca.Checked = False
Piat.Checked = False
Rizal.Checked = False
SantaAna.Checked = False
StaTere.Checked = False
Santonino.Checked = False
Praxedes.Checked = False
Sanchez.Checked = False
Solana.Checked = False
Tuao.Checked = False
Tugue.Checked = False
btnUncheckedckbExpectedFalse.PerformClick()
btnAdvicedUnchecked.PerformClick()
Me.btncagayan.PerformClick()
Me.btnTstmFormat.PerformClick()
End If
End Sub
You have to add exit sub at the end of your first if block...
Private Sub Rcagayan_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cagayan.CheckedChanged
If Duration.Text = "" Then
MessageBox.Show("Please Select Duration", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
exit sub
End if
If Cagayan.Checked = False Then
Abulug.Checked = False
Allacapan.Checked = False
Alcala.Checked = False
Amulung.Checked = False
Aparri.Checked = False
Baggao.Checked = False
Ballesteros.Checked = False
Buguey.Checked = False
Camal.Checked = False
Claveria.Checked = False
Enrile.Checked = False
Gattaran.Checked = False
Gonzaga.Checked = False
Iguig.Checked = False
Lasam.Checked = False
Lallo.Checked = False
Pamplona.Checked = False
Penablanca.Checked = False
Piat.Checked = False
Rizal.Checked = False
SantaAna.Checked = False
StaTere.Checked = False
Santonino.Checked = False
Praxedes.Checked = False
Sanchez.Checked = False
Solana.Checked = False
Tuao.Checked = False
Tugue.Checked = False
btnUncheckedckbExpectedFalse.PerformClick()
btnAdvicedUnchecked.PerformClick()
Me.btncagayan.PerformClick()
Me.btnTstmFormat.PerformClick()
End If
End Sub

Microsoft Visual Basic (Checkbox relationships)

I have a problem :)
I am working in Microsoft Word 2013 right now. I made four checkboxes: "A", "B"
, "C" and "D". My wish is that there is a relationship between the checkboxes. So if I check "A" then I want that "B" and "C" are also automatically checked. And if I check "B" I want that "C" is also automatically checked. When I check "C" then I want that just "C" is checked. And if I check "D" then only "D" needs to be checked.
I already found the next code:
Sub SelectAll_Click()
Dim CB As CheckBox
For Each CB In ActiveSheet.CheckBoxes
If CB.Name <> ActiveSheet.CheckBoxes("Check Box 1").Name Then
CB.Value = ActiveSheet.CheckBoxes("Check Box 1").Value
End If
Next CB
End Sub
Sub Mixed_State()
Dim CB As CheckBox
For Each CB In ActiveSheet.CheckBoxes
If CB.Name <> ActiveSheet.CheckBoxes("Check Box 1").Name And CB.Value <> ActiveSheet.CheckBoxes("Check Box 1").Value And ActiveSheet.CheckBoxes("Check Box 1").Value <> 2 Then
ActiveSheet.CheckBoxes("Check Box 1").Value = 2
Exit For
Else
ActiveSheet.CheckBoxes("Check Box 1").Value = CB.Value
End If
Next CB
End Sub
This works for me in Excel, but not in Word (I get an error). But the main issue is that by this code all of the checkboxes are selected. That's not exactly what
I want.
I hope it is clear what I'm asking for and I hope you can help me out.
Thanks!
Kind regards.
Here is the answer (code), for anyone else in the same situation. The checkboxes have a lot of relationships with each other. I have 13 checkboxes.
Private Sub Q1A_Click()
If Q1A.Value = True Then
Q1B.Value = True
Q1C.Value = True
End If
End Sub
Private Sub Q1B_Click()
If Q1B.Value = True Then
Q1C.Value = True
ElseIf Q1B.Value = False Then
Q1A.Value = False
End If
End Sub
Private Sub Q1C_Click()
If Q1C.Value = False Then
Q1B.Value = False
Q1A.Value = False
Q1D.Value = False
End If
End Sub
Private Sub Q1D_Click()
If Q1D.Value = True Then
Q1C.Value = True
End If
End Sub
Private Sub Q1E_Click()
If Q1E.Value = True Then
Q1F.Value = True
Q1G.Value = True
Q1H.Value = True
Q1I.Value = True
Q1J.Value = True
End If
End Sub
Private Sub Q1F_Click()
If Q1F.Value = True Then
Q1G.Value = True
ElseIf Q1F.Value = False Then
Q1E.Value = False
End If
End Sub
Private Sub Q1G_Click()
If Q1G.Value = False Then
Q1F.Value = False
ElseIf Q1G.Value = False Then
Q1E.Value = False
End If
End Sub
Private Sub Q1H_Click()
If Q1H.Value = True Then
Q1I.Value = True
Q1J.Value = True
ElseIf Q1H.Value = False Then
Q1E.Value = False
End If
End Sub
Private Sub Q1I_Click()
If Q1I.Value = True Then
Q1J.Value = True
ElseIf Q1I.Value = False Then
Q1H.Value = False
Q1E.Value = False
End If
End Sub
Private Sub Q1J_Click()
If Q1J.Value = False Then
Q1I.Value = False
Q1H.Value = False
Q1E.Value = False
End If
End Sub
Private Sub Q2A_Click()
If Q2A.Value = True Then
Q2B.Value = True
Q2C.Value = True
End If
End Sub
Private Sub Q2B_Click()
If Q2B.Value = True Then
Q2C.Value = True
ElseIf Q2B.Value = False Then
Q2A.Value = False
End If
End Sub
Private Sub Q2C_Click()
If Q2C.Value = False Then
Q2B.Value = False
Q2A.Value = False
End If
End Sub
Private Sub Q1A_Click()
If Q1A.Value = True Then
Q1B.Value = True
Q1C.Value = True
End If
End Sub
Private Sub Q1B_Click()
If Q1B.Value = True Then
Q1C.Value = True
ElseIf Q1B.Value = False Then
Q1A.Value = False
End If
End Sub
Private Sub Q1C_Click()
If Q1C.Value = False Then
Q1B.Value = False
Q1A.Value = False
Q1D.Value = False
End If
End Sub
Private Sub Q1D_Click()
If Q1D.Value = True Then
Q1C.Value = True
End If
End Sub
Private Sub Q1E_Click()
If Q1E.Value = True Then
Q1F.Value = True
Q1G.Value = True
Q1H.Value = True
Q1I.Value = True
Q1J.Value = True
End If
End Sub
Private Sub Q1F_Click()
If Q1F.Value = True Then
Q1G.Value = True
ElseIf Q1F.Value = False Then
Q1E.Value = False
End If
End Sub
Private Sub Q1G_Click()
If Q1G.Value = False Then
Q1F.Value = False
ElseIf Q1G.Value = False Then
Q1E.Value = False
End If
End Sub
Private Sub Q1H_Click()
If Q1H.Value = True Then
Q1I.Value = True
Q1J.Value = True
ElseIf Q1H.Value = False Then
Q1E.Value = False
End If
End Sub
Private Sub Q1I_Click()
If Q1I.Value = True Then
Q1J.Value = True
ElseIf Q1I.Value = False Then
Q1H.Value = False
Q1E.Value = False
End If
End Sub
Private Sub Q1J_Click()
If Q1J.Value = False Then
Q1I.Value = False
Q1H.Value = False
Q1E.Value = False
End If
End Sub
Private Sub Q2A_Click()
If Q2A.Value = True Then
Q2B.Value = True
Q2C.Value = True
End If
End Sub
Private Sub Q2B_Click()
If Q2B.Value = True Then
Q2C.Value = True
ElseIf Q2B.Value = False Then
Q2A.Value = False
End If
End Sub
Private Sub Q2C_Click()
If Q2C.Value = False Then
Q2B.Value = False
Q2A.Value = False
End If
End Sub

more than one Select All Checkboxes for each frame

Following Userform has 4 (Four) Select All Check-boxes.
SelectAllE for Eastern Europe
SelectAllA for Middle East & Africa
SelectAllL for Latin America & Caribbean
SA for Select All Regions (Working)
only the fourth one is working correctly
Following are codes for each Checkboxes
Private Sub SA_Click() ' this one is working Correctly
Dim ctl As Control
Dim j As Long
For Each ctl In Me.Controls
If TypeOf ctl Is MSForms.CheckBox Then
If Me.Controls(ctl.Name).Value = True Then
Me.Bulgaria.Value = True
Me.Croatia.Value = True
Me.Croatia.Value = True
Me.czechrepublic.Value = True
Me.Estonia.Value = True
Me.Hungary.Value = True
Me.Latvia.Value = True
Me.Lithuania.Value = True
Me.Macedonia.Value = True
Me.Poland.Value = True
Me.Romania.Value = True
Me.Russia.Value = True
Me.Ukraine.Value = True
Me.Bahrain.Value = True
Me.CoteD.Value = True
Me.Egypt.Value = True
Me.Ghana.Value = True
Me.Iraq.Value = True
Me.Kenya.Value = True
Me.Kuwait.Value = True
Me.Morocco.Value = True
Me.Namibia.Value = True
Me.Nigeria.Value = True
Me.Oman.Value = True
Me.Palestine.Value = True
Me.Qatar.Value = True
Me.Rwanda.Value = True
Me.SaudiArabia.Value = True
Me.Turkey.Value = True
Me.Uganda.Value = True
Me.UAE.Value = True
Me.Zimbabwe.Value = True
Me.Chile.Value = True
Me.Mexico.Value = True
Me.Colombia.Value = True
Else
Me.Bulgaria.Value = False
Me.Croatia.Value = False
Me.Croatia.Value = False
Me.czechrepublic.Value = False
Me.Estonia.Value = False
Me.Hungary.Value = False
Me.Latvia.Value = False
Me.Lithuania.Value = False
Me.Macedonia.Value = False
Me.Poland.Value = False
Me.Romania.Value = False
Me.Russia.Value = False
Me.Ukraine.Value = False
Me.Bahrain.Value = False
Me.CoteD.Value = False
Me.Egypt.Value = False
Me.Ghana.Value = False
Me.Iraq.Value = False
Me.Kenya.Value = False
Me.Kuwait.Value = False
Me.Morocco.Value = False
Me.Namibia.Value = False
Me.Nigeria.Value = False
Me.Oman.Value = False
Me.Palestine.Value = False
Me.Qatar.Value = False
Me.Rwanda.Value = False
Me.SaudiArabia.Value = False
Me.Turkey.Value = False
Me.Uganda.Value = False
Me.UAE.Value = False
Me.Zimbabwe.Value = False
End If
End If
Next
End Sub
Private Sub SelectallE_Click() ' this one is not working Correctly
Dim ctl As Control
Dim j As Long
For Each ctl In Me.Controls
If TypeOf ctl Is MSForms.CheckBox Then
If Me.Controls(ctl.Name).Value = True Then
Me.Bulgaria.Value = True
Me.Croatia.Value = True
Me.Croatia.Value = True
Me.czechrepublic.Value = True
Me.Estonia.Value = True
Me.Hungary.Value = True
Me.Latvia.Value = True
Me.Lithuania.Value = True
Me.Macedonia.Value = True
Me.Poland.Value = True
Me.Romania.Value = True
Me.Russia.Value = True
Me.Ukraine.Value = True
Else
Me.Bulgaria.Value = False
Me.Croatia.Value = False
Me.Croatia.Value = False
Me.czechrepublic.Value = False
Me.Estonia.Value = False
Me.Hungary.Value = False
Me.Latvia.Value = False
Me.Lithuania.Value = False
Me.Macedonia.Value = False
Me.Poland.Value = False
Me.Romania.Value = False
Me.Russia.Value = False
Me.Ukraine.Value = False
Me.Chile.Value = False
Me.Mexico.Value = False
Me.Colombia.Value = False
End If
End If
Next
End Sub
Private Sub SelectAllA_Click() ' this one is not working Correctly
Dim ctl As Control
Dim j As Long
For Each ctl In Me.Controls
If TypeOf ctl Is MSForms.CheckBox Then
If Me.Controls(ctl.Name).Value = True Then
Me.Bahrain.Value = True
Me.CoteD.Value = True
Me.Egypt.Value = True
Me.Ghana.Value = True
Me.Iraq.Value = True
Me.Kenya.Value = True
Me.Kuwait.Value = True
Me.Morocco.Value = True
Me.Namibia.Value = True
Me.Nigeria.Value = True
Me.Oman.Value = True
Me.Palestine.Value = True
Me.Qatar.Value = True
Me.Rwanda.Value = True
Me.SaudiArabia.Value = True
Me.Turkey.Value = True
Me.Uganda.Value = True
Me.UAE.Value = True
Me.Zimbabwe.Value = True
Else
Me.Bahrain.Value = False
Me.CoteD.Value = False
Me.Egypt.Value = False
Me.Ghana.Value = False
Me.Iraq.Value = False
Me.Kenya.Value = False
Me.Kuwait.Value = False
Me.Morocco.Value = False
Me.Namibia.Value = False
Me.Nigeria.Value = False
Me.Oman.Value = False
Me.Palestine.Value = False
Me.Qatar.Value = False
Me.Rwanda.Value = False
Me.SaudiArabia.Value = False
Me.Turkey.Value = False
Me.Uganda.Value = False
Me.UAE.Value = False
Me.Zimbabwe.Value = False
End If
End If
Next
End Sub
Private Sub SelectAllL_Click() ' this one is not working Correctly
Dim ctl As Control
Dim j As Long
For Each ctl In Me.Controls
If TypeOf ctl Is MSForms.CheckBox Then
If Me.Controls(ctl.Name).Value = True Then
Me.Chile.Value = True
Me.Mexico.Value = True
Me.Colombia.Value = True
Else
Me.Chile.Value = False
Me.Mexico.Value = False
Me.Colombia.Value = False
End If
End If
Next
End Sub
Your code is too complicated, why not keeping it more simple? Since you are inside the Click event you know the control and don't need to loop each control of the userform!
Private Sub SA_Click()
SelectAllA.Value = SA.Value
SelectAllL.Value = SA.Value
SelectallE.Value = SA.Value
End Sub
Private Sub SelectallE_Click()
Bulgaria.Value = SelectallE.Value
Croatia.Value = SelectallE.Value
Croatia.Value = SelectallE.Value
czechrepublic.Value = SelectallE.Value
Estonia.Value = SelectallE.Value
Hungary.Value = SelectallE.Value
Latvia.Value = SelectallE.Value
Lithuania.Value = SelectallE.Value
Macedonia.Value = SelectallE.Value
Poland.Value = SelectallE.Value
Romania.Value = SelectallE.Value
Russia.Value = SelectallE.Value
Ukraine.Value = SelectallE.Value
End Sub
Private Sub SelectAllA_Click()
Bahrain.Value = SelectAllA.Value
CoteD.Value = SelectAllA.Value
Egypt.Value = SelectAllA.Value
Ghana.Value = SelectAllA.Value
Iraq.Value = SelectAllA.Value
Kenya.Value = SelectAllA.Value
Kuwait.Value = SelectAllA.Value
Morocco.Value = SelectAllA.Value
Namibia.Value = SelectAllA.Value
Nigeria.Value = SelectAllA.Value
Oman.Value = SelectAllA.Value
Palestine.Value = SelectAllA.Value
Qatar.Value = SelectAllA.Value
Rwanda.Value = SelectAllA.Value
SaudiArabia.Value = SelectAllA.Value
Turkey.Value = SelectAllA.Value
Uganda.Value = SelectAllA.Value
UAE.Value = SelectAllA.Value
Zimbabwe.Value = SelectAllA.Value
End Sub
Private Sub SelectAllL_Click()
Chile.Value = SelectAllL.Value
Mexico.Value = SelectAllL.Value
Colombia.Value = SelectAllL.Value
End Sub

Keep track of wins in my tic tac toe application VB 2010

I've written a Tic Tac Toe application in VB 2010 and it runs well, but I'd like to be able to keep track of the number of wins for X's and O's and/or Player 1 and Player 2. I've tried adding in do while loops, but I'm pretty ignorant as to how to correctly implement them and I'm unsure if that's even the correct way to track wins.. Any help would be greatly appreciated! Here's my code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub btn11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn11.Click
'clos the form
Me.Close()
End Sub
Private Sub btn10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn10.Click
btn1.Enabled = True
btn2.Enabled = True
btn3.Enabled = True
btn4.Enabled = True
btn5.Enabled = True
btn6.Enabled = True
btn7.Enabled = True
btn8.Enabled = True
btn9.Enabled = True
btn1.Text = ""
btn2.Text = ""
btn3.Text = ""
btn4.Text = ""
btn5.Text = ""
btn6.Text = ""
btn7.Text = ""
btn8.Text = ""
btn9.Text = ""
End Sub
Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
If lbl2.Text = "X" Then
btn1.Text = "X"
lbl2.Text = "O"
Else
btn1.Text = "O"
lbl2.Text = "X"
End If
btn1.Enabled = False
End Sub
Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
If lbl2.Text = "X" Then
btn2.Text = "X"
lbl2.Text = "O"
Else
btn2.Text = "O"
lbl2.Text = "X"
End If
Call win()
btn2.Enabled = False
End Sub
Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
If lbl2.Text = "X" Then
btn3.Text = "X"
lbl2.Text = "O"
Else
btn3.Text = "O"
lbl2.Text = "X"
End If
Call win()
btn3.Enabled = False
End Sub
Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
If lbl2.Text = "X" Then
btn4.Text = "X"
lbl2.Text = "O"
Else
btn4.Text = "O"
lbl2.Text = "X"
End If
Call win()
btn4.Enabled = False
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
If lbl2.Text = "X" Then
btn5.Text = "X"
lbl2.Text = "O"
Else
btn5.Text = "O"
lbl2.Text = "X"
End If
Call win()
btn5.Enabled = False
End Sub
Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
If lbl2.Text = "X" Then
btn6.Text = "X"
lbl2.Text = "O"
Else
btn6.Text = "O"
lbl2.Text = "X"
End If
Call win()
btn6.Enabled = False
End Sub
Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
If lbl2.Text = "X" Then
btn7.Text = "X"
lbl2.Text = "O"
Else
btn7.Text = "O"
lbl2.Text = "X"
End If
Call win()
btn7.Enabled = False
End Sub
Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
If lbl2.Text = "X" Then
btn8.Text = "X"
lbl2.Text = "O"
Else
btn8.Text = "O"
lbl2.Text = "X"
End If
Call win()
btn8.Enabled = False
End Sub
Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
If lbl2.Text = "X" Then
btn9.Text = "X"
lbl2.Text = "O"
Else
btn9.Text = "O"
lbl2.Text = "X"
End If
Call win()
btn9.Enabled = False
End Sub
Private Sub win()
If btn1.Text = "X" And btn2.Text = "X" And btn3.Text = "X" Then
lbl2.Text = "X wins"
btn1.Enabled = False
btn2.Enabled = False
btn3.Enabled = False
btn4.Enabled = False
btn5.Enabled = False
btn6.Enabled = False
btn7.Enabled = False
btn8.Enabled = False
btn9.Enabled = False
End If
If btn4.Text = "X" And btn5.Text = "X" And btn6.Text = "X" Then
lbl2.Text = "X wins"
btn1.Enabled = False
btn2.Enabled = False
btn3.Enabled = False
btn4.Enabled = False
btn5.Enabled = False
btn6.Enabled = False
btn7.Enabled = False
btn8.Enabled = False
btn9.Enabled = False
End If
If btn7.Text = "X" And btn8.Text = "X" And btn9.Text = "X" Then
lbl2.Text = "X wins"
btn1.Enabled = False
btn2.Enabled = False
btn3.Enabled = False
btn4.Enabled = False
btn5.Enabled = False
btn6.Enabled = False
btn7.Enabled = False
btn8.Enabled = False
btn9.Enabled = False
End If
If btn1.Text = "X" And btn4.Text = "X" And btn7.Text = "X" Then
lbl2.Text = "X wins"
btn1.Enabled = False
btn2.Enabled = False
btn3.Enabled = False
btn4.Enabled = False
btn5.Enabled = False
btn6.Enabled = False
btn7.Enabled = False
btn8.Enabled = False
btn9.Enabled = False
End If
If btn2.Text = "X" And btn5.Text = "X" And btn8.Text = "X" Then
lbl2.Text = "X wins"
btn1.Enabled = False
btn2.Enabled = False
btn3.Enabled = False
btn4.Enabled = False
btn5.Enabled = False
btn6.Enabled = False
btn7.Enabled = False
btn8.Enabled = False
btn9.Enabled = False
End If
If btn3.Text = "X" And btn6.Text = "X" And btn9.Text = "X" Then
lbl2.Text = "X wins"
btn1.Enabled = False
btn2.Enabled = False
btn3.Enabled = False
btn4.Enabled = False
btn5.Enabled = False
btn6.Enabled = False
btn7.Enabled = False
btn8.Enabled = False
btn9.Enabled = False
End If
If btn1.Text = "X" And btn5.Text = "X" And btn9.Text = "X" Then
lbl2.Text = "X wins"
btn1.Enabled = False
btn2.Enabled = False
btn3.Enabled = False
btn4.Enabled = False
btn5.Enabled = False
btn6.Enabled = False
btn7.Enabled = False
btn8.Enabled = False
btn9.Enabled = False
End If
If btn3.Text = "X" And btn5.Text = "X" And btn7.Text = "X" Then
lbl2.Text = "X wins"
btn1.Enabled = False
btn2.Enabled = False
btn3.Enabled = False
btn4.Enabled = False
btn5.Enabled = False
btn6.Enabled = False
btn7.Enabled = False
btn8.Enabled = False
btn9.Enabled = False
End If
If btn1.Text = "O" And btn2.Text = "O" And btn3.Text = "O" Then
lbl2.Text = "O wins"
btn1.Enabled = False
btn2.Enabled = False
btn3.Enabled = False
btn4.Enabled = False
btn5.Enabled = False
btn6.Enabled = False
btn7.Enabled = False
btn8.Enabled = False
btn9.Enabled = False
End If
If btn4.Text = "O" And btn5.Text = "O" And btn6.Text = "O" Then
lbl2.Text = "O wins"
btn1.Enabled = False
btn2.Enabled = False
btn3.Enabled = False
btn4.Enabled = False
btn5.Enabled = False
btn6.Enabled = False
btn7.Enabled = False
btn8.Enabled = False
btn9.Enabled = False
End If
If btn7.Text = "O" And btn8.Text = "O" And btn9.Text = "O" Then
lbl2.Text = "O wins"
btn1.Enabled = False
btn2.Enabled = False
btn3.Enabled = False
btn4.Enabled = False
btn5.Enabled = False
btn6.Enabled = False
btn7.Enabled = False
btn8.Enabled = False
btn9.Enabled = False
End If
If btn1.Text = "O" And btn4.Text = "O" And btn7.Text = "O" Then
lbl2.Text = "O wins"
btn1.Enabled = False
btn2.Enabled = False
btn3.Enabled = False
btn4.Enabled = False
btn5.Enabled = False
btn6.Enabled = False
btn7.Enabled = False
btn8.Enabled = False
btn9.Enabled = False
End If
If btn2.Text = "O" And btn5.Text = "O" And btn8.Text = "O" Then
lbl2.Text = "O wins"
btn1.Enabled = False
btn2.Enabled = False
btn3.Enabled = False
btn4.Enabled = False
btn5.Enabled = False
btn6.Enabled = False
btn7.Enabled = False
btn8.Enabled = False
btn9.Enabled = False
End If
If btn3.Text = "O" And btn6.Text = "O" And btn9.Text = "O" Then
lbl2.Text = "O wins"
btn1.Enabled = False
btn2.Enabled = False
btn3.Enabled = False
btn4.Enabled = False
btn5.Enabled = False
btn6.Enabled = False
btn7.Enabled = False
btn8.Enabled = False
btn9.Enabled = False
End If
If btn1.Text = "O" And btn5.Text = "O" And btn9.Text = "O" Then
lbl2.Text = "O wins"
btn1.Enabled = False
btn2.Enabled = False
btn3.Enabled = False
btn4.Enabled = False
btn5.Enabled = False
btn6.Enabled = False
btn7.Enabled = False
btn8.Enabled = False
btn9.Enabled = False
End If
If btn3.Text = "O" And btn5.Text = "O" And btn7.Text = "O" Then
lbl2.Text = "O wins"
btn1.Enabled = False
btn2.Enabled = False
btn3.Enabled = False
btn4.Enabled = False
btn5.Enabled = False
btn6.Enabled = False
btn7.Enabled = False
btn8.Enabled = False
btn9.Enabled = False
End If
End Sub
End Class
Declare Variables in your Class and keep track of the wins using those variables. By keeping them in the Class, as opposed to your Win() Method, they will not be reset to 0 the next time someone clicks a button that calls Win().
Private m_intPlayerOneWins As Integer = 0
Private m_intPlayerTwoWins As Integer = 0
Then in your Win() Method:
'If X Wins
m_intPlayerOneWins += 1
Or
'If O Wins
m_intPlayerTwoWins += 1