Results 1 to 8 of 8

Thread: Using a menu item to clear everything, but text in a textbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2021
    Posts
    15

    Post Using a menu item to clear everything, but text in a textbox

    How would I make a menu item that clears everything in a textbox, but text? So like, just symbols (e.g. !"$%^&*(/)? Basically a 'clean' button.

  2. #2
    Frenzied Member jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,838

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,891

    Re: Using a menu item to clear everything, but text in a textbox

    I would think that this would do the job:
    vb.net Code:
    1. myTextBox.Text = New String(myTextBox.Text.Where(Function(ch) Char.IsLetterOrDigit(ch)).ToArray())
    A String is an IEnumerable(Of Char) and the Where method filters out every item in an enumerable list that doesn't satisfy the specified criteria. ToArray turns the filtered list into an array and a new String is created from that.

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2021
    Posts
    15

    Re: Using a menu item to clear everything, but text in a textbox

    That works great, thank you so much!

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,891

    Re: Using a menu item to clear everything, but text in a textbox

    Don't forget to mark your thread Resolved using the Thread Tools menu if the issue has indeed been resolved.

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2021
    Posts
    15

    Re: Using a menu item to clear everything, but text in a textbox

    Sorry, I've just been testing this and it seems that when 'cleaning' a list of text, it compacts all the text together?

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,967

    Re: Using a menu item to clear everything, but text in a textbox

    Code:
    myTextBox.Text = New String(myTextBox.Text.Where(Function(ch) Char.IsLetterOrDigit(ch) OrElse ch = " "c OrElse ch = vbcr OrElse ch = vblf).ToArray())

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,891

    Re: Using a menu item to clear everything, but text in a textbox

    Quote Originally Posted by JakeC View Post
    Sorry, I've just been testing this and it seems that when 'cleaning' a list of text, it compacts all the text together?
    Perhaps you could provide a detailed description of the behaviour you expected and the behaviour you saw and how they differed. You could even provide an example or two. That way, we wouldn't have to guess, assume or interpret.

Tags for this Thread

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