if, elseif, searching, comparison, greater, smaller - vb.net

I have this code on VB. I get no syntax error but when running and testing the numbers i get "number is not valid" or a wrong answer. Would you please help me what is wrong with this code? Thank you.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
TextBox2.Text = ""
Dim Num As Integer
Dim str As String
str = TextBox1.Text
Num = Integer.Parse(str)
If Num >= 100 And Num <= 199 Then
TextBox2.Text = "Basement"
ElseIf Num >= 200 And Num <= 500 And Num >= 900 Then
TextBox2.Text = "Main Floor"
ElseIf Num <= 700 And Num >= 501 And Num >= 750 And Num <= 900 Then
TextBox2.Text = "Upper Floor"
ElseIf Num <= 750 And Num >= 700 Then
TextBox2.Text = "Archives"
Else
TextBox2.Text = "Number is not valid"
End If
End Sub

This is impossible
ElseIf Num >= 200 And Num <= 500 And Num >= 900 Then
Here you are asking that a number be greater-equal than 200 (ok), lower-equal than 500 (ok) AND greater-equal than 900 (not ok since you already asked for a number lower-equal than 500). A number can't both be lower than 500 and greater than 900.
Without having the requirements, it's hard to know what your if statement should look like. I would suggest you run through your code manually "on paper" or start with one if statement at a time instead of writing all of them.

I would not mix to many checks in one line as it can get confusing.
Do somthing like this:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
TextBox2.Text = ""
Dim Num As Integer
Dim str As String
str = TextBox1.Text
Num = Integer.Parse(str)
If Num >= 100 And Num <= 199 Then
TextBox2.Text = "Basement"
ElseIf Num >= 200 And Num <= 500 Then
TextBox2.Text = "Main Floor"
ElseIf Num >= 501 And Num <= 700 then
TextBox2.Text = "Upper Floor"
ElseIf Num ..... and so on
Else
TextBox2.Text = "Number is not valid"
End If
End Sub
Also it's good practice to name your TextBox controls and any other controls:
textbox1 could be something like txtfloornumber
textbox2 could be something like txtfloorname
This makes coming back to your code later in life easier.

Related

How would I calculate overtime pay?

I'm new to Visual Basics and I need some help.
I need to calculate the overtime pay into the gross pay.
Here is my whole code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Assigns integer and decimals'
Dim intHoursWorked As Integer
Dim decPayPerHour As Decimal
Dim decGrossPay As Decimal
Dim decTax As Decimal
Dim decNetPay As Decimal
'Conversions'
If IsNumeric(Me.HoursWorked.Text) Then
intHoursWorked = Convert.ToInt32(Me.HoursWorked.Text)
If IsNumeric(Me.txtPPH.Text) Then
decPayPerHour = Convert.ToDecimal(Me.txtPPH.Text)
End If
End If
'Hours worked + overtime'
If intHoursWorked > 48 Then
MsgBox("You have exceeded your maximum hours")
Me.HoursWorked.Text = ""
Me.lblNetPay.Text = ""
Me.lblGrossPay.Text = ""
Me.lblTax.Text = ""
ElseIf intHoursWorked < 23 Then
MsgBox("You must enter at least 23 hours")
' Add time and a half for overtime
ElseIf decGrossPay = decGrossPay + (decGrossPay - 40) * 0.5 Then
End If
'Pay per hour'
If decPayPerHour < 14.25 Then
MsgBox("You must enter a value above $14.25")
ElseIf decPayPerHour > 100 Then
MsgBox("You must enter a value lower than $100")
Else
If Me.radSingleRate.Checked = True Then
decTax = 0.205D
ElseIf Me.radFamilyRate.Checked = True Then
decTax = 0.15D
End If
End If
'Calculations for Gross, Tax, and Netpay'
decGrossPay = intHoursWorked * decPayPerHour
decTax = intHoursWorked * decPayPerHour * decTax
decNetPay = decGrossPay - decTax
Me.lblGrossPay.Text = decGrossPay.ToString("C")
Me.lblNetPay.Text = decNetPay.ToString("C")
Me.lblTax.Text = decTax.ToString("C")
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.txtName.Focus()
Me.radSingleRate.Checked = True
Me.lblTax.Text = ""
Me.lblGrossPay.Text = ""
Me.lblNetPay.Text = ""
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
' Clears all inputs '
Me.txtName.Text = ""
Me.HoursWorked.Text = ""
Me.txtPPH.Text = ""
Me.lblGrossPay.Text = ""
Me.lblTax.Text = ""
Me.lblNetPay.Text = ""
Me.radSingleRate.Checked = True
Me.radFamilyRate.Checked = False
Me.txtName.Focus()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
' Closes Application '
Me.Close()
End Sub
End Class
I can't figure out what code I should be typing into my code to have it calculate the gross pay. The overtime should be automatically calculated by the program if a number over 40 hours worked is inputted by the user. Overtime is time and a half.
I think you are after something like this?
Select Case intHoursWorked
Case Is > 48
MsgBox ("You have exceeded your maximum hours")
Case Is < 23
MsgBox ("You must enter at least 23 hours")
Case 40 To 48
decGrossPay = decGrossPay * 5
Case Else
'Add code for any other scenarios if required.
End With
Edit, not really sure how you are calculating the pay. Don't you need a figure for the regular hourly rate? If so, maybe something like this. Can't test because it's only partial code and just a guess.
Select Case intHoursWorked
Case Is > 48
MsgBox ("You have exceeded your maximum hours")
Case Is < 23
MsgBox ("You must enter at least 23 hours")
Case 40 To 48
decGrossPay = hourlyrate * 40 + ((intHoursWorked - 40) * (hourlyrate * 1.5))
Case Else
decGrossPay = hourlyrate * intHoursWorked
End With
This part got problem as you did not consider the hours between 24 ~ 48:
ElseIf intHoursWorked < 23 Then
MsgBox("You must enter at least 23 hours")
' Add time and a half for overtime
ElseIf decGrossPay = decGrossPay + (decGrossPay - 40) * 0.5 Then
End If
Should it work if you amend the code as following, so that if the worker work more than 24 but less than 40 hours, they only get basic hours, when they work more than 40 hours, they will get basic + OT, although I did not go through the remaining part and i am consider the decGrossPay is total hours to be paid:
ElseIf intHoursWorked < 23 Then
MsgBox("You must enter at least 23 hours")
' Add time and a half for overtime
ElseIf intHoursWorked > 40 and intHoursWorked <= 48 Then
decGrossPay = 40+ ((intHoursWorked - 40) *0.5 )
else decGrossPay = 40
End If

Lottery Program - Visual Basic

Have to create a lottery program, getting the random numbers and such easily enough. However I'm stumped. Essentially I have 2 buttons. One to display a checked list box with numbers 1-100, along with the 5 lotto numbers. I have a 2nd button that checks 2 things, to make sure that more than 5 numbers are not checked matching numbers. I'm lost on how to check for a match between the selected numbers between the RNG numbers.
Public Class Form1
Private Sub displayBtn_Click(sender As Object, e As EventArgs) Handles displayBtn.Click
Dim lottoNumbers(5) As Integer
Dim counter As Integer = 0
Dim number As Integer
Dim randomGenerator As New Random(Now.Millisecond)
'This will randomly select 5 unique numbers'
Do While counter < 5
number = randomGenerator.Next(0, 98)
If Array.IndexOf(lottoNumbers, number) = -1 Then
lottoNumbers(counter) = number
counter += 1
End If
Loop
'Display the lottery numbers in the label.'
Label1.Text = String.Empty
Array.Sort(lottoNumbers)
For Each num As Integer In lottoNumbers
Label2.Text = "Lottery Numbers"
Label1.Text &= CStr(num) & " "
Next num
For x = 0 To 98
CheckedListBox1.Items.Add(x + 1)
Next
End Sub
Private Sub checkBtn_Click(sender As Object, e As EventArgs) Handles checkBtn.Click
Dim count As Integer = 0
Dim x As Integer
'Checks to see if user checked more than 5'
For x = 0 To CheckedListBox1.Items.Count - 1
If (CheckedListBox1.CheckedItems.Count > 5) Then
MessageBox.Show("You cannot select more than 5 numbers.")
Return
Else
If (CheckedListBox1.GetItemChecked(x) = True) Then
count = count + 1
ListBox1.Items.Add(CheckedListBox1.Items(x))
End If
End If
Next
End Sub

VB.NEt SpVoice / SAPI always first in action

Hello guys i want to get some help.. because my program "BINGO caller" the SAPI/SpVoice is always speaking first before displaying the value in my textbox. and other problem is while speaking the SAPI all buttons and commands are not functioning they become ok when the SAPI is done speaking and the number they speak is showing in texbox after the speak. please help me.. i want to my program is displaying the number while SAPI speak the value in my textbox. please help me. this is my code in displaying the Number and letter in textbox and this is the code in SAPI
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
nos = randomarray(index)
index = index + 1
If nos <= 15 And nos >= 1 Then
col = "B"
ElseIf nos <= 30 And nos >= 16 Then
col = "I"
ElseIf nos <= 45 And nos >= 31 Then
col = "N"
ElseIf nos <= 60 And nos >= 46 Then
col = "G"
ElseIf nos <= 75 And nos >= 61 Then
col = "O"
End If
lbl_no.Text = nos
lbl_let.Text = col
sVar = Mid(nos, 1, 1)
sVar2 = Mid(nos, 2)
CType(Me.Controls("lbl_" & nos.ToString), Label).BackColor = Color.Yellow
If index = randomarray.Count Then
Timer1.Stop()
btn_pause.Enabled = False
btn_clear.Show()
btn_exit.Hide()
End If
voice()
txt_count.Text = index - 1
End Sub
This is the SAPI code
Sub voice()
Dim VObj As Object
VObj = CreateObject("SAPI.SpVoice")
With VObj
.Volume = 100
.Rate = -2
.Speak(col & " " & nos)
.Speak(col & " " & sVar & " " & sVar2)
End With
End Sub
This is my Screenshot of my program
instead of calling voice() on the ui thread just call it on a thread pool thread.
'voice()
System.Threading.ThreadPool.QueueUserWorkItem(AddressOf voice)

I want to create a package check on vb, but my codes aren't working any suggestion?

I can't think of any other way to make it work, any suggestions?
This are my codes:
Public Class frmPackageCheck
Private Sub btnCheck_Click(sender As Object, e As EventArgs) Handles btnCheck.Click
If txtWeight.Text <= 27 Then
Me.lblAnswer.Text = "Accepted"
ElseIf txtWeight.Text >= 27 Then
Me.lblAnswer.Text = "Rejected: Too heavy"
End If
If txtLength.Text <= 100 Then
Me.lblAnswer.Text = "Accepted"
ElseIf txtLength.Text >= 100 Then
Me.lblAnswer.Text = "Rejected: Too large"
End If
If lblWidth.Text <= 100 Then
Me.lblAnswer.Text = "Accepted"
ElseIf txtWidth.Text >= 100 Then
Me.lblAnswer.Text = "Rejected: Too large"
End If
If txtHeight.Text <= 100 Then
Me.lblAnswer.Text = "Accepted"
ElseIf txtHeight.Text >= 100 Then
Me.lblAnswer.Text = "Rejected: Too large"
End If
End Sub
Your problem is likely that Me.lblAnswer.Text keeps getting changed. For example, if the weight is 50, the answer text will be set to "Rejected: Too heavy", but in the next if-block it will get replaced (either by "Accepted" or "Rejected: Too large"). You should continue checking more values only if the state is 'accepted'. Once a rejection reason is found, you should report it.
Alternately you can build a more complicated answer text that is either a combination of all rejection reasons, or 'accepted'. This would be slightly more complicated to program but would find all problems in one pass.

Do..While...Loop Result

What should the results be of the following pseudocode:
Initialize counter to 10
Do while counter < 100
Display counter multiplied by 2
Add 10 to the counter
End loop
I'm thinking: 20, 60, 140
This is my code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim multiplied As Integer
Dim counter As Integer = 10
Do While counter < 100
multiplied = counter * 2
Label1.Text = Label1.Text & ControlChars.NewLine & multiplied.ToString
counter = multiplied + 10
Loop
End Sub
Thanks guys!!
Display counter multiplied by 2
Nothing in that instruction says to modify the counter. Based on a strict interpretation of your instructions, the output should look like this:
20 40 60 80 100 120 140 160 180
Your code, however, matches the results you expect. If you want code that matches your instructions, do it like this:
Dim counter As Integer = 0
Dim result As New StringBuilder()
Dim delimiter As String = ""
Do While counter < 100
result.Append(delimiter).Append( (counter*2).ToString() )
delimiter = Environment.NewLine
counter += 10
Loop
Label1.Text = result.ToString()
And for fun we could do something like this:
Label1.Text = Enumerable.Range(1, 9)
.Select(Function(i) i * 10)
.Aggregate("", Function(s, i) s = s & i.ToString() & ",")