none
update numeric value from textbox to db RRS feed

  • Question

  • i've been stuck with it for a while..

    my db (sqlite) value is set to double. when i try to update from a textbox (each keydown updates the db) i get bunch of errors every time i delete a value and the textbox is blank, when i type a charecter by accident or try to use the minus sign "-".

    actually, with the minus sign it works if i use it after entering a number and not before.. 

    i understand the logic that the db cant accept non-numeric characters but i can't force it to reffer only to numbers ,minus sign or keep it blank..

    help anyone?

    Wednesday, October 24, 2018 6:12 AM

All replies

  • Hi noampro8

    do like this

    Private Sub TextBox_KeyDown(Sender As Object, e As KeyEventArgs) Handles TextBox.KeyDown Try If e.KeyCode = Keys.Enter Then If String.IsNullOrEmpty(Sender.Text) = False AndAlso Microsoft.VisualBasic.IsNumeric(Sender.Text) Then 'Do Update e.SuppressKeyPress = True Else System.Windows.Forms.SendKeys.Send("{TAB}") e.SuppressKeyPress = True End If End If Catch ex As Exception System.Windows.Forms.MessageBox.Show(ex.Message, "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error) End Try End Sub

      Private Sub TextBox_KeyPress(ByVal Sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox.KeyPress
        Select Case e.KeyChar
          Case Microsoft.VisualBasic.Chr(System.Windows.Forms.Keys.Back),
               Microsoft.VisualBasic.ChrW(System.Windows.Forms.Keys.Delete),
               Microsoft.VisualBasic.ChrW(3), 'Ctrl + C
               Microsoft.VisualBasic.ChrW(22), 'Ctrl + V
               Microsoft.VisualBasic.ChrW(24), 'Ctrl + X
               Microsoft.VisualBasic.ChrW(26)  'Ctrl + Z
            e.Handled = False
          Case "0" To "9", "-", ",", "."
            e.Handled = False
          Case Else
            e.Handled = True
        End Select
      End Sub



    • Edited by Xan To Wednesday, October 24, 2018 6:28 AM
    • Proposed as answer by Xan To Wednesday, October 24, 2018 6:29 AM
    Wednesday, October 24, 2018 6:25 AM
  • Hi,

    Set only allows input of numbers, Enter key, Backspace key

     Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
            If Not (Char.IsNumber(e.KeyChar)) AndAlso e.KeyChar <> ChrW(13) AndAlso e.KeyChar <> ChrW(8) Then
                e.Handled = True
            End If
        End Sub

    Enter key trigger

     Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
            If e.KeyValue = 13 Then
                'update
            End If
        End Sub

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.




    • Edited by Alex Li-MSFT Wednesday, October 24, 2018 6:54 AM
    Wednesday, October 24, 2018 6:40 AM
  • thanks guys but still when i delete the textbox value or press the minus sign before the numbers i get an error:

    System.InvalidCastException: 'Conversion from string "" to type 'Double' is not valid.'

    Thursday, October 25, 2018 5:03 AM
  • Hi,

    Have you tried my code?

    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
            If e.KeyValue = 13 Then
                'update
            End If
        End Sub

    use Enter Key to trigger 

    or made a judgment,whether the textbox.Text can be converted to a double

     Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
            Dim str As String = TextBox1.Text
            Dim db As Double
            If Double.TryParse(str, db) Then
                'update
            End If
        End Sub

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 25, 2018 5:53 AM
  • why you don't post your code??
    Thursday, October 25, 2018 8:46 AM