locked
TextBox "processing" RRS feed

  • Question

  • I have a series of TextBoxes in which I allow the user to enter some "values".
    I need to convert those 'string' values to Singles for further processing into my application like this:

    VarA = CSng(tbVarA.Text)

    This works well. The trouble starts when I use the KeyDown event:

        If e.KeyCode = Keys.Subtract Then
             tbVarA.Select()
        End If

    I am probably not using the right "Key" for the "Minus" or "Hyphen" key?
    The problem is that I have allow the user to enter negative values in that TextBox.
    To enter negative values, the user has to enter a "-" somehow.

    I would like control to remain in that TextBox when the user enters that character at the keyboard.

    Sunday, April 12, 2020 6:37 PM

All replies

  • You really are able to describe your problem very rudimentair. 

    But if you have problems with KeyDown, then use KeyUp, a little more is done at that moment and really few users keep it down. 


    Success
    Cor

    Sunday, April 12, 2020 7:15 PM
  • Thank you Cor,

    Please forgive my rudimentary problem description skills. 

    I solved my problem "temporarily" not in the KeyDown (I tried the Keypress event to no avail).

    The problem was that, when I typed the Hyphen character in the TextBox, the PictureBox event fired and cause an exception when trying to convert from "String" to "Single". Here is my whole KeyDown event:

    Private Sub tbVarA_KeyDown(sender As Object, e As KeyEventArgs) Handles tbVarA.KeyDown
            If e.KeyCode = Keys.Up Then
                tbVarA.Text = CSng(Var.Text) + 1
            End If
            If e.KeyCode = Keys.Down Then
                tbVar.Text = CSng(tbVar.Text) - 1
            End If
            PictureBox.Invalidate()
        End Sub

    As you can see, I'm using the Arrows Down and Up to increase or decrease the value inside the TextBox. I am probably going very wrong about this? But that's the only technique I know of.

    The trouble is when the PictureBox event fires, the following code causes an exception:

     VarA = CSng(tbVarA.Text)

    Because it does not recognize the "-".

    You see?

    Sunday, April 12, 2020 7:47 PM
  • Thank you Cor,

    Please forgive my rudimentary problem description skills. 

    I solved my problem "temporarily" not in the KeyDown (I tried the Keypress event to no avail).

    The problem was that, when I typed the Hyphen character in the TextBox, the PictureBox event fired and cause an exception when trying to convert from "String" to "Single". Here is my whole KeyDown event:

    Private Sub tbVarA_KeyDown(sender As Object, e As KeyEventArgs) Handles tbVarA.KeyDown
            If e.KeyCode = Keys.Up Then
                tbVarA.Text = CSng(Var.Text) + 1
            End If
            If e.KeyCode = Keys.Down Then
                tbVar.Text = CSng(tbVar.Text) - 1
            End If
            PictureBox.Invalidate()
        End Sub

    As you can see, I'm using the Arrows Down and Up to increase or decrease the value inside the TextBox. I am probably going very wrong about this? But that's the only technique I know of.

    The trouble is when the PictureBox event fires, the following code causes an exception:

     VarA = CSng(tbVarA.Text)

    Because it does not recognize the "-".

    You see?

    Hi

    Are those emboldened items actually in your code?


    Regards Les, Livingston, Scotland

    Sunday, April 12, 2020 8:06 PM
  • Hi

    You may benifit from using a Funcion to ease the several conversions you may have.

    Just call the Function with the numeric String and it will return (in this case) a valid Single (or zero if not a valid Single).

    Also,my confusion was that you seem to either use other variables, or, have the wrongly typed names - I can't guess  (tbVarA.Text, Var.Text) . Also,'PictureBox'  is not a valid name for the PictureBox control as it is a Type used by VS.

    Anyway, here is your code, altered as best fit inlight of the above comments, and incorporates said Function.

    BTS: you really would benifit from using Option Strict On at the top of you code.

    Option Strict On
    Option Explicit On
    ' Form1 with TextBox named tbVarA
    ' and PictureBox1
    Public Class Form1
      Dim VarA As Single = 0
      Private Sub tbVarA_KeyDown(sender As Object, e As KeyEventArgs) Handles tbVarA.KeyDown
        If e.KeyCode = Keys.Up Then
          tbVarA.Text = (GdetSingle(tbVarA.Text) + 1).ToString
        End If
        If e.KeyCode = Keys.Down Then
          tbVarA.Text = (GdetSingle(tbVarA.Text) - 1).ToString
        End If
        PictureBox1.Invalidate()
      End Sub
      Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
        VarA = GdetSingle(tbVarA.Text)
      End Sub
      Function GdetSingle(s As String) As Single
        Dim v As Single = 0
        If Single.TryParse(s, v) Then Return v
        Return 0
      End Function
    End Class


    Regards Les, Livingston, Scotland



    • Edited by leshay Sunday, April 12, 2020 8:22 PM
    Sunday, April 12, 2020 8:20 PM
  • Hello,

    For non decimal or double or negatives

    Public Class Form1
        Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
            If Not Char.IsControl(e.KeyChar) AndAlso (Not Char.IsDigit(e.KeyChar)) AndAlso (e.KeyChar = "."c) AndAlso (e.KeyChar <> "-"c) Then
                e.Handled = True
            End If
        End Sub
    End Class
    

    Negatives and decimals

    Public Class Form1
        Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
            If Not Char.IsControl(e.KeyChar) AndAlso (Not Char.IsDigit(e.KeyChar)) AndAlso (e.KeyChar <> "."c) AndAlso (e.KeyChar <> "-"c) Then
                e.Handled = True
            End If
    
            ' only allow one decimal point
            If e.KeyChar = "."c AndAlso (TryCast(sender, TextBox)).Text.IndexOf("."c) > -1 Then
                e.Handled = True
            End If
    
            ' only allow minus sign at the beginning
            If e.KeyChar = "-"c AndAlso (TryCast(sender, TextBox)).Text.Length > 0 Then
                e.Handled = True
            End If
    
        End Sub
    End Class
    In both cases to stop paste from windows clipboard set ShortcutsEnabled = False


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, April 12, 2020 8:28 PM