System.InvalidCastException: 'Conversion from string "X" to type 'Double' is not valid.' - vb.net

So basically when i used player1Score = player1Score + 1 and lblP1ScoreNumber.Text = player1Score it worked but it added one to both of the players score instead of just whoever won so i tried using an if statement to say "if whosTurn = x Player1Score = Player1Score + 1 lblP1ScoreNumber.Text = Player1Score ElseIf WhosTurn = 0 Then Player2Score = Player2Score + 1 lblP2ScoreNumber.Text = Player2Score End If" but then the error message that is in the title popped up at this line"ElseIf WhosTurn = 0 Then" any ideas? ( this is the error message"System.InvalidCastException: 'Conversion from string "X" to type 'Double' is not valid.' FormatException: Input string was not in a correct format.") Ill add the code again here so its a bit easier too see both the erroe message and the full code i have so far: I tried what i said in the details of the problem Public Class Form1
Dim Board(8) As String
Dim Player1 As String = "X"
Dim Player2 As String = "O"
Dim XScore As String = ""
Dim WhosTurn As String = Player1
Dim Player1Score As Integer
Dim Player2Score As Integer
Public Property X As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ResetBoard()
End Sub
Private Sub ResetBoard()
WhosTurn = Player1
For i = 0 To 8
Board(i) = ""
Next
If WhosTurn = Player1 Then
lblPlayerTurn.Text = "It is player one's turn"
ElseIf WhosTurn <> Player1 Then
End
End If
lblScorePlayer1X.Text = "Player 1 score"
lblPlayer2O.Text = "Player 2 score"
'If WhosTurn = Player1 Then
'lblPlayerTurn.Text = "It is player one's turn"
'ElseIf WhosTurn = Player2 Then
'lblPlayerTurn.Text = "It is player two's turn"
'End If
Button1.Text = ""
Button2.Text = ""
Button3.Text = ""
Button4.Text = ""
Button5.Text = ""
Button6.Text = ""
Button7.Text = ""
Button8.Text = ""
Button9.Text = ""
End Sub
Private Function CheckForWin(ByRef PlayerToCheck As String) As Boolean
'Player1 wins
If (Board(0) = PlayerToCheck And Board(3) = PlayerToCheck And Board(6) = PlayerToCheck) Then
Return True
ElseIf (Board(1) = PlayerToCheck And Board(4) = PlayerToCheck And Board(7) = PlayerToCheck) Then
Return True
ElseIf (Board(2) = PlayerToCheck And Board(5) = PlayerToCheck And Board(8) = PlayerToCheck) Then
Return True
ElseIf (Board(0) = PlayerToCheck And Board(1) = PlayerToCheck And Board(2) = PlayerToCheck) Then
Return True
ElseIf (Board(3) = PlayerToCheck And Board(4) = PlayerToCheck And Board(5) = PlayerToCheck) Then
Return True
ElseIf (Board(6) = PlayerToCheck And Board(7) = PlayerToCheck And Board(8) = PlayerToCheck) Then
Return True
ElseIf (Board(0) = PlayerToCheck And Board(4) = PlayerToCheck And Board(8) = PlayerToCheck) Then
Return True
ElseIf (Board(2) = PlayerToCheck And Board(4) = PlayerToCheck And Board(6) = PlayerToCheck) Then
Return True
End If
Return False
End Function
Private Sub TakeTurn(ByVal Position As Integer)
If (WhosTurn = Player1) Then
Board(Position) = Player1
Else
Board(Position) = Player2
End If
'Check for win is here if you get confused
If (CheckForWin(WhosTurn) = True) Then
MessageBox.Show("Player " & WhosTurn & " Won")
If WhosTurn = "X" Then
Player1Score = Player1Score + 1
lblP1ScoreNumber.Text = Player1Score
ElseIf WhosTurn = "O" Then
Player2Score = Player2Score + 1
lblP2ScoreNumber.Text = Player2Score
End If
ResetBoard()
Return
End If
If (WhosTurn = Player1) Then
WhosTurn = Player2
Else
WhosTurn = Player1
End If
If WhosTurn = Player2 Then
lblPlayerTurn.Text = "It is player two's turn"
ElseIf WhosTurn = Player1 Then
lblPlayerTurn.Text = "It is player one's turn"
End If
'You've got that code wired to the label's on click event. Is that intended?
'Shouldn 't it be at the bottom of the TakeTurn sub? Looks almost like you could replace
'that If statement and just set the label directly there instead of WhosTurn
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Board(0) <> "" Then
Return
End If
Button1.Text = WhosTurn
TakeTurn(0)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If Board(1) <> "" Then
Return
End If
Button2.Text = WhosTurn
TakeTurn(1)
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
If Board(2) <> "" Then
Return
End If
Button3.Text = WhosTurn
TakeTurn(2)
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
If Board(3) <> "" Then
Return
End If
Button4.Text = WhosTurn
TakeTurn(3)
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
If Board(4) <> "" Then
Return
End If
Button5.Text = WhosTurn
TakeTurn(4)
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
If Board(5) <> "" Then
Return
End If
Button6.Text = WhosTurn
TakeTurn(5)
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
If Board(6) <> "" Then
Return
End If
Button7.Text = WhosTurn
TakeTurn(6)
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
If Board(7) <> "" Then
Return
End If
Button8.Text = WhosTurn
TakeTurn(7)
End Sub
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
If Board(8) <> "" Then
Return
End If
Button9.Text = WhosTurn
TakeTurn(8)
End Sub
Private Sub LabelTurn()
If WhosTurn = Player1 Then
lblPlayerTurn.Text = Player1
Else
lblPlayerTurn.Text = Player2
End If
End Sub
End Class

Related

SSL/TLS connection not working in webclient after being in background

So if I use my program normally no error occurs. But if it is in background for longer time and I use the webclient to visit a web address it immidiately throws could not create a SSL/TLS secure channel error but if I try again the error dissapears. I already set the TLS to 1.2 and expect100continue to false.
Imports System.Net
Imports System.Text.RegularExpressions
Public Class CinemaLinker
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MaximizeBox = False
If Not HaveInternetConnection() Then
MessageBox.Show("Warning: no internet connection!")
End If
ServicePointManager.Expect100Continue = False
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
End Sub
Public Property TextFromBox As String
Public Property StatusLink As String
Private Client As New WebClient
Private GoogleSearch = "****"
Private BingSearch = "*****"
Private AskSearch = "*****"
Private Sub Search() Handles Button1.Click
Cursor.Current = Cursors.WaitCursor
TextFromBox = RichTextBox1.Text
If Button3.Enabled And Button4.Enabled Then
Cursor.Current = Cursors.[Default]
MessageBox.Show("Please choose a category!")
Exit Sub
End If
If TextFromBox.Contains("Type movie or tv series name here") Or String.IsNullOrEmpty(TextFromBox) Or String.IsNullOrWhiteSpace(TextFromBox) Then
Cursor.Current = Cursors.[Default]
If Not Button3.Enabled Then
MessageBox.Show("Please type the name of the movie!")
Exit Sub
End If
If Not Button4.Enabled Then
If Not (String.IsNullOrEmpty(RichTextBox3.Text) Or String.IsNullOrWhiteSpace(RichTextBox3.Text) Or RichTextBox3.Text.Contains("E")) Then
Cursor.Current = Cursors.[Default]
MessageBox.Show("Please type the name of the tv series! Episode number missing!")
Exit Sub
End If
If String.IsNullOrEmpty(RichTextBox2.Text) Or String.IsNullOrWhiteSpace(RichTextBox2.Text) Or RichTextBox2.Text.Contains("S") Then
Cursor.Current = Cursors.[Default]
MessageBox.Show("Please type the name of the tv series! Season and episode numbers missing!")
Exit Sub
End If
Cursor.Current = Cursors.[Default]
MessageBox.Show("Please type the name of the tv series!")
Exit Sub
End If
End If
If Not Button4.Enabled Then
If String.IsNullOrEmpty(RichTextBox3.Text) Or String.IsNullOrWhiteSpace(RichTextBox3.Text) Or RichTextBox3.Text.Contains("E") Then
If String.IsNullOrEmpty(RichTextBox2.Text) Or String.IsNullOrWhiteSpace(RichTextBox2.Text) Or RichTextBox2.Text.Contains("S") Then
Cursor.Current = Cursors.[Default]
MessageBox.Show("Season and episode numbers missing!")
Exit Sub
End If
Cursor.Current = Cursors.[Default]
MessageBox.Show("Episode number missing!")
Exit Sub
ElseIf String.IsNullOrEmpty(RichTextBox2.Text) Or String.IsNullOrWhiteSpace(RichTextBox2.Text) Or RichTextBox2.Text.Contains("S") Then
Cursor.Current = Cursors.[Default]
MessageBox.Show("Season number missing!")
Exit Sub
End If
End If
If Not HaveInternetConnection() Then
Cursor.Current = Cursors.[Default]
MessageBox.Show("No internet connection!")
Exit Sub
End If
Dim ImdbURL As String = getIMDbUrl(TextFromBox)
If Not ImdbURL.Contains("******") And Not Button3.Enabled Then
ImdbURL = getIMDbUrl(TextFromBox) + " movie"
If Not ImdbURL.Contains("***") And Not Button3.Enabled Then
Cursor.Current = Cursors.[Default]
MessageBox.Show("Movie not found! Please add the year in the name!")
Exit Sub
End If
End If
If Not ImdbURL.Contains("***") And Not Button4.Enabled Then
ImdbURL = getIMDbUrl(TextFromBox) + " TV series"
If Not ImdbURL.Contains("****") And Not Button4.Enabled Then
Cursor.Current = Cursors.[Default]
MessageBox.Show("TV series not found! Please add the year in the name!")
Exit Sub
End If
End If
Dim LinkFinal As String = (GetLink(ImdbURL))
If String.IsNullOrEmpty(LinkFinal) Or String.IsNullOrWhiteSpace(LinkFinal) Then
Cursor.Current = Cursors.[Default]
Exit Sub
End If
Clipboard.SetText(LinkFinal)
MessageBox.Show("Link in clipboard. Paste it into desired browser!")
TextFromBox = String.Empty
StatusLink = "0"
Cursor.Current = Cursors.[Default]
End Sub
Public Function HaveInternetConnection() As Boolean
Dim result As Boolean
Try
result = My.Computer.Network.Ping("www.google.com")
Catch ex As Exception
result = False
End Try
Return result
End Function
Private Function GetIP() As String
Return Client.DownloadString("https://api.ipify.org").ToString()
End Function
Private Function matchAll(ByVal regex As String, ByVal html As String, ByVal Optional i As Integer = 1) As ArrayList
Dim list As ArrayList = New ArrayList()
For Each m As Match In New Regex(regex, RegexOptions.Multiline).Matches(html)
list.Add(m.Groups(i).Value.Trim())
Next
Return list
End Function
Private Function getIMDbUrl(MovieName As String, Optional searchEngine As String = "google") As String
Dim address As String = GoogleSearch + MovieName
If searchEngine.ToLower().Equals("bing") Then
address = BingSearch + MovieName
End If
If searchEngine.ToLower().Equals("ask") Then
address = AskSearch + MovieName
End If
Dim html As String = Client.DownloadString(address)
Dim arrayList As ArrayList = matchAll("<a href=""(*****\d{7}/)"".*?>.*?</a>", html, 1)
If arrayList.Count > 0 Then
Return CStr(arrayList(0))
ElseIf searchEngine.ToLower().Equals("google") Then
Return getIMDbUrl(MovieName, "bing")
ElseIf searchEngine.ToLower().Equals("bing") Then
Return getIMDbUrl(MovieName, "ask")
Else
Return String.Empty
End If
End Function
Private Function GetLink(ImdbURL As String) As Object
Dim ID = ImdbURL.Replace("*****/", "")
ID = ID.Replace("/", "")
Dim result As Object
If StatusLink = "1" Then
Cursor.Current = Cursors.[Default]
Exit Function
ElseIf Not Button4.Enabled Then
Dim SeasonNumber As String = "&s=" + RichTextBox2.Text
Dim EpisodeNumber As String = "&e=" + RichTextBox3.Text
Dim TVSeasonNumber As String = "&tv=1" + SeasonNumber
If Not Client.DownloadString("*****/" & ID).Contains(">Episode Guide<") Then
ImdbURL = getIMDbUrl(TextFromBox + " TV series")
StatusLink = "1"
If String.IsNullOrEmpty(ImdbURL) Or String.IsNullOrWhiteSpace(ImdbURL) Then
Cursor.Current = Cursors.[Default]
MessageBox.Show("TV series not found! Please add the year in the name or check category!")
result = String.Empty
Exit Function
Else
ID = ImdbURL.Replace("*****", "")
ID = ID.Replace("/", "")
result = (GetLink(ImdbURL))
If String.IsNullOrEmpty(result) Or String.IsNullOrWhiteSpace(result) Then
Cursor.Current = Cursors.[Default]
MessageBox.Show("TV series not found! Please add the year in the name or check category!")
result = String.Empty
Exit Function
Else
Return result.ToString()
result = String.Empty
Exit Function
End If
End If
Else
Dim address As String = "******" & ID & TVSeasonNumber & "&ip=" & GetIP()
Dim token As String = Client.DownloadString(address)
result = "*****" & ID & TVSeasonNumber & EpisodeNumber & "&ticket=" & token
Return result.ToString
Exit Function
End If
ElseIf Not Button3.Enabled Then
If Client.DownloadString("****" & ID).Contains(">Episode Guide<") Then
ImdbURL = getIMDbUrl(TextFromBox + " movie",)
StatusLink = "1"
If String.IsNullOrEmpty(ImdbURL) Or String.IsNullOrWhiteSpace(ImdbURL) Then
Cursor.Current = Cursors.[Default]
MessageBox.Show("Movie not found! Please add the year in the name or check category!")
result = String.Empty
Exit Function
Else
ID = ImdbURL.Replace("****", "")
ID = ID.Replace("/", "")
result = (GetLink(ImdbURL))
If String.IsNullOrEmpty(result) Or String.IsNullOrWhiteSpace(result) Then
Cursor.Current = Cursors.[Default]
MessageBox.Show("Movie not found! Please add the year in the name or check category!")
result = String.Empty
Exit Function
Else
Return result.ToString()
result = String.Empty
Exit Function
End If
End If
Else
Dim address2 As String = "****" & ID & "&ip=" & GetIP()
Dim token2 As String = Client.DownloadString(address2)
result = "****" & ID & "&ticket=" & token2
Return result.ToString
result = String.Empty
Exit Function
End If
End If
#Disable Warning BC42105 ' Function doesn't return a value on all code paths
End Function
Private Sub RichTextBox1_MouseClick(sender As Object, e As EventArgs) Handles RichTextBox1.GotFocus
If RichTextBox1.Text.Contains("Type movie or tv series name here") Then RichTextBox1.Text = String.Empty
End Sub
Private Sub RichTextBox2_MouseClick(sender As Object, e As EventArgs) Handles RichTextBox2.GotFocus
If RichTextBox2.Text.Contains("S") Then RichTextBox2.Text = String.Empty
End Sub
Private Sub RichTextBox3_MouseClick(sender As Object, e As EventArgs) Handles RichTextBox3.GotFocus
If RichTextBox3.Text.Contains("E") Then RichTextBox3.Text = String.Empty
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Button3.Enabled = False
Button4.Enabled = True
RichTextBox1.Enabled = True
RichTextBox2.Enabled = False
RichTextBox3.Enabled = False
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Button4.Enabled = False
Button3.Enabled = True
RichTextBox1.Enabled = True
RichTextBox2.Enabled = True
RichTextBox3.Enabled = True
End Sub
Private Sub RichTextBox2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles RichTextBox2.KeyPress
If Char.IsLetter(e.KeyChar) Then
e.Handled = True
End If
If CType(e, KeyPressEventArgs).KeyChar = vbCr Then
Search()
End If
End Sub
Private Sub RichTextBox3_KeyPress(sender As Object, e As KeyPressEventArgs) Handles RichTextBox3.KeyPress
If Char.IsLetter(e.KeyChar) Then
e.Handled = True
End If
If CType(e, KeyPressEventArgs).KeyChar = vbCr Then
Search()
End If
End Sub
Private Sub RichTextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles RichTextBox1.KeyPress
If CType(e, KeyPressEventArgs).KeyChar = vbCr Then
Search()
End If
If Char.IsUpper(e.KeyChar) Then e.KeyChar = Char.ToLower(e.KeyChar)
End Sub
Private Sub RichTextBox1_Lostfocus(sender As Object, e As EventArgs) Handles RichTextBox1.LostFocus
If String.IsNullOrEmpty(RichTextBox1.Text) Or String.IsNullOrWhiteSpace(RichTextBox1.Text) Then
RichTextBox1.Text = "Type movie or tv series name here..."
End If
End Sub
Private Sub RichTextBox2_Lostfocus(sender As Object, e As EventArgs) Handles RichTextBox2.LostFocus
If String.IsNullOrEmpty(RichTextBox2.Text) Or String.IsNullOrWhiteSpace(RichTextBox2.Text) Then
RichTextBox2.Text = "S:"
End If
End Sub
Private Sub RichTextBox3_Lostfocus(sender As Object, e As EventArgs) Handles RichTextBox3.LostFocus
If String.IsNullOrEmpty(RichTextBox3.Text) Or String.IsNullOrWhiteSpace(RichTextBox3.Text) Then
RichTextBox3.Text = "E:"
End If
End Sub
Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
StatusLink = "0"
End Sub
Private Sub RichTextBox2_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox2.TextChanged
StatusLink = "0"
End Sub
Private Sub RichTextBox3_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox3.TextChanged
StatusLink = "0"
End Sub
End Class
all of the ***** are https website addresses.
Tried switching the Net version to 4.7. Did not help. It is also legal in my country to stream (interior minister dropped a bomb on the DMCA here lol) and this is streaming only. The code is not operational so i had not given you anything dangerous if it is illegal in yours.

How to change array in my quiz so that the next question comes up with correct button values/words

For my final coding project. I need my code so that when i press a button to answer a question, the code brings up another question stored in my array and i coded it so that each question comes with its own words for each button
If RichTextBox1.Text = Form1.questions(0) Then
Button1.Text = Form1.answers(0)
Button2.Text = "41-3"
Button3.Text = "7-0"
Button4.Text = "27-2"
ElseIf RichTextBox1.Text = Form1.questions(1) Then
Button1.Text = "Pink"
Button2.Text = "Grey"
Button3.Text = Form1.answers(1)
Button4.Text = "White"
ElseIf RichTextBox1.Text = Form1.questions(x) Then "etc etc etc"
Right now when I press the button the question changes but the buttons remain the same from the first question of the quiz I used this sub in each of the 4 button click's private subs to do it.
Sub ChangeQuestion()
Form1.x = (Form1.rn.Next(0, 19))
RichTextBox1.Text = Form1.questions(Form1.x)
End Sub
My Entire Code:
Public Class Form1
Public StudentName As String
Public questions(19) As String
Public answers(19) As String
Public x As Integer
Public rn As New Random
Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Questions
questions(0) = "What is the biggest win in soccer"
questions(1) = "What colour is Real Madrid's 2017-2018 season away kit"
questions(2) = "Neymar Plays for PSG" 'T/F
questions(3) = "What year did Manchester United sign Robin Van Persie"
questions(4) = "Neymar is number 10 in Brazil" 'T/F
questions(5) = "How much money do Manchester United have to pay for their 2014-15 kits?"
questions(6) = "What club loaned David Beckham twice?"
questions(7) = "Julio Cesar is a Center Back" 'T/F
questions(8) = "How many clubs did Zlatan Ibrihimovic play for by 2014"
questions(9) = "Agger is Liverpool's star defender" 'T/F
questions(10) = "What is the purpose of trapping the ball"
questions(11) = "Which of the following are not tackling variations"
questions(12) = "Each team may have __ players including a goalie"
questions(13) = " To shoot the ball, players should point their toes to the ground and kick the ball with the laces"
questions(14) = "If an offensive player is fouled in the box, the result is?"
questions(15) = "A player is allowed to kick, trip, jump at, charge, strike, push, hold or spit at an opponent" 'T/F
questions(16) = "it is best to pass with which part of the foot?"
questions(17) = "A penalty kick is taken __ yards away from the goal line"
questions(18) = "What basic soccer skill is used when the ball goes out of bounds on the sideline?"
questions(19) = "Can every player use their hands?" 'T/F
'Answers
answers(0) = "31-0"
answers(1) = "Black"
answers(2) = "True"
answers(3) = "2012"
answers(4) = "True"
answers(5) = "75 Million"
answers(6) = "AC Milan"
answers(7) = "False"
answers(8) = "6"
answers(9) = "False"
answers(10) = "To absorb the energy of the ball and control it"
answers(11) = "Forearm tackle "
answers(12) = "11"
answers(13) = "True"
answers(14) = "Penalty"
answers(15) = "False"
answers(16) = "Inside"
answers(17) = "12"
answers(18) = "Throw In"
answers(19) = "False"
txtName.Text = StudentName
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Me.Close()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnStart.Click
x = (rn.Next(0, 19))
Form2.RichTextBox1.Text = questions(x)
Me.Hide()
Form2.Show()
End Sub
Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles Button5.Click
Me.Close()
End Sub
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If RichTextBox1.Text = Form1.questions(0) Then
Button1.Text = Form1.answers(0)
Button2.Text = "41-3"
Button3.Text = "7-0"
Button4.Text = "27-2"
ElseIf RichTextBox1.Text = Form1.questions(1) Then
Button1.Text = "Pink"
Button2.Text = "Grey"
Button3.Text = Form1.answers(1)
Button4.Text = "White"
ElseIf RichTextBox1.Text = Form1.questions(2) Then
Button1.Text = Form1.answers(2)
Button2.Text = "False"
Button3.Text = ""
Button4.Text = ""
ElseIf RichTextBox1.Text = Form1.questions(3) Then
Button1.Text = "2009"
Button2.Text = "2010"
Button3.Text = "2011"
Button4.Text = Form1.answers(3)
ElseIf RichTextBox1.Text = Form1.questions(4) Then
Button1.Text = "False"
Button2.Text = Form1.answers(4)
Button3.Text = ""
Button4.Text = ""
ElseIf RichTextBox1.Text = Form1.questions(5) Then
Button1.Text = "73 Million"
Button2.Text = "74 Million"
Button3.Text = Form1.answers(5)
Button4.Text = "76 Million"
ElseIf RichTextBox1.Text = Form1.questions(6) Then
Button1.Text = Form1.answers(6)
Button2.Text = "Liverpool"
Button3.Text = "Arsenal"
Button4.Text = "England"
ElseIf RichTextBox1.Text = Form1.questions(7) Then
Button1.Text = Form1.answers(7)
Button2.Text = "True"
Button3.Text = ""
Button4.Text = ""
ElseIf RichTextBox1.Text = Form1.questions(8) Then
Button1.Text = "3"
Button2.Text = "4"
Button3.Text = "5"
Button4.Text = Form1.answers(8)
ElseIf RichTextBox1.Text = Form1.questions(9) Then
Button1.Text = Form1.answers(9)
Button2.Text = "True"
Button3.Text = ""
Button4.Text = ""
ElseIf RichTextBox1.Text = Form1.questions(10) Then
Button1.Text = "To stop the ball"
Button2.Text = "To prepare for a throw-in"
Button3.Text = Form1.answers(10)
Button4.Text = "To hide the ball"
ElseIf RichTextBox1.Text = Form1.questions(11) Then
Button1.Text = "Block tackle"
Button2.Text = Form1.answers(11)
Button3.Text = "Slide tackle"
Button4.Text = "Poke tackle"
ElseIf RichTextBox1.Text = Form1.questions(12) Then
Button1.Text = "9"
Button2.Text = "12"
Button3.Text = "10"
Button4.Text = Form1.answers(12)
ElseIf RichTextBox1.Text = Form1.questions(13) Then
Button1.Text = Form1.answers(13)
Button2.Text = "False"
Button3.Text = ""
Button4.Text = ""
ElseIf RichTextBox1.Text = Form1.questions(14) Then
Button1.Text = Form1.answers(14)
Button2.Text = "Free-Kick"
Button3.Text = "Throw in"
Button4.Text = "Go"
ElseIf RichTextBox1.Text = Form1.questions(15) Then
Button1.Text = "True"
Button2.Text = Form1.answers(15)
Button3.Text = ""
Button4.Text = ""
ElseIf RichTextBox1.Text = Form1.questions(16) Then
Button1.Text = "Toes"
Button2.Text = "Outside"
Button3.Text = Form1.answers(16)
Button4.Text = "Heel"
ElseIf RichTextBox1.Text = Form1.questions(17) Then
Button1.Text = "13"
Button2.Text = "15"
Button3.Text = "11"
Button4.Text = Form1.answers(17)
ElseIf RichTextBox1.Text = Form1.questions(18) Then
Button1.Text = Form1.answers(18)
Button2.Text = "Free Kick"
Button3.Text = "Goal Kick"
Button4.Text = "Penalty"
ElseIf RichTextBox1.Text = Form1.questions(19) Then
Button1.Text = Form1.answers(19)
Button2.Text = "True"
Button3.Text = ""
Button4.Text = ""
End If
End Sub
Private Sub Bruh_Click(sender As Object, e As EventArgs) Handles Bruh.Click
Me.Close()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
ChangeQuestion()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ChangeQuestion()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ChangeQuestion()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
ChangeQuestion()
End Sub
Sub ChangeQuestion()
Form1.x = (Form1.rn.Next(0, 19))
RichTextBox1.Text = Form1.questions(Form1.x)
End Sub
End Class
You are only setting up the answers in Form2.Load You need to move that code to a separate Sub and then call it from the Form2.Load and each of the answer buttons.
Private Sub FillAnswers()
If RichTextBox1.Text = Form1.questions(0) Then
Button1.Text = Form1.answers(0)
Button2.Text = "41-3"
Button3.Text = "7-0"
Button4.Text = "27-2"
ElseIf RichTextBox1.Text = Form1.questions(1) Then
Button1.Text = "Pink"
Button2.Text = "Grey"
Button3.Text = Form1.answers(1)
Button4.Text = "White"
'etc...
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FillAnswers()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
ChangeQuestion()
FillAnswers()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ChangeQuestion()
FillAnswers()
End Sub
'etc...
Although this should answer your question you have a few other problems to think about.
How to keep score
How to prevent duplicate questions.

Counter using KeyPress events

First time posting here, although I'm a frequent visitor when I'm looking for answers. I am new to VB and programming.
My problem is this. I had this figured out in VBA, but I want to convert my "program" to a standalone executable with VB (using Visual Studio 2015)
I want to keep track of certain keypresses done on a textbox, and so far I figured out something that works, but it seems messy.
Can anyone think of a better way of doing
Public Class Form1
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles TextBox1.KeyPress
Select Case UCase(e.KeyChar) 'For capturing keypresses and adding them
Case "W" : Label3.Text = Val(Label3.Text) + 1
Case "R" : Label4.Text = Val(Label4.Text) + 1
End Select
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
TextBox1.Text = "" 'Clears the text box after each keypress
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim WcountA As Integer
Dim RcountA As Integer
Dim WcountB As Integer
Dim RcountB As Integer
Dim Wavg As Single
Dim Ravg As Single
Select Case Button1.Text
Case "Finish A" 'Finishes first count and stores results in labels
WcountA = Convert.ToInt32(Label3.Text)
RcountA = Convert.ToInt32(Label4.Text)
Button1.Text = "Finish B"
Label5.Text = WcountA
Label7.Text = RcountA
TextBox1.Focus()
Label3.Text = ""
Label4.Text = ""
Case "Finish B" 'Finishes second count and stores in labels
WcountB = Convert.ToInt32(Label3.Text)
RcountB = Convert.ToInt32(Label4.Text)
With Button1
.Text = "Finished"
.Enabled = False
End With
Label6.Text = WcountB
Label8.Text = RcountB
Label3.Text = ""
Label4.Text = ""
WcountA = Label5.Text
RcountA = Label7.Text
WcountB = Label6.Text
RcountB = Label8.Text
Wavg = (WcountA + WcountB) / 2
Ravg = (RcountA + RcountB) / 2
MsgBox("W average = " & Wavg & vbNewLine & "R average = " & Ravg)
End Select
End Sub
End Class
On the form I have the textbox that logs the keypress event, labels that increase by 1 with each specific keypess ("W" and "R"), a button that changes its function with each click(finish first count, finish second count) and some labels I had to use to store the first and second count for the final calculation.
Any suggestion will be appreciated.
Thanks in advance!
First of al, you need to take out the counters of the key pres handler like so;
Because they are gone every time the Button1_Click sub ends.
Public Class Form1
Dim WcountA As Integer
Dim RcountA As Integer
Dim WcountB As Integer
Dim RcountB As Integer
Dim Wavg As Single
Dim Ravg As Single
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles TextBox1.KeyPress
Select Case UCase(e.KeyChar) 'For capturing keypresses and adding them
Case "W" : Label3.Text = Val(Label3.Text) + 1
Case "R" : Label4.Text = Val(Label4.Text) + 1
End Select
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
TextBox1.Text = "" 'Clears the text box after each keypress
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Select Case Button1.Text
Case "Finish A" 'Finishes first count and stores results in labels
WcountA = Convert.ToInt32(Label3.Text)
RcountA = Convert.ToInt32(Label4.Text)
Button1.Text = "Finish B"
Label5.Text = WcountA
Label7.Text = RcountA
TextBox1.Focus()
Label3.Text = ""
Label4.Text = ""
Case "Finish B" 'Finishes second count and stores in labels
WcountB = Convert.ToInt32(Label3.Text)
RcountB = Convert.ToInt32(Label4.Text)
With Button1
.Text = "Finished"
.Enabled = False
End With
Label6.Text = WcountB
Label8.Text = RcountB
Label3.Text = ""
Label4.Text = ""
WcountA = Label5.Text
RcountA = Label7.Text
WcountB = Label6.Text
RcountB = Label8.Text
Wavg = (WcountA + WcountB) / 2
Ravg = (RcountA + RcountB) / 2
MsgBox("W average = " & Wavg & vbNewLine & "R average = " & Ravg)
End Select
End Sub
End Class
This is what I ended up doing, works better and looks cleaner. Thanks Lectere for pointing me to the right path!
Public Class Form1
Dim WcountA As Integer
Dim RcountA As Integer
Dim WcountB As Integer
Dim RcountB As Integer
Dim Wavg As Single
Dim Ravg As Single
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles TextBox1.KeyPress
Select Case UCase(e.KeyChar) 'For capturing keypresses and adding them
Case "W" : lblWcount.Text = Val(lblWcount.Text) + 1
Case "R" : lblRcount.Text = Val(lblRcount.Text) + 1
Case Convert.ToChar(13) : Button1_Click(sender, e)
End Select
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
TextBox1.Text = "" 'Clears the text box after each keypress
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Select Case Button1.Text
Case "Finish A" 'Finishes first count and stores results in variables
If lblWcount.Text = vbNullString Then 'checks for empty values and treats them as 0
WcountA = 0
Else
WcountA = lblWcount.Text
End If
If lblRcount.Text = vbNullString Then 'checks for empty values and treats them as 0
RcountA = 0
Else
RcountA = lblRcount.Text
End If
Button1.Text = "Finish B"
lbl_1.Text = WcountA
lbl_2.Text = RcountA
TextBox1.Focus()
lblWcount.Text = vbNullString
lblRcount.Text = vbNullString
lblcount.Text = "Count B"
Case "Finish B" 'Finishes second count and stores results in variables
lblcount.Text = vbNullString
If lblWcount.Text = vbNullString Then
WcountB = 0
Else
WcountB = lblWcount.Text
End If
If lblRcount.Text = vbNullString Then
RcountB = 0
Else
RcountB = lblRcount.Text
End If
With Button1
.Text = "Reset"
End With
lbl_3.Text = WcountB
lbl_4.Text = RcountB
lblWcount.Text = vbNullString
lblRcount.Text = vbNullString
Wavg = (WcountA + WcountB) / 2
Ravg = (RcountA + RcountB) / 2
MsgBox("W average = " & Wavg & vbNewLine & "R average = " & Ravg)
Button1.Focus()
Case "Reset" 'Resets values
Dim i As Integer
For i = 1 To 4 'clears labels that start with lbl_ (1 through 4)
Dim myLabel As Label = CType(Controls("lbl_" & i), Label)
myLabel.Text = vbNullString
Next
Initial() ' calls initial form state
End Select
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Initial() 'calls initial form state at load up
End Sub
Private Sub Initial() 'initial state sub
lblcount.Text = "Count A"
TextBox1.Focus()
Button1.Text = "Finish A"
End Sub
End Class
I managed to use loops for clearing labels, and also made pressing Enter during the KeyPress event be treated as a button click.
Loving this feeling of accomplishment when I finally figure something out on something new to me! Love pages like this one where one can learn so much!

Hangman Game: How To Make The Program Know I Wrote The Correct Word?

I'm making a hangman game in VB.Net and everything's going smooth except one thing.
I have a problem. Let me first explain.
You start the game, you press the button so it can generate a random word in form of "?" Then you press the keys built in the program. When you press a correct key, the "?" turns into the key you pressed. Yeah you know how hangman works.
Anyway, when i've guessed all the keys correct, i want it to tell me it was correct and randomly generate next word without pressing the "generate button"
Here's the code i wrote:
If Korrekt_Gissat = antal Then
MsgBox("Nice, " & HemligaOrdet & " is correct.")
Reset()
End If
Full code:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim i As Integer
i = 0
'Tangentbordet
For Each ctrl In Controls
If ctrl.tag = "Tangentbordet" Then
ctrl.text() = Chr(90 - i)
i = i + 1
End If
Next
'Slumporden
Ordlistan(0) = "Zed"
Ordlistan(1) = "Ahri"
Ordlistan(2) = "Udyr"
Ordlistan(3) = "Ekko"
Ordlistan(4) = "Shen"
Ordlistan(5) = "Jinx"
Ordlistan(6) = "Draven"
Ordlistan(7) = "Thresh"
Ordlistan(8) = "Yasuo"
Ordlistan(9) = "Kassadin"
End Sub
Private Sub cmdSlumpa_Ord()
Dim slumptal As Integer
'Slumpa orden till textrutan
slumptal = Int((10 * Rnd()) + 1)
HemligaOrdet = Ordlistan(slumptal)
' TextBoxSlump.Text = HemligaOrdetu
If HemligaOrdet = "Zed" Then
PictureChampions.Image = My.Resources.Zed
End If
If HemligaOrdet = "Ahri" Then
PictureChampions.Image = My.Resources.ahri
End If
If HemligaOrdet = "Udyr" Then
PictureChampions.Image = My.Resources.Udyr
End If
If HemligaOrdet = "Ekko" Then
PictureChampions.Image = My.Resources.ekko
End If
If HemligaOrdet = "Shen" Then
PictureChampions.Image = My.Resources.shen
End If
If HemligaOrdet = "Jinx" Then
PictureChampions.Image = My.Resources.Jinx
End If
If HemligaOrdet = "Draven" Then
PictureChampions.Image = My.Resources.Draven
End If
If HemligaOrdet = "Thresh" Then
PictureChampions.Image = My.Resources.Thresh
End If
If HemligaOrdet = "Yasuo" Then
PictureChampions.Image = My.Resources.yasuo
End If
If HemligaOrdet = "Kassadin" Then
PictureChampions.Image = My.Resources.Kassadin
End If
Skapamall()
End Sub
Private Sub btnSlumpa_Click(sender As Object, e As EventArgs) Handles btnSlumpa.Click
cmdSlumpa_Ord()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click, MyBase.Click, Button9.Click, Button8.Click, Button7.Click, Button6.Click, Button5.Click, Button4.Click, Button3.Click, Button29.Click, Button28.Click, Button27.Click, Button26.Click, Button25.Click, Button24.Click, Button23.Click, Button22.Click, Button21.Click, Button20.Click, Button2.Click, Button19.Click, Button18.Click, Button17.Click, Button16.Click, Button15.Click, Button14.Click, Button13.Click, Button12.Click, Button11.Click, Button10.Click
Dim tecken As String
Korrekt_Gissat = False
For position = 1 To HemligaOrdet.Length
tecken = Mid(HemligaOrdet, position, 1)
If UCase(tecken) = sender.text Then
Korrekt_Gissat = True
Mid(Mallen, position, 1) = tecken
TextBox2.Text = Mallen
End If
Next
If Korrekt_Gissat = True Then
Dim btn As Button = DirectCast(sender, Button)
btn.Enabled = False
End If
If Korrekt_Gissat = False Then
AntalFel = AntalFel + 1
Bestraffa()
End If
If Korrekt_Gissat = antal Then
MsgBox("Nice, " & HemligaOrdet & " is correct.")
Reset()
End If
If AntalFel = 8 Then
MsgBox("You lost!")
End
End If
End Sub
Private Sub Bestraffa()
If AntalFel = 1 Then
PicturePenality.Image = My.Resources._1
End If
If AntalFel = 2 Then
PicturePenality.Image = My.Resources._2
End If
If AntalFel = 3 Then
PicturePenality.Image = My.Resources._3
End If
If AntalFel = 4 Then
PicturePenality.Image = My.Resources._4
End If
If AntalFel = 5 Then
PicturePenality.Image = My.Resources._5
End If
If AntalFel = 6 Then
PicturePenality.Image = My.Resources._6
End If
If AntalFel = 7 Then
PicturePenality.Image = My.Resources._7
End If
If AntalFel = 8 Then
PicturePenality.Image = My.Resources._8
End If
End Sub
Private Sub Skapamall()
Mallen = ""
antal = HemligaOrdet.Length
For i = 1 To antal
Mallen = Mallen + "?"
Next i
TextBox2.Text = Mallen
End Sub
End Class
Module:
Module Module1
Public Ordlistan(0 To 9) As String
Public HemligaOrdet As String
Public Mallen As String
Public antal As Integer
Public Korrekt_Gissat As Boolean
Public AntalFel As Integer
End Module
If Korrekt_Gissat = antal Then
This is comparing a Boolean (Korret_Gissat) with an Integer (antal). This is usually a bad idea. Instead, check for a win in the code that checks for a correct guess. (No one could ever win on a turn with an incorrect guess, right?)
If Korrekt_Gissat = True Then
Dim btn As Button = DirectCast(sender, Button)
btn.Enabled = False
If InStr(Mallen, "?") = 0 Then
MsgBox("Congratulations, you won!")
'any code you want to call to end or reset the game
End If
End If
Edit: Alternate Method
Per your request below, here is another way of checking for a win. This method uses a designated function to perform the check and a For loop instead of InStr.
First, create the new function within Module1:
Public Function CheckForWin() As Boolean
For i As Integer = 0 To Mallen.Length -1
If Mid(Mallen, i, 1) = "?" Then
Return False
End If
Next
'This point can only be reached if no instance of "?" was found
Return True
End Function
Next, call the function from your code (where in the first version I used InStr).
If Korrekt_Gissat = True Then
Dim btn As Button = DirectCast(sender, Button)
btn.Enabled = False
If CheckForWin() Then
MsgBox("Congratulations, you won!")
'any code you want to call to end or reset the game
End If
End If

not accessible in this context because it is 'Private' [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I'm writing some code for a calculator and I keep getting this error. I have the math functions in another class but the variables from form1 are not accessible. Below is my code.
I've also tried changing my Dim variables to public but that also doesn't work.
Public Class Form1
'create a value to keep track of whether the calculation is complete so the calculator can revert to zero for new calculations
Dim state As Integer
'create values to store information on numbers and signs entered as well as the answer returned
Dim one As Double
Dim two As Double
Dim ans As Double
Dim sign As Char
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'when "Return" or "Enter" Key is selected button 15 is clicked (seems to only work when textbox selected????)
Me.AcceptButton = Button15
'change the title of the form
Me.Text = "Simple Calculator"
'label the buttons logically
Button1.Text = "1"
Button2.Text = "2"
Button3.Text = "3"
Button4.Text = "4"
Button5.Text = "5"
Button6.Text = "6"
Button7.Text = "7"
Button8.Text = "8"
Button9.Text = "9"
Button10.Text = "0"
Button11.Text = "+"
Button12.Text = "-"
Button13.Text = "X"
Button14.Text = "/"
Button15.Text = "Calc"
Button16.Text = "About Me"
Button17.Text = "Clear"
'allows form to revcieve key events
Me.KeyPreview = True
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'create action when button 1 is clicked
'if state is 1 then previous calculation was solved, then clear textbox to allow for new input
If state = 1 Then
TextBox1.Text = ""
'insert 1 into textbox
Dim Int1 As Integer = 1
TextBox1.Text = TextBox1.Text & Int1
'return state of calculator to zero to designate that calculation has NOT been solved
state = 0
Else
'else insert 1 into textbox
Dim Int1 As Integer = 1
TextBox1.Text = TextBox1.Text & Int1
End If
End Sub
' the above function for each numbered button
Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If state = 1 Then
TextBox1.Text = ""
Dim Int2 As Integer = 2
TextBox1.Text = TextBox1.Text & Int2
state = 0
Else
Dim Int2 As Integer = 2
TextBox1.Text = TextBox1.Text & Int2
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
If state = 1 Then
TextBox1.Text = ""
Dim Int3 As Integer = 3
TextBox1.Text = TextBox1.Text & Int3
state = 0
Else
Dim Int3 As Integer = 3
TextBox1.Text = TextBox1.Text & Int3
End If
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
If state = 1 Then
TextBox1.Text = ""
Dim Int4 As Integer = 4
TextBox1.Text = TextBox1.Text & Int4
state = 0
Else
Dim Int4 As Integer = 4
TextBox1.Text = TextBox1.Text & Int4
End If
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
If state = 1 Then
TextBox1.Text = ""
Dim Int5 As Integer = 5
TextBox1.Text = TextBox1.Text & Int5
state = 0
Else
Dim Int5 As Integer = 5
TextBox1.Text = TextBox1.Text & Int5
End If
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
If state = 1 Then
TextBox1.Text = ""
Dim Int6 As Integer = 6
TextBox1.Text = TextBox1.Text & Int6
state = 0
Else
Dim Int6 As Integer = 6
TextBox1.Text = TextBox1.Text & Int6
End If
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
If state = 1 Then
TextBox1.Text = ""
Dim Int7 As Integer = 7
TextBox1.Text = TextBox1.Text & Int7
state = 0
Else
Dim Int7 As Integer = 7
TextBox1.Text = TextBox1.Text & Int7
End If
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
If state = 1 Then
TextBox1.Text = ""
Dim Int8 As Integer = 8
TextBox1.Text = TextBox1.Text & Int8
state = 0
Else
Dim Int8 As Integer = 8
TextBox1.Text = TextBox1.Text & Int8
End If
End Sub
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
If state = 1 Then
TextBox1.Text = ""
Dim Int9 As Integer = 9
TextBox1.Text = TextBox1.Text & Int9
state = 0
Else
Dim Int9 As Integer = 9
TextBox1.Text = TextBox1.Text & Int9
End If
End Sub
Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
If state = 1 Then
TextBox1.Text = ""
Dim Int0 As Integer = 0
TextBox1.Text = TextBox1.Text & Int0
state = 0
Else
Dim Int0 As Integer = 0
TextBox1.Text = TextBox1.Text & Int0
End If
End Sub
Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
'create an action for when addition button is clicked
Try
'when button is clicked dim sign and text in textbox
sign = "+"
one = TextBox1.Text
TextBox1.Text = ""
Catch ex As Exception
TextBox1.Text = "Error" ' if error occurs return error in textbox
state = 1 'if error occurs return state to 1 to allow for new calculation
End Try
End Sub
'repeat the above action for remainder of arithmic functions
Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
Try
'when button is clicked dim sign and text in textbox
sign = "-"
one = TextBox1.Text
TextBox1.Text = ""
Catch ex As Exception
TextBox1.Text = "Error" ' if error occurs return error in textbox
state = 1 'if error occurs return state to 1 to allow for new calculation
End Try
End Sub
Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
Try
sign = "*"
one = TextBox1.Text
TextBox1.Text = ""
Catch ex As Exception
TextBox1.Text = "Error" ' if error occurs return error in textbox
state = 1 'if error occurs return state to 1 to allow for new calculation
End Try
End Sub
Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click
Try
sign = "/"
one = TextBox1.Text
TextBox1.Text = ""
Catch ex As Exception
TextBox1.Text = "Error"
state = 1 'if error occurs return state to 1 to allow for new calculation
End Try
End Sub
Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
'run functions based on sign stored during calculation
Try
two = TextBox1.Text
If sign = "+" Then
Calculator2.Math.add()
ElseIf sign = "-" Then
Calculator2.Math.minus()
ElseIf sign = "*" Then
Calculator2.Math.multiply()
ElseIf sign = "/" Then
Calculator2.Math.divide()
End If
'if user attempts to divide by zero return divide by zero error in textbox
If TextBox1.Text = "Infinity" Then
TextBox1.Text = "Divide by Zero Error"
state = 1 'if error occurs return state to 1 to allow for new calculation
End If
Catch ex As Exception
TextBox1.Text = "Error"
state = 1 'if error occurs return state to 1 to allow for new calculation
End Try
End Sub
Private Sub Button16_Click(sender As Object, e As EventArgs) Handles Button16.Click
'create message box that provides information about author
Dim msg = "Student Name: Emily Wong" & vbCrLf & "Student Number: 0692740" ' Define the message you want to see inside the message box.
Dim title = "About Me" ' Define a title for the message box.
Dim style = MsgBoxStyle.OkOnly ' make an ok button for the msg box
Dim response = MsgBox(msg, style, title)
End Sub
Private Sub Button17_Click(sender As Object, e As EventArgs) Handles Button17.Click
'create a clear button to clear textboxes when clicked and reset state of calculator
TextBox1.Text = ""
state = 0
End Sub
And this is my other class
Public Class Math
Inherits Form1
'create funtions for add,sub, multiply, and divide features
Sub add()
ans = one + two 'creates calculation of the entered numbers
TextBox1.Text = ans 'returns answer into the textbox
state = 1 'returns state to 1 to show that calculation has occured
End Sub
Sub minus()
ans = one - two
TextBox1.Text = ans
state = 1
End Sub
Sub multiply()
ans = one * two
TextBox1.Text = ans
state = 1
End Sub
Sub divide()
ans = one / two
TextBox1.Text = ans
state = 1
End Sub
End Class
state is a private variable in Form1. You can't access from code outside of Form1. Your Math class cannot reference state. Remove the calls to state from the Math class code. An outside class should not be changing the state of another object in anycase.
Try this instead:
Public Class accounting
Dim Operand1 As Double
Dim Operand2 As Double
Dim [Operator] As String
These are the button from 1 - 0
Private Sub Button6_Click_1(sender As Object, e As EventArgs) Handles Button9.Click, Button8.Click, Button7.Click, Button6.Click, Button5.Click, Button4.Click, Button19.Click, Button12.Click, Button11.Click, Button10.Click
txtans.Text = txtans.Text & sender.text
End Sub
This button is for period/point
Private Sub Button18_Click(sender As Object, e As EventArgs) Handles Button18.Click
If InStr(txtans.Text, ".") > 0 Then
Exit Sub
Else
txtans.Text = txtans.Text & "."
End If
End Sub
This button is for clear or "C" in calculator
Private Sub Button20_Click(sender As Object, e As EventArgs) Handles Button20.Click
txtans.Text = ""
End Sub
These are for add,subtract,divide, and multiply
Private Sub Buttonadd_Click(sender As Object, e As EventArgs) Handles Button13.Click
Operand1 = Val(txtans.Text)
txtans.Text = ""
txtans.Focus()
[Operator] = "+"
End Sub
Private Sub Buttondivide_Click(sender As Object, e As EventArgs) Handles Button15.Click
Operand1 = Val(txtans.Text)
txtans.Text = ""
txtans.Focus()
[Operator] = "-"
End Sub
Private Sub Buttonsubtract_Click(sender As Object, e As EventArgs) Handles Button16.Click
Operand1 = Val(txtans.Text)
txtans.Text = ""
txtans.Focus()
[Operator] = "*"
End Sub
Private Sub Buttonmultiply_Click(sender As Object, e As EventArgs) Handles Button14.Click
Operand1 = Val(txtans.Text)
txtans.Text = ""
txtans.Focus()
[Operator] = "/"
End Sub
This button is for "=" sign
Private Sub Buttoneequal_Click(sender As Object, e As EventArgs) Handles Button17.Click
Dim Result As Double
Operand2 = Val(txtans.Text)
'If [Operator] = "+" Then
' Result = Operand1 + Operand2
'ElseIf [Operator] = "-" Then
' Result = Operand1 - Operand2
'ElseIf [Operator] = "/" Then
' Result = Operand1 / Operand2
'ElseIf [Operator] = "*" Then
' Result = Operand1 * Operand2
'End If
Select Case [Operator]
Case "+"
Result = Operand1 + Operand2
txtans.Text = Result.ToString("#,###.00")
Case "-"
Result = Operand1 - Operand2
txtans.Text = Result.ToString("#,###.00")
Case "/"
Result = Operand1 / Operand2
txtans.Text = Result.ToString("#,###.00")
Case "*"
Result = Operand1 * Operand2
txtans.Text = Result.ToString("#,###.00")
End Select
txtans.Text = Result.ToString("#,###.00")
End Sub
This button is for backspace effect.
Private Sub Buttondel_Click(sender As Object, e As EventArgs) Handles Button21.Click
If txtans.Text < " " Then
txtans.Text = Mid(txtans.Text, 1, Len(txtans.Text) - 1 + 1)
Else
txtans.Text = Mid(txtans.Text, 1, Len(txtans.Text) - 1)
End If
End Sub
Note that "txtans.text" is the textbox where the user inputs and where the output shows.