Results 1 to 15 of 15

Thread: find, replace, words in textbox

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2003
    Posts
    33

    find, replace, words in textbox

    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.
    Im supposed to be working with strings, and substring i guess.

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    VB Code:
    1. Dim s as String=Textbox1.Text
    2. Textbox1.Text=s.Replace("Box", "Rocks")
    Remember that this function is case sensitive, so 'Box' is different from 'box'.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2003
    Posts
    33

    ?

    what exactly does this function do? If you dont mind me asking

  4. #4
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Replace method of string searches the string for a character or string and replace it with another character or string and returns a new string.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2003
    Posts
    33

    text fields

    will this work with one large textbox with some text typed into it. then have two smaller textboxes one where i can enter the word I want to find and one where I enter the one that I want to replace it with. Then after putting the two words in I want to hit a button that will go through search the text and then replace it with the word from the replace field.

  6. #6
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Yes. Imagine you have three textboxes (txtMain, txtFind, txtReplace) and a button. Then in the click event of the button:
    VB Code:
    1. Dim s as String = txtMain.Text
    2. txtMain.Text = s.Replace(txtFind.Text, txtReplace.Text)
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  7. #7
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Lunatic3, it's nice of you to help out a newbie (god knows we all need help). But jeez, a student?

    The mind is not a vessel to be filled, but a fire to be ignited.

    doro40282, if you've gone to class, you might have learned how to press F1. Sorry, that was rude. My point is that there is no reason to ask what a method does when the answer is a click away.

    Real programmers never ask "will this work", give it a try first. If it doesn't work, post what you've tried so far.

  8. #8

    Thread Starter
    Member
    Join Date
    Nov 2003
    Posts
    33

    Talking student

    yes i am a student and not a real programmer yet. I am learning as I go. One thing that helps is looking at other code and breaking it down to understand how that works. I have been doing alot of programming these past few weeks and I greatly appreciate the help you guys are giving me I am learning alot from these forums. I am trying to teach myself my first language and I didnt miss one class all semester by the way

  9. #9

    Thread Starter
    Member
    Join Date
    Nov 2003
    Posts
    33

    method

    and I did read up on that method, i also came across a wildcard method. but the only thing is I try reading it and there is alot that I still do not understand. Some of the information is pretty deep.

  10. #10
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Are you still having troubles accomplishing your task?

  11. #11

    Thread Starter
    Member
    Join Date
    Nov 2003
    Posts
    33

    find search replace

    yes I am having problems. Im really not sure how to start this. I read up on a couple methods like the wildcard method and string replace but Im really not sure how to implement them. I know that when the text is typed into the textbox I need to convert it to a string. And then search the string however many characters the word is that I want to find which could be s.length maybee. and then I am confused about how to replace it maybee using a substring (x, ) or something like that. I have a general understanding of it in english but I have trouble putting it into code.

  12. #12
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Using Lunatic3's method, I put three text boxes and a button on a form. Type word to search for in TextBox1, the replace string in TextBox2, and a bunch of text to search in TextBox3. Then when you click the button,

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         TextBox3.Text = TextBox3.Text.Replace(TextBox1.Text, TextBox2.Text)
    3.     End Sub

  13. #13

    Thread Starter
    Member
    Join Date
    Nov 2003
    Posts
    33

    worked

    thank you that worked well I really appreiciate it. I am also trying to get it to work another way. I'll see if I can get it to work both ways. thanks again

  14. #14

    Thread Starter
    Member
    Join Date
    Nov 2003
    Posts
    33

    Regex

    Does anyone know where I can find some information about using regular expressions to manipulate strings?

  15. #15
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    If you really want to learn it, download chapter 9 of the book in the following link: http://www.oreilly.com/catalog/regex...ter/index.html

    but it may be a little bit hard to start with.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

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