about message boxes

Joined
Jan 24, 2005
Messages
10
Programming Experience
Beginner
hey i wanna make a message box for my 3 text boxes so that when u go and input the amount it has to be in dollar form meaning u have to enter the $ first so how do u do the message box coding for that could someone help me out
i tried this coding but theres errors on the e.handled and e.keychar so id appreciate if someone could help this out for me thanks


Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim testInt As Integer
Select Case e.KeyChar
Case Microsoft.VisualBasic.ChrW(36)
If TextBox1.Text.Length = 1 Then
e.Handled = False
Else
e.Handled = True
End If
Case Microsoft.VisualBasic.ChrW(48) To Microsoft.VisualBasic.ChrW(57)
If TextBox1.Text.Length > 0 Then
e.Handled = False
Else
e.Handled = True
MessageBox.Show("Please enter a dollar value, starting with the '$' sign.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
 
I'd suggest instead of using Keypress you use lostfocus and just use the text.substring to work out if there's a $ etc

TPM
 
I struggled with code trying to verify input the way you are describing. As TPM suggests, it is best to use the lostfocus event.
 
Last edited:
Common practice is to validate input one time, not on every keypress. Also, why have them put the $ in? If it is to be a monetary figure, put a $ in a label just to the left of the textbox and leave the textbox input to be all numeric. Then you can do an easy conversion of the input to decimal format, rather than having to strip the $ out of the string. Here is what I mean:


With the $:
dim input as string = txtInput.text
input = input.substring (1,input.length)
if isnumeric(input) then
dim decInput as decimal = cdec(input)
else
Messagebox.Show("Bad input")
endif

Without the $:
if isnumeric(txtInput.text) then
dim decInput as decimal = cdec(txtInput.text)
else
Messagebox.Show("Bad input")
end if

Also, a suggestion: 3 months from now if you looked back at this code, would you remember what Microsoft.VisualBasic.ChrW(36) was? If you want to use these ChrW() lines, put comments in like:
'ChrW(36) is the $.
 
Back
Top