How would you let a user know that alpha characters are not allowed and only integers?
Printable View
How would you let a user know that alpha characters are not allowed and only integers?
use tooltips + or restrict user input to integers.
whats it for?
Have a label saying so sitting right next to the control of interest :)
If the control is a textbox, I'd change it and use a numericupdown control. That way, the user can't type in non-numeric values.
It's for data in a datagridview. Only numbers should be allowed. How can I restrict their input to integers?Quote:
Originally Posted by .paul.
You would handle one of the keyboard event of the DataGridViewTextBoxEditingControl, and perform your test to see if the character being typed in is numeric.Quote:
Originally Posted by onlyGirl
I wouldn't restrict their keyboard input, I would just validate it as they type using the textchanged event. The problem with validating on the keyboard input is that you leave the door open for things like a right click/paste of something they copied to the clipboard. It won't be caught by a keyevent, and you end up with alpha characters in your numbers only box.Quote:
Originally Posted by stanav
Likewise you don't want to disallow copy/paste functionality, because maybe what they are pasting really is all numbers, and its convenient for them to do so.
That is why textchanged is the best place to validate as they type.
Could you give me a quick example please?Quote:
Originally Posted by kleinma
do you want just integer numbers, or are you allowing decimals?
Well the datatype in the db is double. So yes decimals.Quote:
Originally Posted by kleinma
Why not use the NumericUpDown control for this, much easier than modifying a textbox since the NUD was built for this.
There are probably more elegant ways to do this, but here is what I came up with...
vb Code:
Function illegalCharacterCheck(ByVal testMe As String) As Boolean Dim errorCount as Integer = 0 Dim acceptable As String = "0123456789." Dim i As Integer Dim letter As String = "" For i = 0 To testMe.Length - 1 letter = testMe.Chars(i) If InStr(acceptable, letter) = 0 Then errorCount = errorCount + 1 Else End If Next If errorCount > 0 Return TRUE Else Return False End If End Function
To use it, you call illegalCharacterCheck() with the string you want to test. It returns a Boolean of True or False, with TRUE meaning we have an illegal character.
I use in on textchanged events and handle a TRUE bypulling focus back to the textbox, cell, etc, changing the background color to yellow, lighting up an invalid character(s) lablel, etc.
Example:
vb Code:
If illegalCharacterCheck(myText.Text) = True Then InvalidLabel1.Visible = True myText.Focus() myText.BackColor = Color.Yellow Else InvalidLabel1.Visible = False myText.BackColor = Color.Empty End If
You could also disable, enable the submit/ok button, etc.
Incidentally, you can make a minor modification to the above and use it to test for characters you don't want:
vb Code:
... Dim unacceptable As String = "0123456789!@#$%^&*()<>,.;:'[]{}+=_""" Dim i As Integer Dim letter As String = "" For i = 0 To testMe.Length - 1 letter = testMe.Chars(i) If InStr(unacceptable, letter) > 0 Then errorCount = errorCount + 1 Else End If Next ...
Hope it helps.
One thing I don't get is why do people repeatedly modify the TextBox control to do things that other controls where designed to do.
It seems like everyone loves to do more work than needed.
It's a DataGridView control, and the DGV doesn't have a DataGridViewNumericUpDownColumn out of the box. That's why people try to work with the DataGridViewTextBoxColumn, which uses the DataGridViewTextBoxEditingControl as the editing control.Quote:
Originally Posted by JuggaloBrotha
How would you put the focus back on the cell of the dgv?Quote:
Originally Posted by GeekInOhio
may be you can try using DataGridViewTextBoxEditingControl, e.g
Code:Private txtNumeric As DataGridViewTextBoxEditingControl
Private Sub dataGridView1_EditingControlShowing(ByVal...)
txtNumeric = CType(e.Control, DataGridViewTextBoxEditingControl)
addhandler txtNumeric.TextChanged addressof txtNumeric_TextChanged
End Sub
Private Sub txtNumeric_TextChanged(ByVal...)
if not IsNumeric(txtNumeric.Text)
messagebox.show("Invalid data")
txtNumeric.focus()
End if
End Sub
It says e is not declared.Quote:
Originally Posted by riteshjain1982
came across This smaple hope it will help.
Thanks!! I used a combination of GeekinOhio and riteshjain1982 example.
Code:Private Sub dgv_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If e.Control.GetType.ToString() = "System.Windows.Forms.DataGridViewTextBoxEditingControl" Then
Dim c As DataGridViewTextBoxEditingControl = DirectCast(e.Control, DataGridViewTextBoxEditingControl)
RemoveHandler c.TextChanged, AddressOf edtctrlOnTextChanged
AddHandler c.TextChanged, AddressOf edtctrlOnTextChanged
End If
editingValue = e.Control.Text
End Sub
Private Sub edtctrlOnTextChanged(ByVal sender As Object, ByVal ea As EventArgs)
Dim acceptable As String = "0123456789."
Dim i As Integer
Dim letter As String
Dim errorCount As Integer = 0
Dim ctrl As DataGridViewTextBoxEditingControl = DirectCast(sender, DataGridViewTextBoxEditingControl)
For i = 0 To ctrl.Text.Length - 1
letter = ctrl.Text.Chars(i)
If InStr(acceptable, letter) = 0 Then
errorCount = errorCount + 1
End If
If errorCount > 0 Then
'MessageBox.Show("invalid characters")
hasInvalidChar = True
End If
Next
End Sub
Private Sub DataGridView1_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
If hasInvalidChar = True Then
MessageBox.Show("There are invalid characters in the field. Please enter numeric values.", "ReservoirGrail", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub