How to use nested if-else in vb.net - 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

Related

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

make program press the enter key on textbox focus

Forgive me if this is not possible, here's the scenario.
I have 2 textboxes, I type something on textbox1 and press enter, the text goes to textbox2 and the program presses the enter key on textbox2(user will not press the enter key).
Here's what I have in textbox1_keydown
if e.keycode = keys.enter then
str = textbox1.text
textbox2.focus()
textbox2.text = str
'Make textbox2 press the enter key here, without user pressing it on keyboard
end if
Found the answer
if e.keycode = keys.enter then
str = textbox1.text
textbox2.focus()
textbox2.text = str
'Make textbox2 press the enter key here, without user pressing it on keyboard
SendKeys.Send("{ENTER}")
end if
If e.keycode = Keys.Enter Then
'/* Check whether textbox1 is empty or not */
If textbox1.text <> "" Then
textbox2.text = ""
textbox2.text = textbox1.text
SendKeys.Send("{ENTER}")
Else
'/* if textbox1 is empty then cursor focus on textbox1 only */
textbox1.focus()
End If
End If
OR
If e.keycode = Keys.Enter Then
textbox2.text = ""
textbox2.text = textbox1.text
SendKeys.Send("{ENTER}")
End If

Pull data from text box and update

I would like to pull data from a text box in a user form and use a calculation with it.
I am unsure on how to do this.
This is currently how the user form looks.
As you can see, when a user presses yes on the radio button, a number will appear under total cost. Same with when they select something from the combo box.
What I want to do is when a user enters a number (between a set range) it will calculate that number, multiplied by another number.
Thanks in advance.
Private Sub OptionButton2_Change()
TextBox2.Text = ""
End Sub
Private Sub OptionButton1_Change()
TextBox1.Text = ""
End Sub
Private Sub TextBox1_Change()
If OptionButton1.Value = True Then
If TextBox1.Text <> "" Then
TextBox2.Text = TextBox1.Text * 10
End If
ElseIf OptionButton2.Value = True Then
If TextBox1.Text <> "" Then
TextBox2.Text = TextBox1.Text * 20
End If
End If
End Sub

How to count login attempts Visual Basic

I want to add a count of login attempts to my login function. When a user types a wrong username and password 3 times, the program should close down and show a message. Here is my code for the Login button in my Form1.vb:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = "13Mendv" And TextBox2.Text = "Admin123" Or
TextBox1.Text = "Admin" And TextBox2.Text = "Admin123" Or
TextBox1.Text = "13PateS" And TextBox2.Text = "Staff123" Or
TextBox1.Text = "13KhetP" And TextBox2.Text = "Member123" Or
TextBox1.Text = "13PateN" And TextBox2.Text = "Scorer123" Or
TextBox1.Text = "13ChatP" And TextBox2.Text = "Captain123" Or
TextBox1.Text = "13BonnN" And TextBox2.Text = "Captain123" Or
TextBox1.Text = "13EarlJ" And TextBox2.Text = "Captain123" Or
TextBox1.Text = "13RajaA" And TextBox2.Text = "Captain123" Or
TextBox1.Text = "1" And TextBox2.Text = "1" Or
TextBox1.Text = "13SchaJ" And TextBox2.Text = "Captain123" Then
Timer1.Start() 'Timer on Form1.vb show
ProgressBar1.Show() 'Progress bar on Form1.vb show
Label8.Show() 'Label8 on Form1.vb show
Button4.Show() 'Button4 on Form1.vb show
Else
If TextBox1.Text = "" And TextBox2.Text = "" Then
MsgBox("No Username and/or Password Found!", MsgBoxStyle.Critical, "Error") 'If statement for checking if there is any input in either username or password entry field
Else
If TextBox1.Text = "" Then
MsgBox("No Username Found!", MsgBoxStyle.Critical, "Error") 'Message box no username found
Else
If TextBox2.Text = "" Then
MsgBox("No Password Found!", MsgBoxStyle.Critical, "Error") 'Message box no password found
Else
MsgBox("Invalid Username And/Or Password!", MsgBoxStyle.Critical, "Error") 'Message box invlaid username and or password
TextBox2.Clear()
End If
End If
End If
End If
End Sub
What can I do to add a count into this code to properly notify the user of their 3 failed login attempts?
Public Class Form1
Dim attempts As Integer = 0
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim user As String
user = TextBox1.Text
If user = "Jhayboy" Then
MsgBox("Access Granted")
Form2.Show()
Me.Hide()
ElseIf attempts = 3 Then
MsgBox("Maximum count of retries(3),And you'reach the maximum attempts!Try again later", MsgBoxStyle.Critical, "Warning")
Close()
Else
MsgBox("Username and Password is incorrect! re-enter again you currently have reached attempt " & attempts & " of 3.")
attempts = attempts + 1
TextBox1.Text = ""
TextBox1.Focus()
End If
End Sub
End Class
As several people have suggested, you could create a variable (cntAttempts) that keeps track of how many times the user tries to login, and if the count reaches 3, the program closes. Like this:
Private cntAttempts = 0
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If ... Then
Timer1.Start() 'Timer on Form1.vb show
ProgressBar1.Show() 'Progress bar on Form1.vb show
Label8.Show() 'Label8 on Form1.vb show
Button4.Show() 'Button4 on Form1.vb show
Else
cntAttempts += 1
If cntAttempts = 3 Then
MessageBox.Show("login failed")
Me.close()
End If
If TextBox1.Text = "" And TextBox2.Text = "" Then
...
Else
...
End If
End If
End Sub
HTH
I added the cnt as integer and increment it until cnt<=3 .
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Dim cnt As Integer = 0
If cnt <= 3 Then
If TextBox1.Text = "13Mendv" And TextBox2.Text = "Admin123" Or
TextBox1.Text = "Admin" And TextBox2.Text = "Admin123" Or
TextBox1.Text = "13PateS" And TextBox2.Text = "Staff123" Or
TextBox1.Text = "13KhetP" And TextBox2.Text = "Member123" Or
TextBox1.Text = "13PateN" And TextBox2.Text = "Scorer123" Or
TextBox1.Text = "13ChatP" And TextBox2.Text = "Captain123" Or
TextBox1.Text = "13BonnN" And TextBox2.Text = "Captain123" Or
TextBox1.Text = "13EarlJ" And TextBox2.Text = "Captain123" Or
TextBox1.Text = "13RajaA" And TextBox2.Text = "Captain123" Or
TextBox1.Text = "1" And TextBox2.Text = "1" Or
TextBox1.Text = "13SchaJ" And TextBox2.Text = "Captain123" Then
Timer1.Start() 'Timer on Form1.vb show
ProgressBar1.Show() 'Progress bar on Form1.vb show
Label8.Show() 'Label8 on Form1.vb show
Button4.Show() 'Button4 on Form1.vb show
Else
cnt = cnt + 1
If TextBox1.Text = "" And TextBox2.Text = "" Then
MsgBox("No Username and/or Password Found!", MsgBoxStyle.Critical, "Error") 'If statement for checking if there is any input in either username or password entry field
Else
If TextBox1.Text = "" Then
MsgBox("No Username Found!", MsgBoxStyle.Critical, "Error") 'Message box no username found
Else
If TextBox2.Text = "" Then
MsgBox("No Password Found!", MsgBoxStyle.Critical, "Error") 'Message box no password found
Else
MsgBox("Invalid Username And/Or Password!", MsgBoxStyle.Critical, "Error") 'Message box invlaid username and or password
TextBox2.Clear()
End If
End If
End If
End If
End If
End Sub`
Dim a =0
If txtname.Text = "yourUsername" And txtpass.Text = "yourPassword" Then
Msgbox("Acces Granted")
a=0
Elseif txtname.Text <> "yourUsername" Or txtpass.Text <> "yourPassword" Then
a = MsgBox("INVALID USERNAME AND PASSWORD") + a
End if
If
a = 3 Then
End
End if
This is probably the best way to solve this issue:
If TextBox1.Text = "Username" And TextBox2.Text = "Password" Or
TextBox1.Text = "Admin" And TextBox2.Text = "Admin123" Then
MsgBox("Welcome!")
Me.Hide()
Form1.Show()
Else
If TextBox1.Text = "" And TextBox2.Text = "" Then
MsgBox("No Username and/or Password Found!", MsgBoxStyle.Critical, "Error")
Else
If TextBox1.Text = "" Then
MsgBox("No Username Found!", MsgBoxStyle.Critical, "Error")
Else
If TextBox2.Text = "" Then
MsgBox("No Password Found!", MsgBoxStyle.Critical, "Error")
Else
MsgBox("Invalid Username And/Or Password!", MsgBoxStyle.Critical, "Error")
TextBox2.Clear()
End If
End If
End If
End If
End Sub
End Class

Issue with some Visual Basic programming code

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.