Len čísla v TextBoxe   otázka

VB.NET

Mohol by mi niekto poradiť konkrétny príklad ako mám riešiť zadávanie čísel do text boxu. Chcem totiž aby do TB nebolo možné zadávať iné znaky ako čísla.

V súčasnosti to riešim takým spôsobom že pri konvertovaní hodnoty na číselnú premennú sa vypíše msgBox o chybe ak sa text nedá skonvertovať na čísla, teda ak sú v TB aj iné znaky.

nahlásit spamnahlásit spam 0 odpovědětodpovědět

Tento problém se tu již řešil minimálně 3x.

nahlásit spamnahlásit spam 0 odpovědětodpovědět

snad pomuze toto, osobne pouzivam Devexpress kde se da primo textboxu ve vlastnostech rici co muze obsahovat

       Public Shared Sub VerifyNumberInput(ByVal MyTextBox As TextBox, ByVal AllowDecimal As Boolean, ByVal AllowNegative As Boolean, ByRef e As System.Windows.Forms.KeyPressEventArgs)
            If e.KeyChar = Microsoft.VisualBasic.Chr(48) Or _
               e.KeyChar = Microsoft.VisualBasic.Chr(49) Or _
               e.KeyChar = Microsoft.VisualBasic.Chr(50) Or _
               e.KeyChar = Microsoft.VisualBasic.Chr(51) Or _
               e.KeyChar = Microsoft.VisualBasic.Chr(52) Or _
               e.KeyChar = Microsoft.VisualBasic.Chr(52) Or _
               e.KeyChar = Microsoft.VisualBasic.Chr(53) Or _
               e.KeyChar = Microsoft.VisualBasic.Chr(54) Or _
               e.KeyChar = Microsoft.VisualBasic.Chr(55) Or _
               e.KeyChar = Microsoft.VisualBasic.Chr(56) Or _
               e.KeyChar = Microsoft.VisualBasic.Chr(8) Or _
               e.KeyChar = Microsoft.VisualBasic.Chr(45) Or _
               e.KeyChar = Microsoft.VisualBasic.Chr(46) Or _
               e.KeyChar = Microsoft.VisualBasic.Chr(57) Then
                e.Handled = False
            Else
                e.Handled = True
            End If
            '--------------------------------------------------------------------------------------------------------
            ' Check to see if a decimal has been entered. If a decimal is allowed and there already is a decimal in
            ' the text box, handle the key. If a decimal is being entered and they are not allowed, handle.
            '--------------------------------------------------------------------------------------------------------
            Dim DecPos As Integer = MyTextBox.Text.IndexOf(Microsoft.VisualBasic.Chr(46))
            If DecPos <> -1 And e.KeyChar = Microsoft.VisualBasic.Chr(46) Then e.Handled = True
            If e.KeyChar = Microsoft.VisualBasic.Chr(46) And AllowDecimal = False Then e.Handled = True

            '--------------------------------------------------------------------------------------------------------
            ' Check to see if a decimal has been entered. If a decimal is allowed and there already is a decimal in
            ' the text box, handle the key. If a decimal is being entered and they are not allowed, handle.
            '--------------------------------------------------------------------------------------------------------
            Dim SignPos As Integer = MyTextBox.Text.IndexOf(Microsoft.VisualBasic.Chr(45))
            If SignPos <> -1 And e.KeyChar = Microsoft.VisualBasic.Chr(45) Then e.Handled = True
            If e.KeyChar = Microsoft.VisualBasic.Chr(45) And AllowNegative = False Then e.Handled = True
        End Sub

nahlásit spamnahlásit spam 0 odpovědětodpovědět

Naprosto zbytečné používat komponenty třetích stran, když tohle všechno se dá daleko jednodušeji udělat přímo MaskedTextBoxem, případně jinak.

nahlásit spamnahlásit spam 0 odpovědětodpovědět

Můžete být klidně i trochu konkrétní pane Linhart. Situace lze možná vyřešit MaskedTextBoxem, ale co když chci použít TextBox v ToolStripMenu ? Tam žádnou možnost MaskedTextBoxu nemám.

nahlásit spamnahlásit spam 0 odpovědětodpovědět

Myslím že v tom máte zmatek. Zaprvé žádná standardní třída z Windows Forms jménem ToolStripMenu neexistuje, existuje pouze ToolStripMenuItem, což je obyčejná položka v nabídce. Pokud máte na mysli ToolStripTextBox, tak o něm zde nepadlo jediné slovo, řeč je o obyčejném TextBoxu. Mimochodem ToolStripTextBox má rovněž událost Validating, na kterou se dá provádět validace hodnoty (nebo TextChanged, což je ale zbytečně náročné) - bez použití komponent třetích stran...

nahlásit spamnahlásit spam 1 / 1 odpovědětodpovědět

The following code example uses the KeyPress event to prevent characters from entering the control.

' Boolean flag used to determine when a character other than a number is entered.

Private nonNumberEntered As Boolean = False

' Handle the KeyDown event to determine the type of character entered into the control.

Private Sub textBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) _

Handles textBox1.KeyDown

' Initialize the flag to false.

nonNumberEntered = False

' Determine whether the keystroke is a number from the top of the keyboard.

If e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9 Then

' Determine whether the keystroke is a number from the keypad.

If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 Then

' Determine whether the keystroke is a backspace.

If e.KeyCode <> Keys.Back Then

' A non-numerical keystroke was pressed.

' Set the flag to true and evaluate in KeyPress event.

nonNumberEntered = True

End If

End If

End If

'If shift key was pressed, it's not a number.

If Control.ModifierKeys = Keys.Shift Then

nonNumberEntered = true

End If

End Sub 'textBox1_KeyDown

' This event occurs after the KeyDown event and can be used

' to prevent characters from entering the control.

Private Sub textBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) _

Handles textBox1.KeyPress

' Check for the flag being set in the KeyDown event.

If nonNumberEntered = True Then

' Stop the character from being entered into the control since it is non-numerical.

e.Handled = True

End If

End Sub 'textBox1_KeyPress

zdroj

Control.KeyPress Event

http://msdn.microsoft.com/cs-cz/library/...

nahlásit spamnahlásit spam 0 odpovědětodpovědět
zdroj
Control.KeyPress Event

http://msdn.microsoft.com/cs-cz/library/system.windows.forms.control.keypress%28v=vs.90%29.aspx

The following code example uses the KeyPress event to prevent characters from entering the control.

' Boolean flag used to determine when a character other than a number is entered. 
Private nonNumberEntered As Boolean = False 


' Handle the KeyDown event to determine the type of character entered into the control. 
Private Sub textBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) _
     Handles textBox1.KeyDown
    ' Initialize the flag to false.
    nonNumberEntered = False 

    ' Determine whether the keystroke is a number from the top of the keyboard. 
    If e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9 Then 
        ' Determine whether the keystroke is a number from the keypad. 
        If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 Then 
            ' Determine whether the keystroke is a backspace. 
            If e.KeyCode <> Keys.Back Then 
                ' A non-numerical keystroke was pressed.  
                ' Set the flag to true and evaluate in KeyPress event.
                nonNumberEntered = True 
            End If 
        End If 
    End If 
    'If shift key was pressed, it's not a number. 
    If Control.ModifierKeys = Keys.Shift Then
        nonNumberEntered = true
    End If 
End Sub 'textBox1_KeyDown


' This event occurs after the KeyDown event and can be used  
' to prevent characters from entering the control. 
Private Sub textBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) _
    Handles textBox1.KeyPress
    ' Check for the flag being set in the KeyDown event. 
    If nonNumberEntered = True Then 
        ' Stop the character from being entered into the control since it is non-numerical.
        e.Handled = True 
    End If 
End Sub 'textBox1_KeyPress
nahlásit spamnahlásit spam 0 odpovědětodpovědět

Lze vyuzít událost KeyPress na formuláři

je nutné mít nastaveno ve vlastnostech formuláře

KeyPreview = True

  
Private Sub Form1_KeyPress(ByVal sender As Object, _
             ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress

        Dim strVstup As String

        'číslice, desetinná tečka, znamenko plus a klavesa BackSpace

        Const Povoleno As String = "0123456789" & "." & "+"  _
             & Microsoft.VisualBasic.ChrW(Keys.Back)

        strVstup = e.KeyChar

        If strVstup.IndexOfAny(Povoleno.ToCharArray) = -1 Then
            'nepustí jine, nez vyjmenovane znaky v Povoleno, do ovladacich prvku na formulari 
            e.Handled = True
        End If

    End Sub

nahlásit spamnahlásit spam 0 odpovědětodpovědět
                       
Nadpis:
Antispam: Komu se občas házejí perly?
Příspěvek bude publikován pod identitou   anonym.
  • Administrátoři si vyhrazují právo komentáře upravovat či mazat bez udání důvodu.
    Mazány budou zejména komentáře obsahující vulgarity nebo porušující pravidla publikování.
  • Pokud nejste zaregistrováni, Vaše IP adresa bude zveřejněna. Pokud s tímto nesouhlasíte, příspěvek neodesílejte.

přihlásit pomocí externího účtu

přihlásit pomocí jména a hesla

Uživatel:
Heslo:

zapomenuté heslo

 

založit nový uživatelský účet

zaregistrujte se

 
zavřít

Nahlásit spam

Opravdu chcete tento příspěvek nahlásit pro porušování pravidel fóra?

Nahlásit Zrušit

Chyba

zavřít

feedback