|
-
Nov 7th, 2022, 08:46 PM
#1
Thread Starter
Member
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
-
Nov 7th, 2022, 08:49 PM
#2
Hyperactive Member
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.
-
Nov 7th, 2022, 08:53 PM
#3
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.
-
Nov 7th, 2022, 09:08 PM
#4
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 8th, 2022, 02:43 AM
#5
Thread Starter
Member
Re: Remove certain string from textbox
 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
-
Nov 8th, 2022, 03:23 AM
#6
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 "//")
-
Nov 8th, 2022, 03:47 AM
#7
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 8th, 2022, 04:36 AM
#8
Thread Starter
Member
Re: Remove certain string from textbox
 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!
-
Nov 8th, 2022, 10:24 AM
#9
Addicted Member
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.
-
Nov 8th, 2022, 11:01 AM
#10
Re: Remove certain string from textbox
 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…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 8th, 2022, 11:37 AM
#11
Addicted Member
Re: Remove certain string from textbox
I didnt realize that the replace function would cause the textchanged event to fire if nothing is replaced.
-
Nov 8th, 2022, 12:27 PM
#12
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
 
-
Nov 8th, 2022, 03:10 PM
#13
Re: Remove certain string from textbox
 Originally Posted by vbdotnut
I didnt realize that the replace function would cause the textchanged event to fire if nothing is replaced.
 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]
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|