|
-
Jun 27th, 2008, 01:57 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] [2008] Numbers only, No characters allowed
How would you let a user know that alpha characters are not allowed and only integers?
Using Visual Studio 2008
Please mark your thread RESOLVED if you no longer need help.
-
Jun 27th, 2008, 01:59 PM
#2
Re: [2008] Numbers only, No characters allowed
use tooltips + or restrict user input to integers.
whats it for?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 27th, 2008, 02:00 PM
#3
Re: [2008] Numbers only, No characters allowed
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.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Jun 27th, 2008, 02:26 PM
#4
Thread Starter
Fanatic Member
Re: [2008] Numbers only, No characters allowed
 Originally Posted by .paul.
use tooltips + or restrict user input to integers.
whats it for?
It's for data in a datagridview. Only numbers should be allowed. How can I restrict their input to integers?
Using Visual Studio 2008
Please mark your thread RESOLVED if you no longer need help.
-
Jun 27th, 2008, 02:30 PM
#5
Re: [2008] Numbers only, No characters allowed
 Originally Posted by onlyGirl
It's for data in a datagridview. Only numbers should be allowed. How can I restrict their input to integers?
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.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Jun 27th, 2008, 02:37 PM
#6
Re: [2008] Numbers only, No characters allowed
 Originally Posted by stanav
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.
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.
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.
-
Jun 27th, 2008, 02:41 PM
#7
Thread Starter
Fanatic Member
Re: [2008] Numbers only, No characters allowed
 Originally Posted by kleinma
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.
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?
Using Visual Studio 2008
Please mark your thread RESOLVED if you no longer need help.
-
Jun 27th, 2008, 02:42 PM
#8
Re: [2008] Numbers only, No characters allowed
do you want just integer numbers, or are you allowing decimals?
-
Jun 27th, 2008, 02:42 PM
#9
Thread Starter
Fanatic Member
Re: [2008] Numbers only, No characters allowed
 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.
Using Visual Studio 2008
Please mark your thread RESOLVED if you no longer need help.
-
Jun 27th, 2008, 10:08 PM
#10
Re: [2008] Numbers only, No characters allowed
Why not use the NumericUpDown control for this, much easier than modifying a textbox since the NUD was built for this.
-
Jun 28th, 2008, 08:02 AM
#11
Member
Re: [2008] Numbers only, No characters allowed
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.
In case I forget: I'm using Visual Basic 2008 Express Edition...
Should I, in my odd, bumbling way, actually offer some assistance, feel free to show me some RATE love.
Just Another Laptop Hero
-
Jun 28th, 2008, 10:47 AM
#12
Re: [2008] Numbers only, No characters allowed
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.
-
Jun 28th, 2008, 05:33 PM
#13
Re: [2008] Numbers only, No characters allowed
 Originally Posted by JuggaloBrotha
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.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Jun 30th, 2008, 10:09 AM
#14
Thread Starter
Fanatic Member
Re: [2008] Numbers only, No characters allowed
 Originally Posted by GeekInOhio
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.
How would you put the focus back on the cell of the dgv?
Using Visual Studio 2008
Please mark your thread RESOLVED if you no longer need help.
-
Jun 30th, 2008, 10:32 AM
#15
Re: [2008] Numbers only, No characters allowed
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
__________________
Rate the posts that helped you 
-
Jun 30th, 2008, 10:52 AM
#16
Thread Starter
Fanatic Member
Re: [2008] Numbers only, No characters allowed
 Originally Posted by riteshjain1982
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.
Using Visual Studio 2008
Please mark your thread RESOLVED if you no longer need help.
-
Jun 30th, 2008, 10:55 AM
#17
Re: [2008] Numbers only, No characters allowed
came across This smaple hope it will help.
__________________
Rate the posts that helped you 
-
Jun 30th, 2008, 12:38 PM
#18
Thread Starter
Fanatic Member
Re: [2008] Numbers only, No characters allowed
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
Using Visual Studio 2008
Please mark your thread RESOLVED if you no longer need help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|