Issue with some Visual Basic programming code - vb.net

Basically I have to design a fruit game where a random fruit will be generated and the child has to guess the fruit type. If the child guesses it right, they will receive a message saying congrats and if they get it wrong, they will receive a message saying "try it again".
I have done the programming but I don't know where I am going wrong when I type the name of the fruit. Because even though it is right, it gives a message saying it is wrong.
I have the program code for when the fruit is right and the message with it.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Button1.Visible = True ' The tools that will need to be hide when the check button is cliked
Button2.Visible = False
PictureBox1.Visible = False
TextBox1.Visible = False
If Label1.Text = "1" And TextBox2.Text = "Banana" Then ' If both are true then the following result is true
PictureBox2.Image = My.Resources.Well_done 'my.resources.name 'The well_done picture that appears for the correct
PictureBox2.Visible = True '- answer and at the same time it has to be visble
Label2.Text = "Congrats! " & TextBox2.Text & "! Correct answer!" 'The msg which appears for the correct answer
Label2.Visible = True
Me.BackColor = Color.Yellow ' The background colour of the form
ElseIf Label1.Text = "2" And TextBox1.Text = "apple" Then 'Similary for apple banana and other fruits
PictureBox2.Image = My.Resources.Well_done
PictureBox2.Visible = True
Label2.Text = "Congrats " & TextBox2.Text & "! Correct answer!"
Label2.Visible = True
Me.BackColor = Color.Green
ElseIf Label1.Text = "3" And TextBox1.Text = "orange" Then
PictureBox2.Image = My.Resources.Well_done
PictureBox2.Visible = True
Label2.Text = "Congrats " & TextBox2.Text & "! Correct answer!"
Label2.Visible = True
Me.BackColor = Color.Orange
ElseIf Label1.Text = "4" And TextBox1.Text = "Strawberry" Then
PictureBox2.Image = My.Resources.Well_done
PictureBox2.Visible = True
Label2.Text = "Congrats " & TextBox2.Text & "! Correct answer!"
Label2.Visible = True
Me.BackColor = Color.IndianRed
ElseIf Label1.Text = "5" And TextBox1.Text = "Grapes" Then
PictureBox2.Image = My.Resources.Well_done
PictureBox2.Visible = True
Label2.Text = "Congrats " & TextBox2.Text & "! Correct answer!"
Label2.Visible = True
Me.BackColor = Color.Green

The answer to your question is probably because of a casing issue. Try using ToLower() and make sure your strings are lower case too.
As a side note though, you could write this a lot more efficient if you created an abstract class called Fruit and then derive the different types of fruit (apple, strawberry, etc.) from that class. Then you could just create an abstract method called ToString() and compare the input to the ToString() method. This will save you from having to have a crap ton of "If"'s in your code.

Just to clarify what icemanind said, you want to remove any case issues with your string. I can see that you have Strawberry (capital S) and orange (no capital) which means you haven't set a real string structure. Remember that when you check to see if the strings match, that they have to type it in exactly as you test it.
So you use the .toUpper or .toLower. This will convert the text from the textbox to either lower or uppercase - then the string you test should be in all lower of uppercase (lower being easier i guess.)
So each line should be more like...
If Label1.Text = "1" Andalso TextBox2.Text.ToLower = "banana" Then
Now you can type BAnaNa into the textbox, and it will be tested as banana. Also notice i've used the Andalso - its good for future use. Andalso should be used in these kinds of tests, because it only checks the 2nd condition if the first is valid. And checks both, even if the 1st is false. Not a big thing, but good practice and better performance.

Related

Databinding a check box

I have a check box that I binded with a dataset in its properties on my main form. Then on a separate form I have a datagrid that would be the options for each checkbox for a client. The datagrid is good when I look at it. but when I search for a client in the main form sometimes my checkboxes(some of them) will be selected for no reason. I have no coding for this as its all done in the properties except my search button.
Private Sub CmdSearch_Click(sender As Object, e As EventArgs) Handles CmdSearch.Click
Dim iReturn As String
Dim ClientFound As Boolean
Looking = True
ClientFound = False
Place = ClientBindingSource.Position
If Len(TxtSearch.Text) = 6 Then
ClientBindingSource.MoveFirst()
Do Until ClientFound = True
With ClientBindingSource
If Trim(TxtClient.Text) = TxtSearch.Text Then
If TxtSearch.Text = Trim(TxtClient.Text) Then
ClientFound = True
Else
ClientFound = False
End If
Place = ClientBindingSource.Position
Looking = False
Call LRSearch()
ClientBindingSource.Position = Place
Exit Sub
ElseIf Trim(TxtClient.Text) = "ZZTEST" Then
iReturn = MsgBox(TxtSearch.Text & " Not Found", vbOKOnly)
ClientBindingSource.MoveFirst()
Looking = False
Exit Sub
Else
.MoveNext()
End If
End With
Loop
Else
iReturn = MsgBox("Please check your client code for errors " & TxtSearch.Text, vbOKOnly)
TxtSearch.Text = ""
End If
Looking = False
End Sub
I am not sure why the check boxes on my main form have a mind of their own?

How to implement multiple catches with error provider if textbox is empty

I have a program that writes txtboxes to a .txt file, I don't want to be able to "Generate Log Book Entry" until all text boxes are filled in and all check boxes are checked. I have the error provider to pop up correctly, but I need it implemented in the code so that it works the same way as the check boxes (All checkboxes must be checked before you're able to generate log book entry).
'Aircraft make/model textbox cannot be blank
If Me.TextBox4.Text = "" Then
ErrorProvider1.SetError(TextBox4, "Cannot be blank")
Else
Me.ErrorProvider1.SetError(Me.TextBox4, "")
End If
'N-number textbox cannot be blank
If Me.TextBox3.Text = "" Then
ErrorProvider1.SetError(TextBox3, "Cannot be blank")
Else
Me.ErrorProvider1.SetError(Me.TextBox3, "")
End If
'If all checkboxes are checked then no error is shown; if a single checkbox is not check, errorprovider shown
If CheckBox1.Checked And CheckBox2.Checked And CheckBox3.Checked And CheckBox4.Checked And CheckBox5.Checked And CheckBox6.Checked And CheckBox7.Checked Then
ErrorProvider1.SetError(Button2, "")
Dim FILE_NAME As String = "C:\Users\Blake\Documents\test2.txt" 'Sends information to test2.txt
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
objWriter.Write("Aircraft Make & Model: " & TextBox4.Text & ", N-number: " & TextBox3.Text)
objWriter.Write("
Gascolator Removed & Inspected in accordance with FAA approved maintenance manual")
objWriter.Write("
Signature _____________________________")
objWriter.Write(" " & DateTime.Now) 'Displays current date and time
objWriter.Close()
MsgBox("Text Written To File")
Else
MsgBox("File Does Not Exist")
End If
Else ErrorProvider1.SetError(Button2, "All boxes must be checked")
End If
I have tried moving
Me.ErrorProvider1.SetError(Me.TextBox4, "")
End If
but it doesn't allow more than one "If" statement in that section of code.enter image description here
Doesn't allow text to be written to file bc/ txtboxes are not checked
Blank boxes able to write in .txt file
Entered info with .txtfile
Try restructuring your code like this
If Me.TextBox4.Text = "" Then
ErrorProvider1.SetError(TextBox4, "Cannot be blank")
Else
Me.ErrorProvider1.SetError(Me.TextBox4, "")
End If
If Me.TextBox3.Text = "" Then
ErrorProvider1.SetError(TextBox3, "Cannot be blank")
Else
Me.ErrorProvider1.SetError(Me.TextBox3, "")
'Add this line here
validateCheckBoxes()
End If
Sub validateCheckBoxes()
'Move this part here
If CheckBox1.Checked And CheckBox2.Checked And CheckBox3.Checked And CheckBox4.Checked And CheckBox5.Checked And CheckBox6.Checked And CheckBox7.Checked Then
ErrorProvider1.SetError(Button2, "")
Dim FILE_NAME As String = "C:\Users\Blake\Documents\test2.txt" 'Sends information to test2.txt
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
objWriter.Write("Aircraft Make & Model: " & TextBox4.Text & ", N-number: " & TextBox3.Text)
objWriter.Write("Gascolator Removed & Inspected in accordance with FAA approved maintenance manual")
objWriter.Write("Signature _____________________________")
objWriter.Write(" " & DateTime.Now)
'Displays current date and time
objWriter.Close()
MsgBox("Text Written To File")
Else
MsgBox("File Does Not Exist")
End If
Else
ErrorProvider1.SetError(Button2, "All boxes must be checked")
End If
End Sub
While the way you did this will eventually work, I would suggest another approach.
There is like a bazillion ways to do this. I would simply disable the "Generate Log" button until the user has checked and filled everything. This would prevent most problems you mentioned, but you wouldn't have the nice error messages I read through your code, so if they are important you won't be able to work this way.
Here's a code snippet that would do just what I just spoke about:
'Check if the user is checking boxes
Private Sub Checkboxes_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged, CheckBox3.CheckedChanged, CheckBox4.CheckedChanged, CheckBox5.CheckedChanged, CheckBox6.CheckedChanged, CheckBox7.CheckedChanged
UpdateGenerateLogButtonEnabled()
End Sub
'Check if the user is writing text
Private Sub TextBoxes_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged
UpdateGenerateLogButtonEnabled()
End Sub
'Manage the GenerateLog button Enabled state
Private Sub UpdateGenerateLogButtonEnabled()
GenerateLogBookButton.Enabled = CheckBox1.Checked AndAlso CheckBox2.Checked AndAlso CheckBox3.Checked AndAlso CheckBox4.Checked AndAlso CheckBox5.Checked AndAlso CheckBox6.Checked AndAlso CheckBox7.Checked AndAlso Not TextBox1.Text = "" AndAlso Not TextBox2.Text = ""
End Sub
As I stated, there are many, many ways to validate this kind of code. If you need your error messages, you'll want a different one. Either way, have fun!

How to pass value from DataGridView to RadioButton

How can I pass a value from a DataGridVeiw to a RadioButton in VB.Net?
I have tried the following code but it is not working.
If DataGridView1.Rows(e.RowIndex).Cells(2).Value.ToString.Equals("A") Then
RadioButton1.Checked = True
ElseIf DataGridView1.Rows(e.RowIndex).Cells(2).Value.ToString.Equals("B") Then
RadioButton2.Checked = True
End If
Check if your database is with "A" spaces, for example it can be like "A ":
If DataGridView1.Rows(e.RowIndex).Cells(2).Value.ToString.Equals("A ")
Then RadioButton1.Checked = True
ElseIf DataGridView1.Rows(e.RowIndex).Cells(2).Value.ToString.Equals("B ")
Then RadioButton2.Checked = True End If

How to use nested if-else in vb.net

I am doing calculator and I wrote codes if user doesn't enter value to num1 and num2 it will give message to the screen and I did it with if-else. It is working for num1 and operator but not num2. How can I fix it?
If TextBox1.Text = "" Or TextBox2.Text = "" Then
If TextBox1.Text = "" Then
MsgBox("Please enter data to number 1 ")
TextBox1.Focus()
Label1.ForeColor = Color.Red
Else
Label1.ForeColor = SystemColors.ControlText
End If
ElseIf TextBox1.Text = "" Or TextBox2.Text = "" Then
If TextBox2.Text = "" Then
MsgBox("Please enter data to number 2")
TextBox2.Focus()
Label2.ForeColor = Color.Red
Else
Label1.ForeColor = SystemColors.ControlText
End If
End If
Your code should be something like:
If TextBox1.Text = "" Or TextBox2.Text = "" Then
If TextBox1.Text = "" Then
MsgBox("Please enter data to number 1 ")
TextBox1.Focus()
Label1.ForeColor = Color.Red
End If
' Seperate the conditions because TextBox1 and TextBox2 can both be empty, so an ElseIf will not handle it
If TextBox2.Text = "" Then
MsgBox("Please enter data to number 2")
TextBox2.Focus()
Label2.ForeColor = Color.Red
End If
Else ' Neither of them (so TextBox1 and TextBox2 won't be empty)
Label1.ForeColor = SystemColors.ControlText
End If
This will popup a message when the user leaves TextBox1 empty or TextBox2 empty, if he doesn't leave any of the two empty, the Else will kick in.
For more informations about conditions in VB.NET, take a look here: https://www.dotnetperls.com/if-vbnet

Complex MsgBoxResult.No function VISUAL BASIC

I've made a button1 that if you click on it it starts the timer. In timer_tick I typed Label7.Text = "L O A D I N G". I've made also Exit (Button2). I want to do a code that if Label7.Text = "L O A D I N G" then the msgboxresult.no in button2 will be Button1.Enabled = True . Here is video how I want to do that: http://www.youtube.com/watch?v=4URYhd0xA8I&feature=youtu.be
Here is the code for Button2 (Exit)
Dim msg As String
Dim title As String
Dim style As MsgBoxStyle
Dim response As MsgBoxResult
msg = "Are you sure you want to exit?" ' Define message for exit."
style = MsgBoxStyle.DefaultButton2 Or _
MsgBoxStyle.Question Or MsgBoxStyle.YesNo
title = "Exit"
' Display message.
response = MsgBox(msg, style, title)
If response = MsgBoxResult.No Then
If Label7.Text = "L O A D I N G" Then
Button1.Enabled = True
Button5.Enabled = False
TextBox1.Enabled = True
End If
End If
If response = MsgBoxResult.Yes Then
Me.Close()
End If
I could just simply do:
If response = MsgBoxResult.No Then
Button1.Enabled = True
but it is important that label7 is also mentioned, because label7 shows up when timer1 starts. Please help I don't know what is happening.