How to print out multiple If Statement in Visual Basic - vb.net

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.

Related

Why is the result of my program always underweight in BMI?

Here is my code:
Dim Weight, Height, Bmi_value As Integer
Weight = TextBox1.Text
Height = TextBox2.Text
Bmi_value = (Weight / Height ^ 2)
TextBox3.Text = Bmi_value
Select Case Bmi_value
Case 0.0 To 18.5
TextBox4.Text = "Underweight"
Case 18.6 To 24.9
TextBox4.Text = "Normal"
Case 25.0 To 29.9
TextBox4.Text = "Overweight"
Case Is >= 30.0
TextBox4.Text = "Obese"
End Select
End Sub
This is your code fixed:
'You were using Integer instead of Double
Dim Weight, Height, Bmi_value As Double
If you want to take the value from the textBoxes, you have to covert it to Double.
Weight = Convert.ToDouble(TextBox1.Text)
Height = Convert.ToDouble(TextBox2.Text)
'Better to call the right function Math.Pow()
Bmi_value = (Weight / Math.Pow(Height, 2))
'You have to convert it to String
TextBox3.Text = Convert.ToString(Bmi_value)
Select Case Bmi_value
Case 0.0 To 18.5
TextBox4.Text = "Underweight"
Case 18.6 To 24.9
TextBox4.Text = "Normal"
Case 25.0 To 29.9
TextBox4.Text = "Overweight"
Case Is >= 30.0
TextBox4.Text = "Obese"
End Select
End Sub
There was alot of error in this code:
First: don't declare var as integer if you need decimal value, like with the BMI calculation.
Second: always convert your value to the right type if you are getting them from TextBox or if you want to print them inside a TextBox
Dim dblWeight As Double, dblHeight As Double, dblBMI As Double
dblWeight = CDbl(TextBox1.Text) 'assumes lbs.
dblHeight = CDbl(TextBox2.Text) 'assumes inches
On Error GoTo 0
dblBMI = ((dblWeight * 703.0#) / (dblHeight * dblHeight))
TextBox3.Text = dblBMI
Select Case dblBMI
Case Is <= 18.5 : TextBox4.Text = "Underweight"
Case 18.6 To 24.9 : TextBox4.Text = "Normal"
Case 25.0# To 29.9 : TextBox4.Text = "Overweight"
Case 30.0# To 34.9 : TextBox4.Text = "Obese Class I"
Case 35.0# To 39.9 : TextBox4.Text = "Obese Class II"
Case Is >= 40.0# : TextBox4.Text = "Obese Class III"
End Select
Exit Sub
Try this, Works for me.

Wrong Calculation in Visual basic

Hi i have problem a wrong calculation in visual basic
when i input 1.5 all in the combo boxes then
the textbox4.textto textbox14.text are the units that has the values of 3,3,3,3,3,3,6,2,3 that is equal to 29.
I add all from textbox30.textto textbox22.text the divide them to the total units that is 29
the textbox31.text is equal to 1.51724137931034 but the correct value is 1.50
like this
TextBox30.Text = 3*1.5
TextBox29.Text = 3*1.5
TextBox28.Text = 3*1.5
TextBox27.Text = 3*1.5
TextBox26.Text = 3*1.5
TextBox25.Text = 3*1.5
TextBox24.Text = 6*1.5
TextBox23.Text = 2*1.5
TextBox22.Text = 3*1.5
a = 4.5 + 4.5 + 4.5 + 4.5 + 4.5 + 4.5 + 9 + 3 + 4.5
textbox31.text = a/29
heres the code
Dim a As Integer
TextBox30.Text = TextBox4.Text * ComboBox5.Text
TextBox29.Text = TextBox5.Text * ComboBox6.Text
TextBox28.Text = TextBox6.Text * ComboBox7.Text
TextBox27.Text = TextBox7.Text * ComboBox8.Text
TextBox26.Text = TextBox8.Text * ComboBox9.Text
TextBox25.Text = TextBox9.Text * ComboBox10.Text
TextBox24.Text = TextBox10.Text * ComboBox11.Text
TextBox23.Text = TextBox11.Text * ComboBox12.Text
TextBox22.Text = TextBox12.Text * ComboBox13.Text
a = TextBox30.Text + Val(TextBox29.Text) + Val(TextBox28.Text) + Val(TextBox27.Text) + Val(TextBox26.Text) + Val(TextBox25.Text) + Val(TextBox24.Text) + Val(TextBox23.Text) + Val(TextBox22.Text)
TextBox31.Text = (a / 29)
I don't know if it solve your problem, but it's always better to use a datatype like double for decimals. Integer is just for whole numbers.
So try Dim a As Double
a is an integer. a = 44. a/29 = 1.5172413793.
Can avoid some of these problems by ensuring option strict is always on.

If Statement inside Case Statement (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.

Loops - Adding Numbers - Visual Basic

So the program has to add all the numbers from "x" to "y".
But it also has to display all the numbers added :
i.e. 10 to 20 should display 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 = 165
Here's what I have:
Dim firstnum As Integer = Val(TextBox1.Text)
Dim secondnum As Integer = Val(TextBox2.Text)
Dim sum As Integer = 0
While firstnum <= secondnum
sum = sum + firstnum
firstnum = firstnum + 1
Label3.Text = firstnum & "+"
End While
suum.Text = " = " & Val(sum)
With the following:
Label3.Text = firstnum & "+"
You are overwriting the value in Label3 every time you go through the loop. What you probably want to do is concatenate the existing value with the next number.
This should get you on your way:
Label3.Text = Label3.Text & firstnum & " + "
Is Linq ok? Then you can use Enumerable.Range and Enumerable.Sum:
Dim startNum = Int32.Parse(TextBox1.Text)
Dim endNum = Int32.Parse(TextBox2.Text)
Dim numbers = Enumerable.Range(startNum, endNum - startNum + 1) 'inclusive, therefore + 1
Label3.Text = String.Join(" + ", numbers)
suum.Text = numbers.Sum()
your Label3.Text will only contain the last num and "+" at the end of the algorithm. You should replace
Label3.Text = firstnum & "+"
with
Label3.Text = Label3.Text & firstnum & "+ "

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