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
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://“, “”)
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.
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
Re: Remove certain string from textbox
Quote:
Originally Posted by
.paul.
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
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 "//")
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
Re: Remove certain string from textbox
Quote:
Originally Posted by
.paul.
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!
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.
Re: Remove certain string from textbox
Quote:
Originally Posted by
vbdotnut
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…
Re: Remove certain string from textbox
Quote:
Replacing while typing
I didnt realize that the replace function would cause the textchanged event to fire if nothing is replaced.
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.
Re: Remove certain string from textbox
Quote:
Originally Posted by
vbdotnut
I didnt realize that the replace function would cause the textchanged event to fire if nothing is replaced.
Quote:
Originally Posted by
Shaggy Hiker
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]