Replace Text in a TextBox


Everyone has probably used or at least seen the find and replace feature in programs. This allows the user to find everything that matches the specified text string and replace it. This source sample demonstrates a very bare bones version of that same functionality in Visual Basic. In general to find a sub string in a parent string you can use the InStr function which is demonstrated below. However, in order to replace the found strings with a new one you need to get a little bit trickier. See below for how this can be done in a text box.

To try out this code create a new project, add a command button to the main form, add a text box as well. Double click the command button to into the click event handler for your button. In this method add the following code.

  1. Dim StartPos, Counter As Integer
  2. Dim FindString, ReplaceText As String
  3. FindString = "test"
  4. ReplaceText = "MyString"
  6. For Counter = 1 To Len(Text1.Text)
  7.         StartPos = InStr(Text1.Text, FindString)
  8.         If StartPos > 0 Then
  9.                 Text1.SelStart = StartPos - 1
  10.                 Text1.SelLength = Len(FindString)
  11.                 Text1.SelText = "" + ReplaceText
  12.         End If
  13. Next

Once you have the code in place run your application and add some text that has the string test in it. Hit the button and you will see all of these instances of the word test replaced with the string 'MyString'.

Note: The source for this was found at DreamVB which is no longer online.

If you enjoyed this post, subscribe for updates (it's free)

OR you can just user REPLACE

OR you can just user REPLACE function
strOriginal="your original text"
strFind="part of text you want to find"
strReplaceWith="text which you want to replace with"

done ;)

OR you can just user***

OR you can just user*** REPLACE function

OR you can just use REPLACE function

How to import text and xml file into new created file

I want to import text tab and comma delimited and xml data into other text file which will be created at run time. help me please in this way.

pls help me on linking the vb

pls help me on linking the vb and the crystal report

pls help me on linking the vb

pls help me on linking the vb and the crystal report

txtUser control

now i got a problem with txtUser text box which has txtUser.MaxLength= 15
i want to use a MsgBox to dialog with the input, if entry exceed the
maximum length of txtUser.please help me

please the save settings

please the save settings command is not working, is there going to be any declarations please, and with another example using checkbox or option button

like in vb6

list list1,"vew","tablename",fieldname","fieldname like '"& text1.text &"%'"

You have to have a textbox

You have to have a textbox on your form named text1.

this code doesn't work

this code does not work for me. i get a run-time error 424: object required. the debug points to Line 6 & i'm not sure how to fix it.

but how in the If statement

but how in the If statement when you are you listview