wildcards

I need to add functionality to my form in this program that for some fields allows negative numbers, some just numbers, some three positive numbers separted by commas, and some just text. Also, I prefill with a text file so I need the error check for this also...

Thanks!

Newbiana



Answer this question

wildcards

  • ca_localhero

    THANKS! That works after the fact, but I was trying to error check as they type.

    I can allow for certain types of characters and their number but how do you check for whether you are at the beginning of the text as they type

    Private Sub ErrorCheckDecNeg(ByVal Sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles CV.KeyPress,

    Dim EachTextBox As TextBox = Sender

    If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar) Or (e.KeyChar = "." And EachTextBox.Text.IndexOf(".") < 0) Or (e.KeyChar = "-" And EachTextBox.Text.IndexOf("-") < 0)) Then

    If e.KeyChar = "-" And SOMETHING HERE TO CHECK IF AT BEGINNING OF STRING Then

    e.Handled = False

    End If

    e.Handled = True

    End If

    End Sub

    Newbiana


  • Kerrie S.

    First off,

    You should be doing the validation "after the fact" as you've stated, unless you're trying to create a key logger replica. Otherwise, you're creating a lot more work for yourself and will very possibly confuse the end user unless your code has some serious control over the cursor location.

    My suggestion is use the following format:

    ...at the beginning of the string

    Select Case left(trim(text1.text))

       Case "-", ".", "#", "$"

           msbox("Invalid Character " & left(trim(text1.text)))

       Case Else

            Some Code

    End Select

    If you want to search the string to find certain characters:

    For x = 1 to len(trim(text1.text))

    Select Case mid(Trim(text1.text), x, 1)

       Case "-", ".", "#", "$"

           msgbox("Invalid Character " & mid(text1.text, x, 1))

       Case Else

            Some Code

    End Select

    next x

    Adamus



  • Slugo

    Huh

        Dim mystring As String
        mystring = "This is a test"
        MsgBox (Len(mystring))

    The above doesn't work

    Best of luck to you.

    Adamus



  • StriderIRL

    Thanks, but what I am trying to do is just not allow negative signs unless at the beginnng of the textbox

    Perhaps using a MaskedTextBox

    Thanks!

    Newbiana


  • zaboboa

    I think something along the lines of

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

    If e.KeyChar = "-" And TextBox1.SelectionStart <> 0 Then

    e.Handled = True

    End If

    End Sub

    will do what you want.

    Dave


  • Jelle Mees

    No I was talking to Dave299...

    The len does not work on a string:>(

    Newbiana


  • Earl J Bonovich

    But how do I check if I'm at the front of the textbox for the negative sign
  • DSD

    LoneStar wrote:
    But how do I check if I'm at the front of the textbox for the negative sign
    Hint:

    If CInt(Text1.Text) < 0 Then
    MsgBox ("We've got a negative number")
    Else
    MsgBox ("We're positive")
    End If

    Adamus



  • Chaitanya Vempati

    Well, I can allow for only one - but I can't figure out how to check if I'm at the beginning of the textbox.

    Also, I used this:

    Private Sub ErrorCheckOutGntName(ByVal Sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles OutGntName.KeyPress

    Dim EachTextBox As TextBox = Sender

    If EachTextBox.Text Like " .gnt" Then

    e.Handled = True

    End If

    End Sub

    but it doesn't work.

    Also, this doesn't either:

    If EachTextBox.Text Like ",#," Then

    e.Handled = False

    End If

    I've only been using VB for less than a week, so the syntax is a complete mystery at this point. Book ordered and on the way... Thanks!

    Newbiana


  • Johan Levin141712

    And your question is

  • presence76

    If left(Trim(text1.text)) = "-" Then

    msgbox("It's no good")

    End if

    Also, your verbiage and focus should be "at the beginning of the string" not "at the beginning of the textbox"

    Adamus



  • tmg2006

    first of all keypress is not for that , you can use keypress event to check charachters one by one, the proper charachter will be added to the text or to reject it

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=218113&SiteID=1

    if you want to validate the text in textbox you can use validate event and use contain method

    textbox1.text.contain("sometext")

    hope this helps



  • SolomonDev

    That worked like a charm!

    So if I may try to stump you, how would you check for x,y,z

    i.e.: 1234134, 1343, 13432

    Steph


  • tlwilli2

    If you're talking to me ...:)

    For x = 1 to len(trim(text1.text))

    Select Case mid(Trim(text1.text), x, 1)

       Case "-", ".", "#", "$"

           msgbox("Invalid Character " & mid(text1.text, x, 1))

       Case Else

            If NOT IsNumeric(mid(text1.text, x, 1)) Then

                 msgbox(mid(text1.text, x, 1) & " is not a number")

            End If

    End Select

    next x

    Adamus



  • wildcards