Results 1 to 18 of 18

Thread: [RESOLVED] [2008] Numbers only, No characters allowed

  1. #1

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Resolved [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.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: [2008] Numbers only, No characters allowed

    use tooltips + or restrict user input to integers.
    whats it for?

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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 -

  4. #4

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2008] Numbers only, No characters allowed

    Quote 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.

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2008] Numbers only, No characters allowed

    Quote 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 -

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2008] Numbers only, No characters allowed

    Quote 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.

  7. #7

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2008] Numbers only, No characters allowed

    Quote 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.

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2008] Numbers only, No characters allowed

    do you want just integer numbers, or are you allowing decimals?

  9. #9

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2008] Numbers only, No characters allowed

    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.
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

  10. #10
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    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.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  11. #11
    Member GeekInOhio's Avatar
    Join Date
    Jun 2008
    Location
    You'll never guess...
    Posts
    56

    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:
    1. Function illegalCharacterCheck(ByVal testMe As String) As Boolean
    2.  
    3. Dim errorCount as Integer = 0
    4.  
    5.                    Dim acceptable As String = "0123456789."
    6.             Dim i As Integer
    7.             Dim letter As String = ""
    8.             For i = 0 To testMe.Length - 1
    9.                 letter = testMe.Chars(i)
    10.                 If InStr(acceptable, letter) = 0 Then
    11.                     errorCount = errorCount + 1
    12.                 Else
    13.                    
    14.                 End If
    15.  
    16.             Next    
    17.  
    18.             If errorCount > 0
    19.  
    20.             Return TRUE
    21.  
    22.             Else
    23.  
    24.             Return False
    25.            
    26.             End If
    27. 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:
    1. If illegalCharacterCheck(myText.Text) = True Then
    2.             InvalidLabel1.Visible = True
    3.             myText.Focus()
    4.             myText.BackColor = Color.Yellow
    5.         Else
    6.             InvalidLabel1.Visible = False
    7.             myText.BackColor = Color.Empty
    8.         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:
    1. ...
    2.  Dim unacceptable As String = "0123456789!@#$%^&*()<>,.;:'[]{}+=_"""
    3.             Dim i As Integer
    4.             Dim letter As String = ""
    5.             For i = 0 To testMe.Length - 1
    6.                 letter = testMe.Chars(i)
    7.                 If InStr(unacceptable, letter) > 0 Then
    8.                     errorCount = errorCount + 1
    9.                 Else
    10.                    
    11.                 End If
    12.             Next
    13. ...

    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

  12. #12
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    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.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  13. #13
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2008] Numbers only, No characters allowed

    Quote 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 -

  14. #14

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2008] Numbers only, No characters allowed

    Quote Originally Posted by GeekInOhio
    There are probably more elegant ways to do this, but here is what I came up with...

    vb Code:
    1. Function illegalCharacterCheck(ByVal testMe As String) As Boolean
    2.  
    3. Dim errorCount as Integer = 0
    4.  
    5.                    Dim acceptable As String = "0123456789."
    6.             Dim i As Integer
    7.             Dim letter As String = ""
    8.             For i = 0 To testMe.Length - 1
    9.                 letter = testMe.Chars(i)
    10.                 If InStr(acceptable, letter) = 0 Then
    11.                     errorCount = errorCount + 1
    12.                 Else
    13.                    
    14.                 End If
    15.  
    16.             Next    
    17.  
    18.             If errorCount > 0
    19.  
    20.             Return TRUE
    21.  
    22.             Else
    23.  
    24.             Return False
    25.            
    26.             End If
    27. 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:
    1. If illegalCharacterCheck(myText.Text) = True Then
    2.             InvalidLabel1.Visible = True
    3.             myText.Focus()
    4.             myText.BackColor = Color.Yellow
    5.         Else
    6.             InvalidLabel1.Visible = False
    7.             myText.BackColor = Color.Empty
    8.         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:
    1. ...
    2.  Dim unacceptable As String = "0123456789!@#$%^&*()<>,.;:'[]{}+=_"""
    3.             Dim i As Integer
    4.             Dim letter As String = ""
    5.             For i = 0 To testMe.Length - 1
    6.                 letter = testMe.Chars(i)
    7.                 If InStr(unacceptable, letter) > 0 Then
    8.                     errorCount = errorCount + 1
    9.                 Else
    10.                    
    11.                 End If
    12.             Next
    13. ...

    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.

  15. #15
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    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

  16. #16

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2008] Numbers only, No characters allowed

    Quote 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.

  17. #17
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: [2008] Numbers only, No characters allowed

    came across This smaple hope it will help.
    __________________
    Rate the posts that helped you

  18. #18

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    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
  •  



Click Here to Expand Forum to Full Width