Avoiding Keyboard Errors

  • Thread starter Thread starter cmdolcet69
  • Start date Start date
C

cmdolcet69

Is there somthing I can use for a text box control that will help
avoid my users of entering "stupid" data. In many cases our user will
enter number such as 10.0 or 0.50 or even 0,45 however if my user
enter ..045 or ,,045 how can i control this and prompt him to re-
enter. I thought of using the replace function however I don;t know
what direction to take with this. Could someone please point me on the
right track???
 
Is there somthing I can use for a text box control that will help
avoid my users of entering "stupid" data. In many cases our user will
enter number such as 10.0 or 0.50 or even 0,45 however if my user
enter ..045 or ,,045 how can i control this and prompt him to re-
enter. I thought of using the replace function however I don;t know
what direction to take with this. Could someone please point me on the
right track???

Hi,
Replace function can be used, in this sample you may correct incorrect
user entries that are done with " . " instead of
" , ":

If TextBox1.Text.Contains(".") = True Then
TextBox1.Text = TextBox1.Text.Replace(".", ",")
End If
End Sub
 
Well, the Textbox class has a number of events you can hook, such as Keyup,
Keypress, etc. You could listen to these and handle them appropriately to
validate your data, or even try to replace it.

If you want to replace duplicate chars you can do something like

do while mystring.IndexOf(",,") > -1
mystring = Replace(mystring, ",,", ",")
loop

You could probably do the same thing somehow with RegExs, but I wouldn't be
able to tell you how to do that.

Also, it's been a while since I've written WinForms code (vs. WebForms). Do
WinForms have "validator" controls, like webforms?
Could use those to prompt the user if they exist.
 
Is there somthing I can use for a text box control that will help
avoid my users of entering "stupid" data. In many cases our user will
enter number such as 10.0 or 0.50 or even 0,45 however if my user
enter ..045 or ,,045 how can i control this and prompt him to re-
enter. I thought of using the replace function however I don;t know
what direction to take with this. Could someone please point me on the
right track???

You should be validating all (yes I mean all) data from the users to
make sure they didn't enter bad data. One way of doing this is to use
Regex or something similar to validate format. Another method would be
to take advantage of <Type>.TryParse() to see if the string that the
user entered can even be converted.

For example (typed in message):

//////////////////
Dim count As Integer

If Integer.TryParse(myTextBox.Text, count) Then
MessageBox.Show("count '" & count & "' is good data")
Else
MessageBox.Show("Stupid user! Enter in an actual number!")
End If
//////////////////

Remember, all your users are stupid (again, I mean all), you should
never ever trust them. Some users might not be stupid in the classical
sense, but they will, at some point, make a stupid mistake. This is a
fact that developers must learn to live with, as you can never assume
competence from your users.

And if one my users is reading this, then don't take it personally,
I'm sure you all fall under the "accidentally made a stupid mistake"
category of stupid users. :-)

Thanks,

Seth Rowe [MVP]
 
Is there somthing I can use for a text box control that will help
avoid my users of entering "stupid" data. In many cases our user will
enter number such as 10.0 or 0.50 or even 0,45 however if my user
enter ..045 or ,,045 how can i control this and prompt him to re-
enter. I thought of using the replace function however I don;t know
what direction to take with this. Could someone please point me on the
right track???

Also you can do this for inserting automatically if user forgets
typing comma after "first" digit. Note that you can do the same for
inserting comma after second or third digit depends on the length of
number:

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles TextBox1.TextChanged
Try
If Not TextBox1.Text.Contains(",") = True Then

'Change index value "1" to insert comma
'after desired digit
TextBox1.Text = TextBox1.Text.Insert(1, ",")

'Continue typing, focus next char
TextBox1.SelectionStart = 2

End If

Catch
'Only clearing textbox brings exception.
'So just ignore it.

End Try
End Sub
 
Is there somthing I can use for a text box control that will help
avoid my users of entering "stupid" data. In many cases our user will
enter number such as 10.0 or 0.50 or even 0,45 however if my user
enter ..045 or ,,045 how can i control this and prompt him to re-
enter. I thought of using the replace function however I don;t know
what direction to take with this. Could someone please point me on the
right track???

I also should have mentioned you can use the MaskedEditTextBox to
restrict what data can be entered.

Thanks,

Seth Rowe [MVP]
 
cmdolcet69 said:
Is there somthing I can use for a text box control that will help
avoid my users of entering "stupid" data. In many cases our user will
enter number such as 10.0 or 0.50 or even 0,45 however if my user
enter ..045 or ,,045 how can i control this and prompt him to re-
enter.

\\\
Private Sub TextBox1_Validating( _
ByVal sender As Object, _
ByVal e As CancelEventArgs _
) Handles TextBox1.Validating
Dim SourceControl As TextBox = DirectCast(sender, TextBox)
Dim n As Double
If Not Double.TryParse(SourceControl.Text, n) Then
Me.ErrorProvider1.SetError( _
SourceControl, _
"Value must be a double." _
)
Else
If Me.ErrorProvider1.GetError(SourceControl).Length > 0 Then
Me.ErrorProvider1.SetError(SourceControl, String.Empty)
End If
...
End If
End Sub
///
 
\\\
Private Sub TextBox1_Validating( _
ByVal sender As Object, _
ByVal e As CancelEventArgs _
) Handles TextBox1.Validating
Dim SourceControl As TextBox = DirectCast(sender, TextBox)
Dim n As Double
If Not Double.TryParse(SourceControl.Text, n) Then
Me.ErrorProvider1.SetError( _
SourceControl, _
"Value must be a double." _
)
Else
If Me.ErrorProvider1.GetError(SourceControl).Length > 0 Then
Me.ErrorProvider1.SetError(SourceControl, String.Empty)
End If
...
End If
End Sub
///

Ok I posted the code for a better explanation: This code gets called
every keyboard entry.



Public Sub Runtime_Keypress(ByVal sender As Object, ByVal e As
KeyPressEventArgs)
Try
Dim gageValues() As String
gageValues = CStr(CType(sender, Label).Tag).Split(",")

If gageValues(0) = "Keyboard" Then
If e.KeyChar = Chr(8) Then 'This is the backspace
check
If
CType(Me.readingLabelArrayList(sender.tabindex), Label).Text.Length >
0 Then

CType(Me.readingLabelArrayList(sender.tabindex), Label).Text =
CType(Me.readingLabelArrayList(sender.tabindex),
Label).Text.Substring(0,
CType(Me.readingLabelArrayList(sender.tabindex), Label).Text.Length -
1)
End If
ElseIf e.KeyChar = vbCr Then

mNewRuntimeMethods.calcOperations.PerformCalculations(mNewRuntimeMethods.tmpSavedInfo,
readingLabelArrayList, pFile, mNewRuntimeMethods.intCurrentPartNumber,
mNewRuntimeMethods.boolMinMaxStartButtonBeenPressed)
mNewRuntimeMethods.SampleData()
CType(Me.readingLabelArrayList(sender.tabindex),
Label).BorderStyle = BorderStyle.FixedSingle
mNewRuntimeMethods.boolKeyboardSampled = True
Else
If e.KeyChar = Chr(45) Or e.KeyChar = Chr(46) Or
(e.KeyChar >= Chr(48) And e.KeyChar <= Chr(57)) Then

CType(Me.readingLabelArrayList(sender.tabindex), Label).Text +=
e.KeyChar
End If
End If
Else
e.Handled = True
End If
Catch ex As Exception
tListener.AddMethodError(ex)
End Try
End Sub
 
One easy one is to do an IsNumeric test on the text each time it is
changed. Simplistic, but it works.

But that's just my opinion...

Mike

If IsNumeric(txtTZ.Text) Then
If txtTZ.Text < -12 Or txtTZ.Text > 12 Then
MsgBox("Timezone entry must be from -12 to +12, inclusive")
Exit Sub
End If
Else
MsgBox("Time zone entry must numeric.")
Exit Sub
End If
gdTZ = txtTZ.Text
SaveSetting(My.Application.Info.AssemblyName, "Settings", _
"UserTZ", gdTZ)
 
Back
Top