Results 1 to 8 of 8

Thread: Pasting in to a textbox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    Ontario, Canada
    Posts
    134

    Pasting in to a textbox

    I want to make my program check whats being pasted in to my textbox and remove all disallowed characters, for example, I need to to remove all characters except numbers and commas. What would be the best way to carry this out?

  2. #2
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    use the Isnumeric function in the textbox change event

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    Ontario, Canada
    Posts
    134
    Theres two problems when using that, 1) I'd like to also have commas and 2) I'd like it to remove all non numeric characters automatically.

  4. #4
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    Originally posted by Xerpher
    Theres two problems when using that, 1) I'd like to also have commas and 2) I'd like it to remove all non numeric characters automatically.
    commas dont falsify the isnumeric test. try this:

    VB Code:
    1. Private Sub Text1_Change()
    2. If Not IsNumeric(Text1.Text) Then
    3.     Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
    4. End If
    5. End Sub

  5. #5
    Fanatic Member skald2k's Avatar
    Join Date
    Feb 2002
    Location
    Sydney, Australia
    Posts
    535
    Maybe trap for keystrokes. Detect if a numeric character is pressed and cancel it.
    - If at first you dont succeed, then give up, cause you will never will!

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    Ontario, Canada
    Posts
    134
    Well its a good thing that commas are allowed, but i cant allow periods... I guess I'll have to use a loop to go through each character pasted and detect whether its a number/comma or not then decide if it goes through So much work for such a late hour

  7. #7
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    Originally posted by Xerpher
    Well its a good thing that commas are allowed, but i cant allow periods... I guess I'll have to use a loop to go through each character pasted and detect whether its a number/comma or not then decide if it goes through So much work for such a late hour
    try this:

    VB Code:
    1. Private Sub Text1_Change()
    2. If Not IsNumeric(Text1.Text) Then
    3.     Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
    4. End If
    5. Text1.Text = Replace(Text1.Text, ".", "")
    6. End Sub

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    Ontario, Canada
    Posts
    134
    The replace! thats it! I dont know why I didn't think of that before, I'll make a for each loop that replaces all disallowed characters stored in an array with nothing!

    Thanks for the inspriration Muddy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width