im trying to write a program to calculate BMI for practice. I have it working for the most part but im having trouble with 1 thing. I want to say if its any letter or not a number then display an error message. Here is the code that I need help with. How do I make it work?

ElseIf (ComboBox2.Text) = ???? Then
MsgBox("ERROR2: Please Select Your Height In The Inches Box.")

theres more than that but this is the only part giving me trouble

Dani AI

Generated

wanted a way to show an error when the inches box contains letters or otherwise is not a valid number. As noted, Visual Basic provides routines for numeric checks, but the most reliable pattern is: 1) check for empty input, 2) attempt a typed parse (so the value is returned as a number), and 3) enforce the valid range for inches (0 through 11). Integer.TryParse avoids ambiguous conversions and exceptions and is preferred over loose tests.

Try this pattern for validating the inches ComboBox input:

Dim txt As String = ComboBox2.Text.Trim()
Dim inches As Integer

If txt = String.Empty Then
    MsgBox("ERROR: Enter inches (0-11).")
ElseIf Not Integer.TryParse(txt, inches) Then
    MsgBox("ERROR: Inches must be a whole number.")
ElseIf inches < 0 Or inches > 11 Then
    MsgBox("ERROR: Inches must be between 0 and 11.")
Else
    ' Valid inches value — proceed to combine with feet and calculate BMI.
End If

For a simpler, more foolproof UI, prefill the ComboBox with the strings "0" through "11" and set its style so the user cannot type arbitrary text, or use a NumericUpDown control limited to 0..11. Example to populate the ComboBox:

For i As Integer = 0 To 11
    ComboBox2.Items.Add(i.ToString())
Next
ComboBox2.DropDownStyle = ComboBoxStyle.DropDownList

References: Integer.TryParse documentation (examples and behavior) can help implement robust parsing: Integer.TryParse documentation.

IsNumeric Function: IsNumeric returns True if the data type of Expression is Boolean, Byte, Decimal, Double, Integer, Long, SByte, Short, Single, UInteger, ULong, or UShort, or an Object that contains one of those numeric types. It also returns True if Expression is a Char or String that can be successfully converted to a number.

IsNumeric returns False if Expression is of data type Date or of data type Object and it does not contain a numeric type. IsNumeric returns False if Expression is a Char or String that cannot be converted to a number.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.