Form still open up even I use too many if statements - vb.net

Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles nextr.Click
If formroomsinv.lblpartyr.Text = 0 Then
MsgBox("Room is not available please select other rooms, Thankyou.", MsgBoxStyle.Information)
ElseIf formroomsinv.lblbdayr.Text = 0 Then
MsgBox("Room is not available please select other rooms, Thankyou.", MsgBoxStyle.Information)
ElseIf formroomsinv.lblvipr.text = 0 Then
MsgBox("Room is not available please select other rooms, Thankyou.", MsgBoxStyle.Information)
ElseIf Formroomsinv.lbldeluxer.Text = 0 Then
MsgBox("Room is not available please select other rooms, Thankyou.", MsgBoxStyle.Information)
ElseIf birthday = 0 AndAlso deluxe = 0 AndAlso party = 0 AndAlso vip = 0 Then
MessageBox.Show("You must choose a room with at least one hour.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
Me.Hide()
formsnacks.Show()
End If
End Sub
This is the code of the button. When I pressed it, even though the label is = 0 it still opens up my form. I expect that the program will not open form because It has an error message that I've code.

Try to use "0" instead of 0, because Text is a String not an Integer.
And make sure your birthday, deluxe, party and vip variables are also integer, it not, use vip = "0"
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles nextr.Click
If formroomsinv.lblpartyr.Text = "0" Then
MsgBox("Room is not available please select other rooms, Thankyou.", MsgBoxStyle.Information)
ElseIf formroomsinv.lblbdayr.Text = "0" Then
MsgBox("Room is not available please select other rooms, Thankyou.", MsgBoxStyle.Information)
ElseIf formroomsinv.lblvipr.text = "0" Then
MsgBox("Room is not available please select other rooms, Thankyou.", MsgBoxStyle.Information)
ElseIf Formroomsinv.lbldeluxer.Text = "0" Then
MsgBox("Room is not available please select other rooms, Thankyou.", MsgBoxStyle.Information)
ElseIf birthday = 0 AndAlso deluxe = 0 AndAlso party = 0 AndAlso vip = 0 Then
MessageBox.Show("You must choose a room with at least one hour.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
Me.Hide()
formsnacks.Show()
End If
End Sub

Related

ISNUMERIC AND EMPTY STRING

I want to create an application where if someone enters a number, it is entered into a listbox but if someone enters an alphabet or leave the textbox blank, a message box should appear. How to do this? Thanks.
Private Sub btnRecord_Click(sender As Object, e As EventArgs) Handles btnRecord.Click
Dim grade As Double
grade = CDbl(txtGrades.Text)
If grade >= 0 And IsNumeric(grade) = True Then
lstGrades.Items.Add(grade)
txtGrades.Text = " "
ElseIf txtGrades.Text = " " Then
MessageBox.Show("Number cannot be less than 0", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
ElseIf IsNumeric(txtGrades.Text) = False Then
MessageBox.Show("Number cannot be an alphabet", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
End Sub
You could simplify your logic and eliminate errors by using TryParse. This method determines if a String can be converted to a Double and returns either True or False along with the converted value:
Private Sub btnRecord_Click(sender As Object, e As EventArgs) Handles btnRecord.Click
Dim grade As Double
If Double.TryParse(txtGrades.Text, grade) Then
lstGrades.Items.Add(grade)
txtGrades.Text = " "
Else
MessageBox.Show("Number cannot be an alphabet", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
End Sub

Input Numbers and Unit Grades Only at Textbox (VB.Net)

Hi I Just Want to Input 1 to 5 only as unit grades on textbox
i use this code:
Private Sub TextBox16_TextChanged(sender As Object, e As EventArgs) Handles TextBox16.TextChanged
If TextBox16.Text >= 5 Then
TextBox16.clear()
MsgBox("Unit Grades Only From 1 to 5")
End If
End Sub
End Class
error came up:
An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
Additional information: Conversion from string "" to type 'Double' is not valid.
Alright you have two ways to do this:
Preventing Typing Them
In this case, you will deny user from typing other characters. So code this:
Private Sub TextBox16_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox16.KeyPress
'Deny user from entering more than one charecter
TextBox1.MaxLength = 1
Select Case e.KeyChar
Case "1", "2", "3", "4", "5" ,vbBack 'vbBack is backspace
'You can enter any number that you whant
e.Handled = False
Case Else
'Ignore the key
e.Handled = True
'Play a Beep sound.
Beep()
End Select
End Sub
or
Checking With TextChanged
Type in this in your method:
Dim textbox_val As Integer
'The user may enter alphabetical characters
Try
textbox_val = Convert.ToInt32(TextBox1.Text)
Catch ex As Exception
MsgBox("Unit Grades Only From 1 to 5")
Exit Sub
End Try
If textbox_val >= 5 Then
TextBox1.Clear()
MsgBox("Unit Grades Only From 1 to 5")
End If
If you have your heart set on using a textbox for this , the following code should prevent any besides a 1 through 5 in the textbox. I used a textbox with a bit better naming txtUnitGrade.
Private _sLastValidValue As String = String.Empty
Private _bIgnoreChange As Boolean = False
Private Sub txtUnitGrade_TextChanged(sender As Object, e As EventArgs) Handles txtUnitGrade.TextChanged
If Not _bIgnoreChange Then
If txtUnitGrade.Text = String.Empty Then
_sLastValidValue = String.Empty
Else
If Not IsNumeric(txtUnitGrade.Text) Then
SetValueToLast()
Else
Dim iParsedValue As Integer = Integer.MinValue
If Not (Integer.TryParse(txtUnitGrade.Text, iParsedValue)) OrElse iParsedValue < 0 OrElse iParsedValue > 5 Then
SetValueToLast()
Else
_sLastValidValue = txtUnitGrade.Text
End If
End If
End If
End If
End Sub
Private Sub SetValueToLast()
Beep() 'you could add some other audible or visual cue that an block occured
_bIgnoreChange = True
txtUnitGrade.Text = _sLastValidValue
txtUnitGrade.SelectionStart = txtUnitGrade.Text.Length
_bIgnoreChange = False
End Sub

Web Browser auto login error (Object reference not set to an instance of an object.)

I have 2 buttons on my form and here is the two bits of code.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If LoggedInToAdastra = True Then
LogOutOfAdastra()
Exit Sub
End If
strUsername = txtUsername.Text
strPassword = txtPassword.Text
LoggedInToAdastra = False
ProgressBar1.Value = 10
Try
WebBrowser1.ScriptErrorsSuppressed = True
WebBrowser1.Navigate("https://nww.awv.nems.nhs.uk/AWA/login.aspx?ReturnUrl=%2fAWA%2fdefault.aspx")
WaitForPageLoad()
WebBrowser1.Document.All("ctl00$MainContent$txtUserName").SetAttribute("value", strUsername)
WebBrowser1.Document.All("ctl00$MainContent$txtPassWord").SetAttribute("value", strPassword)
WebBrowser1.Document.All("ctl00$MainContent$btnLogin").InvokeMember("click")
ProgressBar1.Value = 40
WaitForPageLoad()
ProgressBar1.Value = 80
WebBrowser1.Document.All("ctl00$MainContent$ctl01").InvokeMember("click")
WaitForPageLoad()
ProgressBar1.Value = 100
LoggedInToAdastra = True
txtPassword.Enabled = False
txtUsername.Enabled = False
Button2.Enabled = False
Button1.Text = "Log Out"
Button2.BackColor = Color.MintCream
Button1.BackColor = Color.MintCream
txtPassword.BackColor = Color.MintCream
txtUsername.BackColor = Color.MintCream
ProgressBar1.Value = 0
btnUpdate.Enabled = True
btnDoAll.Enabled = True
btnSearchSelected.Enabled = True
doc = Nothing
Catch ex As Exception
MsgBox("Login Failed")
Button2.BackColor = Color.MistyRose
Button1.BackColor = Color.MistyRose
txtPassword.BackColor = Color.MistyRose
txtUsername.BackColor = Color.MistyRose
txtUsername.Text = ""
txtPassword.Text = ""
ProgressBar1.Value = 0
End Try
End Sub
And the second button, once logged in, you should be able to search for a patient using this button and choosing a patient from a listbox...
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
If LoggedInToAdastra = False Then
MsgBox("Please re-login to Adastra")
End If
strPatientNHS = lstPatients.SelectedItems(0).SubItems(0).Text
EditNote(strPatientNHS)
End Sub
Public Sub EditNote(strPatientNHS As String)
'Try
doc = WebBrowser1.Document
WebBrowser1.ScriptErrorsSuppressed = True
WebBrowser1.Document.OpenNew(True)
WebBrowser1.Navigate("https://nww.awv.nems.nhs.uk/AWA/login.aspx?ReturnUrl=%2fAWA%2fdefault.aspx")
ProgressBar1.Value = 10
WaitForPageLoad()
ProgressBar1.Value = 20
WebBrowser1.Document.All("ctl00$MainContent$txtUserName").SetAttribute("value", strUsername)
WebBrowser1.Document.All("ctl00$MainContent$txtPassWord").SetAttribute("value", strPassword)
WebBrowser1.Document.All("ctl00$MainContent$btnLogin").InvokeMember("click")
'WebBrowser1.Navigate("https://nww.awv.nems.nhs.uk/AWA/MainMenu.aspx")
WaitForPageLoad()
ProgressBar1.Value = 30
WebBrowser1.Document.All("ctl00$MainContent$ctl01").InvokeMember("click")
WaitForPageLoad()
ProgressBar1.Value = 40
WebBrowser1.Document.GetElementById("ct100_MainContent_txtNationalNumber").SetAttribute("value", strPatientNHS)
WebBrowser1.Document.GetElementById("ct100_MainContent_cboSurgery2").SetAttribute("value", "All Practices")
WebBrowser1.Document.All("ct100$MainContent$bSearch").InvokeMember("click")
End Sub
It errors here on the EditNote sub
WebBrowser1.Document.GetElementById("ct100_MainContent_txtNationalNumber").SetAttribute("value", strPatientNHS)
WebBrowser1.Document.GetElementById("ct100_MainContent_cboSurgery2").SetAttribute("value", "All Practices")
WebBrowser1.Document.All("ct100$MainContent$bSearch").InvokeMember("click")
I have tried all sorts, but not sure why its bringing through the Error (Object reference not set to an instance of an object.)
The control does exist on the web page, and that is the correct ID for it. I have tried using Document.All and looking for the element too but to no avail.
I did resolve this in the end, in quite bizarre fashion...
so it was giving me an error basically because it couldn't find the control
WebBrowser1.Document.All("ctl00$MainContent$txtUserName")
so as a last resort, I copied and pasted the exact control from the page source and it worked... even though every character was the same...
This fixed my problem anyway. Don't type the control out, copy and paste it! Maybe someone with more knowledge can explain how this works? is there hidden characters in the HTML?

How to limit user for login more than three times in this vb.net coding

How can I modify the following code to limit a user to three login attempts?
Public Class Form3
Dim Attemp As Integer = 0
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.Text = "1" And TextBox2.Text = "1" Or
TextBox1.Text = "2" And TextBox2.Text = "2" Then
MsgBox("You are Now Logged In as User Rights", MsgBoxStyle.Information, "Login")
Form5.Show()
Close()
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")
End If
End If
End If
End If
End Sub
End Class
You have to add something like Attempt = Attempt + 1 just before the last end if. Then you can check Attempt variable in the first if with an and.

Visual Basic 2010 Express: Check if my.settings.username has a value

VISUAL BASIC: You know how you can make your own settings in your program setting/properties, well I made a register system for a software. You register by entering a password and username then pressing 'Register' to save the username and password to My.Settings.Username and My.Settings.Password using My.Settings.Save().
The code: My.Settings.Username = TextBox1.Text
My.Settings.Password = TextBox2.Text
My.Settings.Save()
This all works perfectly but how can I prevent the user from changing it again.
This is my code so far:
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Dim res As MsgBoxResult
If Not My.Settings.Username = String.Empty And
My.Settings.Password = String.Empty Then
MsgBox("You have registered a WCUE account, please use 'CHANGE PASSWORD' in 'Settings' to do so.", MsgBoxStyle.Exclamation, "Not possible.")
ElseIf TextBox1.Text = String.Empty Or
TextBox2.Text = String.Empty Then
MsgBox("Don't press the register button if the text boxes are empty!", MsgBoxStyle.Critical, "Fill all required fields please.")
Else
If TextBox1.Text = My.Settings.Username And
TextBox2.Text = My.Settings.Password Then
MsgBox("If you are trying to log in then press the 'Log-in' text below the register button!", MsgBoxStyle.Exclamation, "You have already registered!")
Else
res = MsgBox("Register a WCUE account?", MsgBoxStyle.YesNo, "Create")
If res = MsgBoxResult.Yes Then
My.Settings.Username = TextBox1.Text
My.Settings.Password = TextBox2.Text
My.Settings.Save()
MsgBox("You can now login to WCUE and set the time limit on this user!", MsgBoxStyle.Information, "YEAH - Register successful!")
ElseIf res = MsgBoxResult.No Then
MsgBox("You should register as quick as possible before someone unauthorized sets a password!!", MsgBoxStyle.Exclamation, "Suit yourself.")
End If
End If
Exit Sub
End If
End Sub
The FIRST if statement is what I am having trouble with, I tried to check if My.Settings.Username and My.Setting.Password contains a value (string) which should be the username and password, if it does not then you can register but if it does contain a value then it tells you that someone has already registered. Nothing happens?
I hope this is clear enough, please help!
I ~think~ this is what you're trying to do...
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
If My.Settings.Username <> String.Empty AndAlso My.Settings.Password <> String.Empty Then
MsgBox("You have registered a WCUE account, please use 'CHANGE PASSWORD' in 'Settings' to do so.", MsgBoxStyle.Exclamation, "Not possible.")
Else
If TextBox1.Text = String.Empty OrElse TextBox2.Text = String.Empty Then
MsgBox("Don't press the register button if the text boxes are empty!", MsgBoxStyle.Critical, "Fill all required fields please.")
Else
Dim res As MsgBoxResult
res = MsgBox("Register a WCUE account?", MsgBoxStyle.YesNo, "Create")
If res = MsgBoxResult.Yes Then
My.Settings.Username = TextBox1.Text
My.Settings.Password = TextBox2.Text
My.Settings.Save()
MsgBox("You can now login to WCUE and set the time limit on this user!", MsgBoxStyle.Information, "YEAH - Register successful!")
ElseIf res = MsgBoxResult.No Then
MsgBox("You should register as quick as possible before someone unauthorized sets a password!!", MsgBoxStyle.Exclamation, "Suit yourself.")
End If
End If
End If
End Sub