Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello, I am new to vb.net. This is my second small final exam project for class. I have to have a text box with some text in it. Then I have put a word in a field such as box and then in anothe field I have to enter another word such as rocks. What I then have to do is hit a button that will search the text in the textbox for the word box and replace it with the word rocks for example. Any one have any ideas. I am pretty inexperienced at programming.

 

I know I have to use strings though. I looked at the strings replace method but I got confused. Im not sure what the code should look like any help would be greatly appreciated.

Posted
Should I somehow put the contents of the textbox into a string. then use a for statement or something with a sunstring (x. s.length) so that it seaches for the same length as the word I want to change. or textbox1.text.substring (0, x-1) Sorry if this sounds bad I am pretty inexperienced at programming but I find it very interesting. I think this was what my teacher was talking about,
  • *Experts*
Posted
You don't have to do anything like you said. Do what Robby showed you. In that example, TextBox1 is the TextBox which you search for the word. It uses the Replace method of it to replace each word that you specify for the first argument with the word you specify in the second argument. Then it returns the modified string. Put that code in the Click event of your button. What you also have to do is substitute the hardcoded values with the values from the textboxes which specify what to look for, and with what to replace it.
Posted

How about sending your search text to a function that finds the position within the text box starting from the beginning! Here is an example of the function;

 

 

Public Function FindText(ByVal strText As String, ByVal intStartingPoint As Integer) As Integer

'Initialize return value to false by default

Dim returnValue As Integer = -1

Dim intIndexToText As Integer

'Ensure that search string has been specified and a valid starting point

If strText.Length > 0 And intStartingPoint >= 0 Then

'Obtain location of the search string

intIndexToText = rtbViewer.Find(strText, intStartingPoint, RichTextBoxFinds.MatchCase)

'Determine whether the text was found

If intIndexToText >= 0 Then

returnValue = intIndexToText

End If

End If

Return returnValue

End Function

 

You can then call the function again starting at an point past the last occurrance.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...