Results 1 to 13 of 13

Thread: Remove certain string from textbox

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2021
    Posts
    41

    Remove certain string from textbox

    I want a textbox to actively search for 2 strings to delete them, one being "http://" and the other "https://"
    ive tried using a timer as such

    Code:
       Private Sub Timer1_Tick(sender As Object, e As EventArgs)
            If txtURL.Text = "https://" Then
                txtURL.Text = ""
            End If
    
            If txtURL.Text = "http://" Then
                txtURL.Text = ""
            End If
        End Sub
    and that doesn't work. What else can I do? I want it to get rid of the "http://" or "https://" the second it is typed or pasted into the textbox.
    Thanks

  2. #2
    Hyperactive Member Frabulator's Avatar
    Join Date
    Jan 2015
    Location
    USA
    Posts
    393

    Re: Remove certain string from textbox

    Inside the timer use the replace function.

    Code:
        Sub Main()
    
            ' Replace substring with another substring.
            Dim value1 As String = "abcdef"
            Dim value2 As String = value1.Replace("abc", "hi")
            Console.WriteLine(value1)
            Console.WriteLine(value2)
    
        End Sub
    source: https://www.dotnetperls.com/replace-vbnet

    EDIT—-

    I believe a more optimal method would be to have the text box value change when needed instead of actively. If you are changing it actively then you will run the issue of refocusing on the text box and messing up user control.

    Instead, have it run the code when focus is removed from the box, or when your process requiring the string is needed.

    Code:
    Dim Str as String = textbox1.text
    Str = Str.Replace(“http://“, “”)
    Str = Str.Replace(“https://“, “”)
    Last edited by Frabulator; Nov 7th, 2022 at 09:08 PM.
    Oops, There it goes. Yep... my brain stopped...
    _________________________________

    Useful Things:

    How to link your VB.Net application to Excel

  3. #3
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,138

    Re: Remove certain string from textbox

    Using a timer for this seems silly since the TextBox has an Event that is thrown when the contents of the textbox changes, so you should be placing any of this manipulation code inside that event.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Remove certain string from textbox

    Code:
    Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox1.TextChanged
        ‘ put your replace code in this event handler
    End Sub

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2021
    Posts
    41

    Re: Remove certain string from textbox

    Quote Originally Posted by .paul. View Post
    Code:
    Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox1.TextChanged
        ‘ put your replace code in this event handler
    End Sub
    Ok, so I have that but now when using my script

    Code:
            If txtURL.Text = "https://" Then
                txtURL.Text = ""
            End If
    
            If txtURL.Text = "http://" Then
                txtURL.Text = ""
            End If
    it won't get rid of the http:// or https:// if I add a string such as https://website.com, all I need is for the http:// or https:// to go away and keep the other string

  6. #6
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,901

    Re: Remove certain string from textbox

    txtURL.Text doesn't match "https://" when the content of txtURL.Text is "https://website.com"

    I would check the location of "//" in the text
    If the location is not zero then copy the content of the text starting from the location + 2 (for the length of "//")

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

    Re: Remove certain string from textbox

    Code:
    If txtURL.Text.Contains("https://") Then
        txtURL.Text = txtURL.Text.Replace(“https://“, “”)
    End If
    
    If txtURL.Text.Contains(“http://") Then
        txtURL.Text = txtURL.Text.Replace(“http://“, “”)
    End If

  8. #8

    Thread Starter
    Member
    Join Date
    Feb 2021
    Posts
    41

    Resolved Re: Remove certain string from textbox

    Quote Originally Posted by .paul. View Post
    Code:
    If txtURL.Text.Contains("https://") Then
        txtURL.Text = txtURL.Text.Replace(“https://“, “”)
    End If
    
    If txtURL.Text.Contains(“http://") Then
        txtURL.Text = txtURL.Text.Replace(“http://“, “”)
    End If
    Thank You!

  9. #9
    Addicted Member
    Join Date
    Jan 2022
    Posts
    211

    Re: Remove certain string from textbox

    You dont need to check if the string contains x, the replace function will replace it if it exists.

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Remove certain string from textbox

    Quote Originally Posted by vbdotnut View Post
    You dont need to check if the string contains x, the replace function will replace it if it exists.
    Replacing while typing will cause another textchange to be fired. It’s better to isolate it to avoid multiple fruitless calls to replace…

  11. #11
    Addicted Member
    Join Date
    Jan 2022
    Posts
    211

    Re: Remove certain string from textbox

    Replacing while typing
    I didnt realize that the replace function would cause the textchanged event to fire if nothing is replaced.

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Remove certain string from textbox

    Will textchanged be raised in the case of a copy/paste? I don't recall, but I thought it did not.
    My usual boring signature: Nothing

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Remove certain string from textbox

    Quote Originally Posted by vbdotnut View Post
    I didnt realize that the replace function would cause the textchanged event to fire if nothing is replaced.
    Quote Originally Posted by Shaggy Hiker View Post
    Will textchanged be raised in the case of a copy/paste? I don't recall, but I thought it did not.
    As far as I am aware, from memory, TextChanged is as it says… Raised whenever the text changes for any reason.

    [QUOTE MSDN]This event is raised if the Text property is changed by either a programmatic modification or user interaction.[/QUOTE]

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