locked
KeyPress Validation with decimal. RRS feed

  • Question

  • I am Currently using this code for validation of a textbox..

    Private Sub EximTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
            Select Case m_ETMasktype
                Case validationtypes.numbers
                    If (Microsoft.VisualBasic.Asc(e.KeyChar) < 48) Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 57) Then
                        e.Handled = True
                    End If
                    If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
                        e.Handled = False
                    End If
                Case validationtypes.Decimals
                    If Not Char.IsControl(e.KeyChar) AndAlso Not Char.IsDigit(e.KeyChar) AndAlso e.KeyChar <> "." Then
                        e.Handled = True
                    Else
                        If e.KeyChar = "." And Me.Text.IndexOf(".") <> -1 Then
                            e.Handled = True
                        ElseIf e.KeyChar = "." Then
                            e.Handled = False
                        ElseIf Char.IsDigit(e.KeyChar) Then
                            If Me.Text.IndexOf(".") <> -1 Then
                                If Me.Text.Length >= Me.Text.IndexOf(".") + (pointdecimal + 1) Then  'replace 2 for greater numbers after decimal point
                                    e.Handled = True
                                End If
                            End If
                        End If
                    End If
                Case validationtypes.UpperCase
                    If Char.IsLower(e.KeyChar) Then
                        e.Handled = True
                        SendKeys.Send(Char.ToUpper(e.KeyChar))
                    End If
            End Select
    
        End Sub

    now my textbox is set to select all text on focus " Me.SelectAll()"

    now issue is when there is a data in text box with decimal and all data is already selected, i can not enter new figure before deleting the existing data.

    all i am trying to is enter data when old date is selected.

    when there is no decimal it automatically delete the existing data and i can strait away insert the data

    I hope i am able to explain my issue.

    Please help..


    Pradeep Yadav (Social MSDN)

    Tuesday, October 16, 2018 2:04 PM

Answers

  • See if this help you Check TextBox SelectionStart 

       If Not Char.IsDigit(e.KeyChar) And Not e.KeyChar = "." Then
                e.Handled = True
            Else
                If e.KeyChar = "." And MyTextBox1.Text.IndexOf(".") <> -1 Then
                    e.Handled = True
                ElseIf e.KeyChar = "." Then
                    e.Handled = False
                ElseIf Char.IsDigit(e.KeyChar) Then
                    If MyTextBox1.Text.IndexOf(".") <> -1 Then
                        If MyTextBox1.SelectionStart > MyTextBox1.Text.IndexOf(".") And MyTextBox1.Text.Split(".")(1).Length >= 2 Then
                            e.Handled = True
                        End If
                    End If
                End If
            End If

    • Edited by Tech Aspirant Wednesday, October 17, 2018 7:37 AM
    • Marked as answer by pk.yadav Wednesday, October 17, 2018 8:01 AM
    Wednesday, October 17, 2018 7:26 AM

All replies

  • To recreate the issue what I have done in my case is created a class Inherits Textbox with following code then dragged MyTextbox from toolbox on form

    Then on Keypress added code of ValidatioTypes.Decimal

    But I am not able to regenerate the issue can you please tell me how can I re create ? Is I am doing something wrong ?

    Imports System
    Imports System.Windows.Forms
    
    Public Class MyTextBox
        Inherits TextBox
    
        Private alreadyFocused As Boolean
    
        Protected Overrides Sub OnLeave(ByVal e As EventArgs)
            MyBase.OnLeave(e)
    
            Me.alreadyFocused = False
    
        End Sub
    
        Protected Overrides Sub OnGotFocus(ByVal e As EventArgs)
            MyBase.OnGotFocus(e)
    
            ' Select all text only if the mouse isn't down.
            ' This makes tabbing to the textbox give focus.
            If MouseButtons = MouseButtons.None Then
    
                Me.SelectAll()
                Me.alreadyFocused = True
    
            End If
    
        End Sub
    
        Protected Overrides Sub OnMouseUp(ByVal mevent As MouseEventArgs)
            MyBase.OnMouseUp(mevent)
    
            ' Web browsers like Google Chrome select the text on mouse up.
            ' They only do it if the textbox isn't already focused,
            ' and if the user hasn't selected all text.
            If Not Me.alreadyFocused AndAlso Me.SelectionLength = 0 Then
    
                Me.alreadyFocused = True
                Me.SelectAll()
    
            End If
    
        End Sub
    
    End Class

    Private Sub MyTextBox1_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles MyTextBox1.KeyPress
            If Not Char.IsControl(e.KeyChar) AndAlso Not Char.IsDigit(e.KeyChar) AndAlso e.KeyChar <> "." Then
                e.Handled = True
            Else
                If e.KeyChar = "." And Me.Text.IndexOf(".") <> -1 Then
                    e.Handled = True
                ElseIf e.KeyChar = "." Then
                    e.Handled = False
                ElseIf Char.IsDigit(e.KeyChar) Then
                    If Me.Text.IndexOf(".") <> -1 Then
                        If Me.Text.Length >= Me.Text.IndexOf(".") Then  'replace 2 for greater numbers after decimal point
                            e.Handled = True
                        End If
                    End If
                End If
            End If
    
        End Sub

     
    Tuesday, October 16, 2018 5:47 PM
  • Instead of such sophisticated manipulations (and you also have to take into consideration the Paste from Clipboard case), maybe you should not limit the user’s freedom, but validate the string when the user presses OK, Save, Submit, etc.


    Tuesday, October 16, 2018 8:10 PM
  • ok, first of all, sorry but issue is entirely different.

    Actual Issue : when i restrict the digits after decimal to a certain digit let say 2 and as soon as 2 digits are entered

    my textbox stop excepting any key.

    Now all i want, If Caret is before decimal it should except the key press

    and only in case of when Caret is after decimal it should check the digit after decimal to accept or not.



    Pradeep Yadav (Social MSDN)

    Wednesday, October 17, 2018 7:07 AM
  • See if this help you Check TextBox SelectionStart 

       If Not Char.IsDigit(e.KeyChar) And Not e.KeyChar = "." Then
                e.Handled = True
            Else
                If e.KeyChar = "." And MyTextBox1.Text.IndexOf(".") <> -1 Then
                    e.Handled = True
                ElseIf e.KeyChar = "." Then
                    e.Handled = False
                ElseIf Char.IsDigit(e.KeyChar) Then
                    If MyTextBox1.Text.IndexOf(".") <> -1 Then
                        If MyTextBox1.SelectionStart > MyTextBox1.Text.IndexOf(".") And MyTextBox1.Text.Split(".")(1).Length >= 2 Then
                            e.Handled = True
                        End If
                    End If
                End If
            End If

    • Edited by Tech Aspirant Wednesday, October 17, 2018 7:37 AM
    • Marked as answer by pk.yadav Wednesday, October 17, 2018 8:01 AM
    Wednesday, October 17, 2018 7:26 AM