If Statement inside Case Statement (VB.Net) - vb.net

Hi I am new in coding at Visual studio.
I'm Using Visual Studio 2012
I Have a Question.
I want to Connect combobox and textbox31 to textbox1
ex. if the combobox is 100 and textbox31 is 1 so the textbox1 will be 100
I End Up With This Code:
Dim c As String
c = ComboBox1.Text
Select Case "c"
Case 100
If TextBox31.Text >= 2.25 Then
TextBox1.Text = 100
ElseIf TextBox31.Text >= 2.5 Then
TextBox1.Text = 75
ElseIf TextBox31.Text >= 2.75 Then
TextBox1.Text = 50
ElseIf TextBox31.Text >= 3 Then
TextBox1.Text = 25
ElseIf TextBox31.Text >= 3.25 Then
TextBox1.Text = 0
End If
Case 75
If TextBox31.Text >= 2.25 Then
TextBox31.Text = 75
ElseIf TextBox1.Text >= 2.5 Then
TextBox1.Text = 75
ElseIf TextBox31.Text >= 2.75 Then
TextBox1.Text = 50
ElseIf TextBox31.Text >= 3 Then
TextBox1.Text = 25
ElseIf TextBox31.Text >= 3.25 Then
TextBox1.Text = 0
End If
Case 50
If TextBox31.Text >= 2.25 Then
TextBox1.Text = 50
ElseIf TextBox31.Text >= 2.5 Then
TextBox1.Text = 50
ElseIf TextBox31.Text >= 2.75 Then
TextBox1.Text = 50
ElseIf TextBox31.Text >= 3 Then
TextBox1.Text = 25
ElseIf TextBox31.Text >= 3.25 Then
TextBox1.Text = 0
End If
Case 25
If TextBox31.Text >= 2.25 Then
TextBox1.Text = 25
ElseIf TextBox31.Text >= 2.5 Then
TextBox1.Text = 25
ElseIf TextBox31.Text >= 2.75 Then
TextBox1.Text = 25
ElseIf TextBox31.Text >= 3 Then
TextBox1.Text = 25
ElseIf TextBox31.Text >= 3.25 Then
TextBox1.Text = 0
End If
End Select
but when i iput in combobox and textbox 31 the textbox1 didnt respond or what i want to get.

The logic you're using there is nested a bit more than I'd normally be comfortable with. In this instance you could consider something more like this:
Dim c As Integer
Dim aintValues() As Integer = {0, 25, 50, 75, 100}
If IsNumeric(ComboBox1.Text) And IsNumeric(TextBox31.Text) Then
c = ComboBox1.Text
Else
Exit Sub
End If
If c <= 25 Then
aintValues = {0, 25, 25, 25, 25}
ElseIf c <= 50 Then
aintValues = {0, 25, 50, 50, 50}
ElseIf c <= 75 Then
aintValues = {0, 25, 50, 75, 75}
End If
If TextBox31.Text >= 3.25 Then
TextBox1.Text = aintValues(0)
ElseIf TextBox31.Text >= 3.0 Then
TextBox1.Text = aintValues(1)
ElseIf TextBox31.Text >= 2.75 Then
TextBox1.Text = aintValues(2)
ElseIf TextBox31.Text >= 2.5 Then
TextBox1.Text = aintValues(3)
ElseIf TextBox31.Text >= 2.25 Then
TextBox1.Text = aintValues(4)
ElseIf TextBox31.Text >= 0 Then
TextBox1.Text = 42
Else
' negative
End If
Or you can still use a select case as follows:
Dim c As Integer
Dim aintValues() As Integer = {0, 25, 50, 75, 100}
If IsNumeric(ComboBox1.Text) And IsNumeric(TextBox31.Text) Then
c = ComboBox1.Text
Else
Exit Sub
End If
If c <= 25 Then
aintValues = {0, 25, 25, 25, 25}
ElseIf c <= 50 Then
aintValues = {0, 25, 50, 50, 50}
ElseIf c <= 75 Then
aintValues = {0, 25, 50, 75, 75}
End If
Select Case TextBox31.Text
Case Is >= 3.25
TextBox1.Text = aintValues(0)
Case Is >= 3.0
TextBox1.Text = aintValues(1)
Case Is >= 2.75
TextBox1.Text = aintValues(2)
Case Is >= 2.5
TextBox1.Text = aintValues(3)
Case Is >= 2.25
TextBox1.Text = aintValues(4)
Case Is >= 0
TextBox1.Text = 42
Case Else
' negative
End Select

I don't know what your script is about, but one thing could be your failure:
The first if..then-statement sets the text of TextBox31 instead of TextBox1.
I rather would write:
If TextBox31.Text >= 2.25 Then
TextBox1.Text = 100

There's a couple issues with your code, but one place to start would be to understand that your conditional
Case 100
If TextBox31.Text >= 2.25 Then
TextBox1.Text = 100
ElseIf TextBox31.Text >= 2.5 Then
TextBox1.Text = 75
ElseIf TextBox31.Text >= 2.75 Then
TextBox1.Text = 50
ElseIf TextBox31.Text >= 3 Then
TextBox1.Text = 25
ElseIf TextBox31.Text >= 3.25 Then
TextBox1.Text = 0
End If
is functionally equivalent to
Case 100
If TextBox31.Text >= 2.25 Then
TextBox1.Text = 100
End If
the other conditions will never be tested because if TextBox31.Text is any value greater than or equal to 2.25 then it passes and we're done.
Addressing this problem, you might reverse the order of your conditions, i.e.
Case 100
If TextBox31.Text >= 3.25 Then
TextBox1.Text = 0
ElseIf TextBox31.Text >= 3 Then
TextBox1.Text = 25
ElseIf TextBox31.Text >= 2.75 Then
TextBox1.Text = 50
ElseIf TextBox31.Text >= 2.5 Then
TextBox1.Text = 75
ElseIf TextBox31.Text >= 2.25 Then
TextBox1.Text = 100
End If
Additionally, it sounds like you'd like to cover the situation in which TextBox31.Text < 2.25 which you could do with an Else
Case 100
If TextBox31.Text >= 3.25 Then
TextBox1.Text = 0
ElseIf TextBox31.Text >= 3 Then
TextBox1.Text = 25
ElseIf TextBox31.Text >= 2.75 Then
TextBox1.Text = 50
ElseIf TextBox31.Text >= 2.5 Then
TextBox1.Text = 75
ElseIf TextBox31.Text >= 2.25 Then
TextBox1.Text = 100
Else
TextBox1.Text = 100000
End If

I Solve Now The Problem
i Use This Code:
Case 100
If TextBox31.Text >= 3 Then
TextBox1.Text = 0
ElseIf TextBox31.Text >= 2.75 Then
TextBox1.Text = 25
ElseIf TextBox31.Text >= 2.5 Then
TextBox1.Text = 50
ElseIf TextBox31.Text >= 2.25 Then
TextBox1.Text = 75
ElseIf TextBox31.Text >= 1 Then
TextBox1.Text = 100
Else
TextBox1.Text = 0
Thanks Mr.sfletche For Not Giving up helping me.
Same With Other who help me thanyo very much
Mr.sfletche if i have other problems regarding this would you help me in fute
thanks alot guys

It's a problem of mathematical Sequences. You must not use if statement here. If so, you'll go insane when to work to revise is happened unfortunately.
'Example Data
Dim c = "50"
Dim Text = "3.12"
Dim value As Double
Double.TryParse(Text, value)
Dim num = Math.Ceiling((3.25 - value) / 0.25)
Console.WriteLine(Math.Min(Integer.parseInt(c), 25 * num))
I assume that all you want to do is the above.

Related

How to print out multiple If Statement in Visual Basic

I have been trying to display this if statement in my VB form application with no luck. Below is the code I have attempted.
Dim iNumber1 As Double
Dim iNumber2 As Double
Dim iNumber3 As Double
Dim iNumber4 As Double
Dim iNumber5 As Double
Dim iNumber6 As Double
Dim iNumber7 As Double
Dim iAns As Double
Dim overall As Double
iNumber1 = TextBox1.Text
iNumber2 = TextBox2.Text
iNumber3 = TextBox3.Text
iNumber4 = TextBox7.Text
iNumber5 = TextBox6.Text
iNumber6 = TextBox5.Text
iNumber7 = TextBox8.Text
iAns = iNumber1 + iNumber2 + iNumber3 + iNumber4 + iNumber5 + iNumber6 + iNumber7
overall = (Math.Round(Val(iAns / 7), 1))
If iNumber1 <= 1.9 Then
TextBox4.Text = $"Comprehension: {iNumber1} Entering"
ElseIf iNumber1 >= 2.9 And iNumber1 >= 2 Then
TextBox4.Text = $"Comprehension: {iNumber1} Emerging"
ElseIf iNumber1 >= 3.9 And iNumber1 >= 3 Then
TextBox4.Text = $"Comprehension: {iNumber1} Developing"
ElseIf iNumber1 >= 4.9 And iNumber1 >= 4 Then
TextBox4.Text = $"Comprehension: {iNumber1} Expanding"
ElseIf iNumber1 >= 5.9 And iNumber1 >= 5 Then
TextBox4.Text = $"Comprehension: {iNumber1} Bridging"
ElseIf iNumber1 >= 6.9 And iNumber1 >= 6 Then
TextBox4.Text = $"Comprehension: {iNumber1} Reaching"
End If
Any suggestions on how to get this to come out? I am totally stuck. Thanks.

If statement won't continue

I am trying to do some computations on VB.net. I used if else statement since I'm a bit familiar with it. My code goes like this
Try
Dim a As Integer = msalary.Text
If (a < 9000) Then
Label5.Text = a - 200
ElseIf (9000 < a < 9999.99) Then
Label5.Text = a - 225
ElseIf (10000 < a < 10999.99) Then
Label5.Text = a - 250
ElseIf (11000 <= a < 11999.99) Then
Label5.Text = a - 275
ElseIf (12000 <= a < 12999.99) Then
Label5.Text = a - 300
ElseIf (13000 <= a < 14000) Then
Label5.Text = a - 325
ElseIf (14000 <= a < 15000) Then
Label5.Text = a - 350
ElseIf (15000 <= a < 16000) Then
Label5.Text = a - 375
ElseIf (17000 <= a < 18000) Then
Label5.Text = a - 400
ElseIf (18000 <= a < 19000) Then
Label5.Text = a - 425
ElseIf (19000 <= a < 20000) Then
Label5.Text = a - 450
ElseIf (20000 <= a < 21000) Then
Label5.Text = a - 475
ElseIf (21000 <= a < 22000) Then
Label5.Text = a - 500
ElseIf (22000 <= a < 23000) Then
Label5.Text = a - 525
ElseIf (23000 <= a < 24000) Then
Label5.Text = a - 550
ElseIf (24000 <= a < 25000) Then
Label5.Text = a - 575
ElseIf (25000 <= a < 26000) Then
Label5.Text = a - 600
ElseIf (26000 <= a < 27000) Then
Label5.Text = a - 625
ElseIf (27000 <= a < 28000) Then
Label5.Text = a - 650
ElseIf (28000 <= a < 29000) Then
Label5.Text = a - 675
ElseIf (29000 <= a < 30000) Then
Label5.Text = a - 700
ElseIf (30000 <= a < 31000) Then
Label5.Text = a - 725
ElseIf (31000 <= a < 32000) Then
Label5.Text = a - 750
ElseIf (32000 <= a < 33000) Then
Label5.Text = a - 800
ElseIf (33000 <= a < 34000) Then
Label5.Text = a - 825
ElseIf (34000 <= a < 35000) Then
Label5.Text = a - 850
ElseIf (a >= 35000) Then
Label5.Text = a - 875
ElseIf a = "" Then
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
The farthest the condition was able to go to -225 even if I put 20000 in it. It will only subtract 225 from the 20000. Is there something wrong on what I did or is there a better way to do it?
Looks like you're missing some AndAlso from your If/ElseIf statements. Normally, you wouldn't be able to go from String to Integer without doing a conversion, so I'm guessing you don't have Option Strict
Try
Dim a As Integer = msalary.Text
If (a < 9000) Then
Label5.Text = a - 200
ElseIf (9000 <= a AndAlso a < 10000) Then
Label5.Text = a - 225
ElseIf (10000 <= a AndAlso a < 11000) Then
Label5.Text = a - 250
ElseIf (11000 <= a AndAlso a < 12000) Then
Label5.Text = a - 275
ElseIf (12000 <= a AndAlso a 13000) Then
Label5.Text = a - 300
ElseIf (13000 <= a AndAlso a < 14000) Then
Label5.Text = a - 325
ElseIf (14000 <= a AndAlso a < 15000) Then
Label5.Text = a - 350
ElseIf (15000 <= a AndAlso a < 16000) Then
Label5.Text = a - 375
ElseIf (17000 <= a AndAlso a < 18000) Then
Label5.Text = a - 400
ElseIf (18000 <= a AndAlso a < 19000) Then
Label5.Text = a - 425
ElseIf (19000 <= a AndAlso a < 20000) Then
Label5.Text = a - 450
ElseIf (20000 <= a AndAlso a < 21000) Then
Label5.Text = a - 475
ElseIf (21000 <= a AndAlso a < 22000) Then
Label5.Text = a - 500
ElseIf (22000 <= a AndAlso a < 23000) Then
Label5.Text = a - 525
ElseIf (23000 <= a AndAlso a < 24000) Then
Label5.Text = a - 550
ElseIf (24000 <= a AndAlso a < 25000) Then
Label5.Text = a - 575
ElseIf (25000 <= a AndAlso a < 26000) Then
Label5.Text = a - 600
ElseIf (26000 <= a AndAlso a < 27000) Then
Label5.Text = a - 625
ElseIf (27000 <= a AndAlso a < 28000) Then
Label5.Text = a - 650
ElseIf (28000 <= a AndAlso a < 29000) Then
Label5.Text = a - 675
ElseIf (29000 <= a AndAlso a < 30000) Then
Label5.Text = a - 700
ElseIf (30000 <= a AndAlso a < 31000) Then
Label5.Text = a - 725
ElseIf (31000 <= a AndAlso a < 32000) Then
Label5.Text = a - 750
ElseIf (32000 <= a AndAlso a < 33000) Then
Label5.Text = a - 800
ElseIf (33000 <= a AndAlso a < 34000) Then
Label5.Text = a - 825
ElseIf (34000 <= a AndAlso a < 35000) Then
Label5.Text = a - 850
ElseIf (a >= 35000) Then
Label5.Text = a - 875
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
I wouldn't use such a giant conditional branch, I'd find a more algorithmic approach.
Example:
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim salary As Integer = 34567, subtractor As Integer = 200
Dim r As Range = GetSalaryRange(salary)
For I As Integer = 9000 To r.Lower Step 1000
subtractor += 25
Next
Label5.Text = (salary - subtractor).ToString
End Sub
Function GetSalaryRange(salary As Integer) As Range
If salary < 9000 Then Return New Range() With {.Lower = 0, .Upper = 8999}
Dim remainder As Integer = salary Mod 1000
Return New Range With {.Lower = salary - remainder, .Upper = salary - remainder + 999}
End Function
Public Class Range
Public Lower, Upper As Integer
Public Function Contains(number As Integer) As Boolean
If number >= Lower AndAlso number <= Upper Then Return True Else Return False
End Function
End Class
End Class

visual basics equal to or greater than

I am trying to write some code that says if textbox1 is equal to 0 between 10 then HandDecimal = 1. Else if textbox1 is equal to 10.1 through 100, then HandDecimal = 3. Else if textbox1 is equal to 100.1 and greater than HandDecimal = 5.
Here is my code, but it does not seem to work for me.
If WeightDecimal = 0 <= 10 Then
HandDecimal = 1
ElseIf WeightTextBox.Text = 10 <= 100 Then
HandDecimal = 3
ElseIf WeightTextBox.Text >= 100.1 Then
HandDecimal = 5
End If
How do I have to change the code to make it work?
Dim weight as Decimal = Decimal.Parse(WeightTextBox.Text)
If weight >= 0 AndAlso weight <= 10 Then
HandDecimal = 1
ElseIf weight > 10 AndAlso weight <= 100 Then
HandDecimal = 3
ElseIf weight > 100 Then
HandDecimal = 5
End If
Select Case statement with To operator
Select Case WeightDecimal
Case 0 To 10
HandDecimal = 1
Case 10.1 To 100
HandDecimal = 3
Case Else
HandDecimal = 5
End Select

Visual Basic - "If" Statements Not Showing Result In Label

I'm working on an assignment where the program needs to calculate and display the total cost for shipping a package, based on weight and whether it is being shipped to Continental U.S., Alaska, or Hawaii. When I click the Calculate button, though, the label that is supposed to display the total is left blank. I've looked through this and tried placing the calculation in different parts/at the end of the "If" statements. Here's what I have thus far, any help would be appreciated:
Dim decWeight As Decimal
Dim decTotalCost As Decimal
Dim decDestination As Decimal
Dim decRate As Decimal
If IsNumeric(txtWeight.Text) Then
decWeight = Convert.ToDecimal(txtWeight.Text)
If decWeight <= 30 > 0 Then
If radContinental.Checked Then
decDestination = 1
ElseIf radHawaii.Checked Then
decDestination = 1.2
ElseIf radAlaska.Checked Then
decDestination = 1.26
End If
If decWeight <= 2 Then
decRate = 3.69
ElseIf decWeight <= 4 > 2 Then
decRate = 4.86
ElseIf decWeight <= 6 > 4 Then
decRate = 5.63
ElseIf decWeight <= 8 > 6 Then
decRate = 5.98
ElseIf decWeight <= 10 > 8 Then
decRate = 6.28
ElseIf decWeight <= 30 > 10 Then
decRate = 15.72
End If
decTotalCost = decRate * decDestination
lblTotalCost.Text = decTotalCost.ToString("C")
ElseIf decWeight <= 0 Then
MsgBox("Please Enter a Positive Weight.", , "Input Error")
ElseIf decWeight > 30 Then
MsgBox("You Have Entered " & decWeight.ToString() & ". Please Enter a Weight Under 30 Pounds", , "Input Error")
End If
ElseIf txtWeight.Text = "" Then
MsgBox("Please Enter the Weight", , "Input Error")
Else : MsgBox("Please Enter a Number", , "Input Error")
End If
You should try this if statement: If decWeight <= 30 and decWeight > 0 Then
This will check if the decWeight is less than or equal to 30 and make sure that it is 'non-zero' Hope this helps :-)

VB code not calculating

For some reason when I run this it only ever calculates to 0
Where am I going wrong? :(
The user has 3 input boxes to place values. From there those values should be calculating. It only ever equals a value of 0. I get no errors
Option Strict On
Public Class Form1
Private Sub btnCal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCal.Click
Dim dblPacA, dblPacB, dblPacC, dblAnswerA, dblAnswerB, dblAnswerC, dblGrandTotal As Double
Dim dblAnswerA1, dblAnswerB1, dblAnswerC1 As Double
'Packages Retail
Dim dblPACA_FACTOR As Double = 99
Dim dblPACB_FACTOR As Double = 199
Dim dblPACC_FACTOR As Double = 299
'Rate of each range
Dim dblTENNINE_FACTOR As Double = 0.8
Dim dblTWONINE_FACTOR As Double = 0.7
Dim dblFIVENINE_FACTOR As Double = 0.6
Dim dblONETEN_FACTOR As Double = 0.5
Try
'important calculate
dblAnswerA1 = dblPacA * dblPACA_FACTOR
dblAnswerB1 = dblPacB * dblPACB_FACTOR
dblAnswerC1 = dblPacC * dblPACC_FACTOR
dblPacA = CDbl(txtPacA.Text)
dblPacB = CDbl(txtPacB.Text)
dblPacC = CDbl(txtPacC.Text)
dblGrandTotal = dblAnswerA + dblAnswerB + dblAnswerC
lblGrandTotal.Text = "Gran Total:" & (dblGrandTotal.ToString("c"))
'lblAnswer.Text = dblAnswer.ToString
'lblAnswer.Text = "PackageA:" & dblAnswerA _
' & "PackageB:" & dblAnswerB & "PackageC:" _
'& dblAnswerC & "GrandTotal:" & dblGrandTotal
Catch
End Try
If dblPacA >= 0 Then
If dblPacA < 10 Then
dblAnswerA = dblAnswerA1
lblAnswerA.Text = "PackageA:" & dblAnswerA.ToString("c")
ElseIf dblPacA >= 10 And dblPacA < 20 Then
dblAnswerA = dblAnswerA1 * dblTENNINE_FACTOR
lblAnswerA.Text = "PackageA:" & (dblAnswerA.ToString("c"))
ElseIf dblPacA >= 20 And dblPacA < 50 Then
dblAnswerA = dblAnswerA1 * dblTWONINE_FACTOR
lblAnswerA.Text = "PackageA:" & dblAnswerA.ToString("c")
ElseIf dblPacA >= 50 And dblPacA < 100 Then
dblAnswerA = dblAnswerA1 * dblFIVENINE_FACTOR
lblAnswerA.Text = "PackageA:" & dblAnswerA.ToString("c")
ElseIf dblAnswerA >= 100 Then
dblAnswerA = dblAnswerA1 * dblONETEN_FACTOR
lblAnswerA.Text = "PackageA:" & dblAnswerA.ToString("c")
End If
Else
MessageBox.Show("txtPacA must be greater than or equal to 0", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
If dblPacB >= 0 Then
If dblPacB >= 10 And dblPacB <= 19 Then
dblAnswerB = dblAnswerB1 * dblTENNINE_FACTOR
lblAnswerB.Text = "PackageB:" & dblAnswerB.ToString("c")
ElseIf dblPacB >= 20 And dblPacB <= 49 Then
dblAnswerB = dblAnswerB1 * dblTWONINE_FACTOR
lblAnswerB.Text = "PackageB:" & dblAnswerB.ToString("c")
ElseIf dblPacB >= 50 And dblPacB <= 99 Then
dblAnswerB = dblAnswerB1 * dblFIVENINE_FACTOR
lblAnswerB.Text = "PackageB:" & dblAnswerB.ToString("c")
Else
dblAnswerB = dblAnswerB * dblONETEN_FACTOR
lblAnswerB.Text = "PackageB:" & dblAnswerB.ToString("c")
End If
Else
MessageBox.Show("txtPacB must be greater than or equal to 0", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
If dblPacC >= 0 Then
If dblPacC >= 10 And dblPacC <= 19 Then
dblAnswerC = dblAnswerC1 * dblTENNINE_FACTOR
lblAnswerC.Text = "PackageC:" & dblAnswerC.ToString("c")
ElseIf dblPacC >= 20 And dblPacA <= 49 Then
dblAnswerC = dblAnswerC1 * dblTWONINE_FACTOR
lblAnswerC.Text = "PackageC:" & dblAnswerC.ToString("c")
ElseIf dblPacC >= 50 And dblPacC <= 99 Then
dblAnswerC = dblAnswerC1 * dblFIVENINE_FACTOR
lblAnswerC.Text = "PackageC:" & dblAnswerC.ToString("c")
Else
dblAnswerC = dblAnswerC1 * dblONETEN_FACTOR
lblAnswerC.Text = "PackageC:" & dblAnswerC.ToString("c")
End If
Else
MessageBox.Show("txtPacC must be greater than or equal to 0", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
End Sub
End Class
dblAnswerA1 = dblPacA * dblPACA_FACTOR
where is dblPacA 's value set? its not. its 0