Question:
How do I make an error appear if a letter is entered in a number text box? Vb.net?
anonymous
2011-10-28 01:41:37 UTC
I've written a carpet program in VB.net which calculates the amount of carpet required to cover rooms between 5x5 meters and 20x20 meters. Once width and length of the room have been entered the following calculations are made and displayed in text boxes:

1) how many meters of carpet are needed (based on room dimensions)
2) how many rolls of carpet are required
3) how many meters of carpet are unused and left over

The program works fine however I want an error label to be displayed when a value lower than 5 or higher than 20 is entered in the width/length text boxes and the calculate button is clicked. I want the same error label to appear if a letter or symbol is entered in the width/length text boxes and the calculate button is clicked. What code would make this happen? Here’s my code so far:

Public Class Form1

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim width, length As Integer
If IsValidInput(txtWidth.Text) Then
width = txtWidth.Text
End If
If IsValidInput(txtLength.Text) Then
length = txtLength.Text
End If
CalculateAndDisplay(width, length)
End Sub

Sub CalculateAndDisplay(ByVal width, ByVal length)

Dim strips, totalLength, rolls, carpetLeft As Integer
Const rollwidth = 5
Const rollLength = 30

strips = width \ rollwidth

If width Mod 5 > 0 Then
strips = strips + 1
End If

totalLength = strips * length
rolls = totalLength \ rollLength

If CDbl(totalLength / rollLength) > CDbl(rolls) Then
rolls = rolls + 1
End If

carpetLeft = (rollLength * rolls) - totalLength
txtMetersRequired.Text = totalLength.ToString()
txtRollsRequired.Text = rolls.ToString()
txtWidthRemaining.Text = carpetLeft.ToString()

End Sub

End Class
Five answers:
texasmaverick
2011-10-28 05:04:46 UTC
Revise the first paragraph of your code as follows.





Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

Dim width, length As Integer



Width = Val(txtWidth.Text)

Length= Val(txtLength.Text)

If Width<5 or Width>20 or Length<5 or Length>20 then

MsgBox("You have entered an invalid length or width. ")

Exit Sub

End If



If IsNumeric(txtWidth.Text) = False Or IsNumeric(txtLength.Text) = False Then

MsgBox("You have entered a non number for width or length")

Exit Sub

End If



CalculateAndDisplay(width, length)



'the rest of your code is OK



TexMav
AnalProgrammer
2011-10-28 09:18:12 UTC
This code is where you need to make your changes.

If IsValidInput(txtWidth.Text) Then

width = txtWidth.Text

End If

If IsValidInput(txtLength.Text) Then

length = txtLength.Text

End If

CalculateAndDisplay(width, length)



I am assuming that IsValidInput() is a function that checks for characters.

This function should also put the text in the label field.



But then you ruin things by always calling the CalculateAndDisplay subroutine.



Try this.

If IsValidInput(txtWidth.Text) Then

width = txtWidth.Text

If IsValidInput(txtLength.Text) Then

length = txtLength.Text

CalculateAndDisplay(width, length)

End If

End If



Now both fields must be valid before the calculate sub is called.

But as IsValidInput is a VB function why not write your own version



If IsValidWidth(txtWidth.Text) Then

width = txtWidth.Text

If IsValidLength(txtLength.Text) Then

length = txtLength.Text

CalculateAndDisplay(width, length)

End If

End If



Have fun.
?
2011-10-28 17:52:14 UTC
The best way to handle illegal characters such as letters is to prevent the user from entering them in the first place. This is called key validation and it uses the keyPress event for the text box.





How this works is whenever a key press is made in the textbox the keypress event fires and looks at what individual key was pressed. If it is a valid numeric entry or edit then nothing is done and we allow the key.. however if its a letter or other character we do not wnat then we cancel the key by setting it to a null and follow that by setting a handled flag to true.



If you want you can add a message box to tell the user that only numeric entries are allowed. I kept the example below as simple as possible adnd did not include any kind of messaging.



Since your dealing with just two textboxes for user input its easy enough to duplicate this code in two keypress event handlers. One for each text box.







Private Sub txtWidth_KeyPress(sender As System.Object, _

e As System.Windows.Forms.KeyPressEventArgs) _

Handles txtWidth.KeyPress



Dim UserKeyInput As Char



UserKeyInput = e.KeyChar



Select Case UserKeyInput



Case "0" To "9", ".", Chr(Keys.Back)

'allowed character inputs (Numbers, decimal point and backspace)

'do nothing

Case Else

'illegal input - cancel the users input

e.KeyChar = ""

e.Handled = True

End Select







End Sub









Data Validation:



Once a number has been entered into the text boxes you can perform data validation when you click the button. Basically copy the numeric strings in the textboxes into daoble data type variables and then test to see if they are with in your valid range with a couple of if statements. If they fail by being either too big or too small then show a message and exit the button subroutine (exit sub) to abort any further processing.
anonymous
2011-10-28 09:05:18 UTC
Something like this should do it:



Dim width as Integer

Dim height as Integer

Integer.TryParse(txtWidth.Text, width)

Integer.TryParse(txtHeight.Text, height)



If(width < 5 Or width > 20)

MessageBox.Show("Width Error")

End If



If(height < 5 Or height > 20)

MessageBox.Show("Height Error")

End If



Hope I could make this technique clear enough.
AJ
2011-10-28 14:26:38 UTC
a simplier way is to use the textbox's keypress event. In there you set it that only number keys, the del key, backspace and period can do anything. Pressing any other key does nothing.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...