Results 1 to 12 of 12

Thread: search through text box

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    182

    search through text box

    i need to be able to remover certain lines from a text box

    e.g. before
    line 1
    line 2
    line 1
    line 4
    line 2
    line 5
    line 6
    line 2
    line 1

    so when i type e.g. line 1 in a text box and hit enter i would like it to search the text box and remove what ever is in the text box including any doubles etc.

    e.g. if i type line 1 in the text box 2 it remove all "line 1" lines
    so after
    line 2
    line 4
    line 2
    line 5
    line 6
    line 2

    any ideas?

  2. #2
    Addicted Member
    Join Date
    Apr 2008
    Posts
    198

    Re: search through text box

    you can put the file in list box. write the file again without the line u enter.
    IT CTO & System Administrator.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    182

    Re: search through text box

    i am looking for something which searches and removes the item i want in a text box. i prefer not to use a list box. thanks anyway

  4. #4
    Hyperactive Member
    Join Date
    Oct 2007
    Location
    Indiana
    Posts
    295

    Re: search through text box

    Try using the replace function.

    Text1.text = replace(Text1.Text,"Line1","")

    This will remove every occurrence of "Line1"

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: search through text box

    text1.text = replace(text1.text, "line1", "")

    edit: snap
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: search through text box

    Quote Originally Posted by westconn1 and Caskbill
    Text1.text = replace(Text1.Text,"Line1","")
    Eh! That will leave some blank lines there and it also replaces "Line11" with "1", "xyzLine1abc" with "xyzabc".

    How about this:
    Code:
    '-- vbLf and vbCr are added to front and back to make sure
    '-- the first and last lines will be also removed if matched.
    sText = vbLf & Text1.Text & vbCr
    sItem = vbLf & Text2.Text & vbCr
    sText = Replace(sText, sItem, "")
    Text1.Text = Mid$(sText, 2, Len(sText) - 2) '-- remove added chars
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    182

    Re: search through text box


    Code:
    '-- vbLf and vbCr are added to front and back to make sure
    '-- the first and last lines will be also removed if matched.
    sText = vbLf & Text1.Text & vbCr
    sItem = vbLf & Text2.Text & vbCr
    sText = Replace(sText, sItem, "")
    Text1.Text = Mid$(sText, 2, Len(sText) - 2) '-- remove added chars
    i just tried that and after clicking a commnd button nothing happened. i had two text boxes with multiline set as well as scroll bars

  8. #8
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: search through text box

    Quote Originally Posted by adam786
    i just tried that and after clicking a commnd button nothing happened. i had two text boxes with multiline set as well as scroll bars
    It worked for me, how are you doing it?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    182

    Re: search through text box

    firstly i opened up vb 6 and click exe
    then added 2 text boxes
    then set the 2 text boxes to multiline and scroll bars (both ver and hori)
    added a command button and added code to command button

    clicked run and does not do anything.

  10. #10
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: search through text box

    Let's try to define the problem better, primarily because AnHn noticed that the problem is not that simple. You have three possibilities that have to be handled. The last line of a multi-line text box could have:
    (1) VbCrLf and nothing else
    (2) a string without VbCrLf at the end
    (3) A string with VbCrLF at the end

    The other lines have at least VbCrLF and may or may not have a string preceding it. It appears that you want the code to (A) get rid of all double line spacing first and then (B) examine the last line, which could now be any of (1) through (3) above, and delete any line within the text box that matches it, including the VbCrLf on that line, thus repacking the text box.

    Is that correct?
    Doctor Ed

  11. #11
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    Pakistan
    Posts
    388

    Wink Re: search through text box

    Duh! Everything said and done, just use this simple piece of code. This will work just you want it to. Note that you will need two textboxes (one will contain the data and the other will contain the line which you want to remove) and one command button. When you click on the command button, your specified line will be removed.
    PLUS, this code has been tested for functionality and I'm including a working form file which you can dnld and use.

    Code:
    Private Sub Command1_Click()
       Dim srch As String, linedata As String
       Dim i As Integer
       Text1.Tag = ""
       For i = 1 To 50 'enough for 50 lines. extend the loop to as much as required
          If InStr(Text1.Text,vbNewLine) = 0 Then
             linedata = Text1.Text
             Text1.Text = ""
          Else
             linedata = Left(Text1.Text,InStr(Text1.Text,vbNewLine)-1)
             Text1.Text = Mid(Text1.Text,Instr(Text1.Text,vbNewLine)+2)
          End If
          If linedata <> Text2.Text Then
             If Text1.Tag <> "" Then Text1.Tag = Text1.Tag & vbNewLine
             Text1.Tag = Text1.Tag & linedata
          End If
          If Text1.Text = "" Then Exit For
       Next i
       Text1.Text = Text1.Tag
    End Sub
    Attached Files Attached Files
    If your problem is solved, then drag down the Thread Tools and mark your thread as Resolved.

    If I helped you solve your problem, inflate some air into my ego by rating my post and adding a comment too.

    For notorious issues (elaborate yourself) contact me via PM. I don't answer them in the forums EVER.

  12. #12
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: search through text box

    Quote Originally Posted by lone_REBEL
    Just use this simple piece of code. This will work. Note that you will need two text boxes. One will contain the data and the other will contain the line that you want to remove. You also need a command button. When you click on the command button, your specified line will be removed. This code has been tested for functionality, and I'm including a working form file.

    Code:
    Private Sub Command1_Click()
       Dim srch As String, linedata As String
       Dim i As Integer
       Text1.Tag = ""
       For i = 1 To 50 'enough for 50 lines. extend the loop to as much as required
          If InStr(Text1.Text,vbNewLine) = 0 Then
             linedata = Text1.Text
             Text1.Text = ""
          Else
             linedata = Left(Text1.Text,InStr(Text1.Text,vbNewLine)-1)
             Text1.Text = Mid(Text1.Text,Instr(Text1.Text,vbNewLine)+2)
          End If
          If linedata <> Text2.Text Then
             If Text1.Tag <> "" Then Text1.Tag = Text1.Tag & vbNewLine
             Text1.Tag = Text1.Tag & linedata
          End If
          If Text1.Text = "" Then Exit For
       Next i
       Text1.Text = Text1.Tag
    End Sub
    Good job, 'Rebel.
    Doctor Ed

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