Results 1 to 27 of 27

Thread: Searching Text

  1. #1

    Thread Starter
    Lively Member lavarock09's Avatar
    Join Date
    Jun 2005
    Posts
    124

    Searching Text

    How do I search text for a certain peice of text,

    I want to do a bbcode thing, so it takes bbcode and makes it into text with all the formatting, so I want it to be able to find 2 things, e.g. [/b] and [//b] and ignore the text inbetween, e.g. [/b]Hi[//b]

    I want it to find the [/b] and the [//b] and the output to make Hi bold,

    Is this possible in VB? and if so, how would I do it?

    Ignore the extra slash, it was making the text bold

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Searching Text

    You search like this:
    VB Code:
    1. Dim pos as integer
    2. pos = instr(text1.text,"/b") ' will return 0 if not found, or the position if it is

    Not sure exactly what you want. Usually, we get the text in between two tags.

  3. #3

    Thread Starter
    Lively Member lavarock09's Avatar
    Join Date
    Jun 2005
    Posts
    124

    Re: Searching Text

    well, that doesn't seem to do anyhting,

    what I want is, when in the textbox you type

    [//b]Hi All[///b] (ignore the 2 extra slashes)

    It outputs Hi All into a textbox

  4. #4

  5. #5

    Thread Starter
    Lively Member lavarock09's Avatar
    Join Date
    Jun 2005
    Posts
    124

    Re: Searching Text

    richtextbox is fine, as long as it works

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Searching Text

    OK here's an example.

    VB Code:
    1. Option Explicit
    2. Private Const BOLDSTART = "[//b]"
    3. Private Const BOLDEND = "[///b]"
    4. Private Sub Form_Load()
    5.  
    6. End Sub
    7.  
    8.  
    9. Private Sub RichTextBox1_Change()
    10.  
    11.     Dim intStart As Integer
    12.     Dim intEnd As Integer
    13.    
    14.     intStart = InStr(1, RichTextBox1.Text, BOLDSTART)
    15.     intEnd = InStr(1, RichTextBox1.Text, BOLDEND)
    16.    
    17.     If intStart > 0 And intEnd > 0 Then
    18.         RichTextBox1.Text = Replace(RichTextBox1.Text, BOLDSTART, "")
    19.         RichTextBox1.Text = Replace(RichTextBox1.Text, BOLDEND, "")
    20.         RichTextBox1.SelStart = intStart - 1
    21.         RichTextBox1.SelLength = intEnd - intStart
    22.         RichTextBox1.SelBold = True
    23.         RichTextBox1.SelStart = Len(RichTextBox1.Text)
    24.         RichTextBox1.SelBold = False
    25.     End If
    26.    
    27.    
    28. End Sub
    This only works for the last bold words. To make it work for more than one pair of tags you'll need to either wait until the user is done and have them hit a button to convert all the tags at once, or you'll need to maintain an array of intStart and intEnd values for each par of tags and redo the bolding for all the words each time a new one is found.

  7. #7

    Thread Starter
    Lively Member lavarock09's Avatar
    Join Date
    Jun 2005
    Posts
    124

    Re: Searching Text

    That seems to do the trick, I can do the same for Underline and bold, it should be just a case of changing a few words.

    How would I do url's?

  8. #8

    Thread Starter
    Lively Member lavarock09's Avatar
    Join Date
    Jun 2005
    Posts
    124

    Re: Searching Text

    Ah, I now have a problem

    I have added Underline

    this is my code

    VB Code:
    1. Option Explicit
    2. Private Const BOLDSTART = "[//b]"
    3. Private Const BOLDEND = "[///b]"
    4. Private Const UNDERSTART = "[//u]"
    5. Private Const UNDEREND = "[///u]"
    6. Private Sub RichTextBox1_Change()
    7.  
    8.     Dim intStart As Integer
    9.     Dim intEnd As Integer
    10.         intStart = InStr(1, RichTextBox1.Text, BOLDSTART)
    11.     intEnd = InStr(1, RichTextBox1.Text, BOLDEND)
    12.    
    13.    
    14.         End Sub
    15.        
    16. Private Sub Command1_Click()
    17. Dim intStart As Integer
    18.     Dim intEnd As Integer
    19.         intStart = InStr(1, RichTextBox1.Text, BOLDSTART)
    20.     intEnd = InStr(1, RichTextBox1.Text, BOLDEND)
    21. If intStart > 0 And intEnd > 0 Then
    22.         RichTextBox1.Text = Replace(RichTextBox1.Text, BOLDSTART, "")
    23.         RichTextBox1.Text = Replace(RichTextBox1.Text, BOLDEND, "")
    24.         RichTextBox1.SelStart = intStart - 1
    25.         RichTextBox1.SelLength = intEnd - intStart
    26.         RichTextBox1.SelBold = True
    27.         RichTextBox1.SelStart = Len(RichTextBox1.Text)
    28.         RichTextBox1.SelBold = False
    29.     End If
    30.     Dim intStart2 As Integer
    31.     Dim intEnd2 As Integer
    32.         intStart2 = InStr(1, RichTextBox1.Text, UNDERSTART)
    33.     intEnd2 = InStr(1, RichTextBox1.Text, UNDEREND)
    34. If intStart2 > 0 And intEnd2 > 0 Then
    35.         RichTextBox1.Text = Replace(RichTextBox1.Text, UNDERSTART, "")
    36.         RichTextBox1.Text = Replace(RichTextBox1.Text, UNDEREND, "")
    37.         RichTextBox1.SelStart = intStart2 - 1
    38.         RichTextBox1.SelLength = intEnd2 - intStart
    39.         RichTextBox1.SelUnderline = True
    40.         RichTextBox1.SelStart = Len(RichTextBox1.Text)
    41.         RichTextBox1.SelUnderline = False
    42.     End If
    43. End Sub

    but it makes some letters after the [///b] bold

  9. #9

  10. #10

    Thread Starter
    Lively Member lavarock09's Avatar
    Join Date
    Jun 2005
    Posts
    124

    Re: Searching Text

    In the richtextbox I have typed,

    [//b]Hi[///b]
    [//u]Hi[///u]

    and it outputs

    Hi
    Hi

  11. #11
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Searching Text

    I don't think you should use the change event to do formatting. The click event should take care of it. You'd have to keep track of the position in the change event to know what you want to format.

  12. #12

    Thread Starter
    Lively Member lavarock09's Avatar
    Join Date
    Jun 2005
    Posts
    124

    Re: Searching Text

    I'm using the click event, I changed it

  13. #13
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Searching Text

    I just got this working in the changed event: seems to work
    VB Code:
    1. Private Sub RichTextBox1_Change()
    2.   Static intStart1 As Integer
    3.   Static intEnd1 As Integer
    4.   Static intStart2 As Integer
    5.   Static intEnd2 As Integer
    6.   intStart1 = InStr(1, RichTextBox1.Text, BOLDSTART)
    7.   If intStart1 > 0 Then
    8.     intEnd1 = InStr(intStart1, RichTextBox1.Text, BOLDEND)
    9.   End If
    10.   If intEnd1 > 0 Then
    11.     RichTextBox1.Text = Replace(RichTextBox1.Text, BOLDSTART, "")
    12.     RichTextBox1.Text = Replace(RichTextBox1.Text, BOLDEND, "")
    13.     RichTextBox1.SelStart = intStart1 - 1
    14.     RichTextBox1.SelLength = intEnd1 - intStart1
    15.     RichTextBox1.SelBold = True
    16.     RichTextBox1.SelStart = Len(RichTextBox1.Text)
    17.     RichTextBox1.SelBold = False
    18.     intEnd1 = 0
    19.   End If
    20.   intStart2 = InStr(1, RichTextBox1.Text, UNDERSTART)
    21.   If intStart2 > 0 Then
    22.     intEnd2 = InStr(intStart2, RichTextBox1.Text, UNDEREND)
    23.   End If
    24.   If intEnd2 > 0 Then
    25.     RichTextBox1.Text = Replace(RichTextBox1.Text, UNDERSTART, "")
    26.     RichTextBox1.Text = Replace(RichTextBox1.Text, UNDEREND, "")
    27.     RichTextBox1.SelStart = intStart2 - 1
    28.     RichTextBox1.SelLength = intEnd2 - intStart2
    29.     RichTextBox1.SelUnderline = True
    30.     RichTextBox1.SelStart = Len(RichTextBox1.Text)
    31.     RichTextBox1.SelUnderline = False
    32.     intEnd2 = 0
    33.   End If
    34. End Sub

  14. #14

  15. #15
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Searching Text

    So it didn't keep redimming them, just to save some time, I guess. I was going to implement a position counter (which would have had to be static) but decided that as long as the formatting tags were being deleted, the position didn't matter.

  16. #16

    Thread Starter
    Lively Member lavarock09's Avatar
    Join Date
    Jun 2005
    Posts
    124

    Re: Searching Text

    Quote Originally Posted by dglienna
    I just got this working in the changed event: seems to work
    VB Code:
    1. Private Sub RichTextBox1_Change()
    2.   Static intStart1 As Integer
    3.   Static intEnd1 As Integer
    4.   Static intStart2 As Integer
    5.   Static intEnd2 As Integer
    6.   intStart1 = InStr(1, RichTextBox1.Text, BOLDSTART)
    7.   If intStart1 > 0 Then
    8.     intEnd1 = InStr(intStart1, RichTextBox1.Text, BOLDEND)
    9.   End If
    10.   If intEnd1 > 0 Then
    11.     RichTextBox1.Text = Replace(RichTextBox1.Text, BOLDSTART, "")
    12.     RichTextBox1.Text = Replace(RichTextBox1.Text, BOLDEND, "")
    13.     RichTextBox1.SelStart = intStart1 - 1
    14.     RichTextBox1.SelLength = intEnd1 - intStart1
    15.     RichTextBox1.SelBold = True
    16.     RichTextBox1.SelStart = Len(RichTextBox1.Text)
    17.     RichTextBox1.SelBold = False
    18.     intEnd1 = 0
    19.   End If
    20.   intStart2 = InStr(1, RichTextBox1.Text, UNDERSTART)
    21.   If intStart2 > 0 Then
    22.     intEnd2 = InStr(intStart2, RichTextBox1.Text, UNDEREND)
    23.   End If
    24.   If intEnd2 > 0 Then
    25.     RichTextBox1.Text = Replace(RichTextBox1.Text, UNDERSTART, "")
    26.     RichTextBox1.Text = Replace(RichTextBox1.Text, UNDEREND, "")
    27.     RichTextBox1.SelStart = intStart2 - 1
    28.     RichTextBox1.SelLength = intEnd2 - intStart2
    29.     RichTextBox1.SelUnderline = True
    30.     RichTextBox1.SelStart = Len(RichTextBox1.Text)
    31.     RichTextBox1.SelUnderline = False
    32.     intEnd2 = 0
    33.   End If
    34. End Sub
    This still doesn't stop this happening,

    Input

    [//b]Hi[///b]
    [//u]Hi[///u]

    Output

    Hi
    Hi

  17. #17
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Searching Text

    Sorry about that. There were some mistakes in my original code.

    VB Code:
    1. Option Explicit
    2. Private Const BOLDSTART = "[//b]"
    3. Private Const BOLDEND = "[///b]"
    4. Private Const UNDERSTART = "[//u]"
    5. Private Const UNDEREND = "[///u]"
    6.        
    7. Private Sub Command1_Click()
    8. Dim intStart As Integer
    9. Dim intEnd As Integer
    10. Dim intStart2 As Integer
    11. Dim intEnd2 As Integer
    12.  
    13. intStart = InStr(1, RichTextBox1.Text, BOLDSTART) + Len(BOLDSTART)
    14. intEnd = InStr(1, RichTextBox1.Text, BOLDEND) - 1
    15. intStart2 = InStr(1, RichTextBox1.Text, UNDERSTART) + Len(UNDERSTART)
    16. intEnd2 = InStr(1, RichTextBox1.Text, UNDEREND) - 1
    17.  
    18. If intStart > 0 And intEnd > 0 Then
    19.     RichTextBox1.SelStart = intStart - 1
    20.     RichTextBox1.SelLength = intEnd - intStart + 1
    21.     RichTextBox1.SelBold = True
    22.     RichTextBox1.SelStart = Len(RichTextBox1.Text)
    23.     RichTextBox1.SelBold = False
    24. End If
    25. If intStart2 > 0 And intEnd2 > 0 Then
    26.     RichTextBox1.SelStart = intStart2 - 1
    27.     RichTextBox1.SelLength = intEnd2 - intStart2 + 1
    28.     RichTextBox1.SelUnderline = True
    29.     RichTextBox1.SelStart = Len(RichTextBox1.Text)
    30.     RichTextBox1.SelUnderline = False
    31. End If
    32.  
    33. RichTextBox1.TextRTF = Replace(RichTextBox1.TextRTF, BOLDSTART, "")
    34. RichTextBox1.TextRTF = Replace(RichTextBox1.TextRTF, BOLDEND, "")
    35. RichTextBox1.TextRTF = Replace(RichTextBox1.TextRTF, UNDERSTART, "")
    36. RichTextBox1.TextRTF = Replace(RichTextBox1.TextRTF, UNDEREND, "")

  18. #18

    Thread Starter
    Lively Member lavarock09's Avatar
    Join Date
    Jun 2005
    Posts
    124

    Re: Searching Text

    Thanks, thats alot better, but how would I go about doing URL's and IMG's

  19. #19
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Searching Text

    Here is code for the url that I copied and modified slightly from a post by manovo11. Note that the url tags aren't necessary with this code but I included them for consistency.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Const WM_USER As Long = &H400
    4. Private Const EM_AUTOURLDETECT As Long = (WM_USER + 91)
    5. Private Const EM_GETSEL As Long = &HB0
    6.  
    7. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
    8.      ByVal hwnd As Long, _
    9.      ByVal wMsg As Long, _
    10.      ByVal wParam As Long, _
    11.      lParam As Any) As Long
    12.  
    13. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
    14.      ByVal hwnd As Long, _
    15.      ByVal lpOperation As String, _
    16.      ByVal lpFile As String, _
    17.      ByVal lpParameters As String, _
    18.      ByVal lpDirectory As String, _
    19.      ByVal nShowCmd As Long) As Long
    20.  
    21.  
    22. Private Const BOLDSTART = "[//b]"
    23. Private Const BOLDEND = "[///b]"
    24. Private Const UNDERSTART = "[//u]"
    25. Private Const UNDEREND = "[///u]"
    26. Private Const URLSTART = "[//url]"
    27. Private Const URLEND = "[///url]"
    28.  
    29.        
    30. Private Sub Command1_Click()
    31. Dim intStart As Integer
    32. Dim intEnd As Integer
    33. Dim intStart2 As Integer
    34. Dim intEnd2 As Integer
    35.  
    36. intStart = InStr(1, RichTextBox1.Text, BOLDSTART) + Len(BOLDSTART)
    37. intEnd = InStr(1, RichTextBox1.Text, BOLDEND) - 1
    38. intStart2 = InStr(1, RichTextBox1.Text, UNDERSTART) + Len(UNDERSTART)
    39. intEnd2 = InStr(1, RichTextBox1.Text, UNDEREND) - 1
    40.  
    41. If intStart > 0 And intEnd > 0 Then
    42.     RichTextBox1.SelStart = intStart - 1
    43.     RichTextBox1.SelLength = intEnd - intStart + 1
    44.     RichTextBox1.SelBold = True
    45.     RichTextBox1.SelStart = Len(RichTextBox1.Text)
    46.     RichTextBox1.SelBold = False
    47. End If
    48. If intStart2 > 0 And intEnd2 > 0 Then
    49.     RichTextBox1.SelStart = intStart2 - 1
    50.     RichTextBox1.SelLength = intEnd2 - intStart2 + 1
    51.     RichTextBox1.SelUnderline = True
    52.     RichTextBox1.SelStart = Len(RichTextBox1.Text)
    53.     RichTextBox1.SelUnderline = False
    54. End If
    55.  
    56. RichTextBox1.TextRTF = Replace(RichTextBox1.TextRTF, BOLDSTART, "")
    57. RichTextBox1.TextRTF = Replace(RichTextBox1.TextRTF, BOLDEND, "")
    58. RichTextBox1.TextRTF = Replace(RichTextBox1.TextRTF, UNDERSTART, "")
    59. RichTextBox1.TextRTF = Replace(RichTextBox1.TextRTF, UNDEREND, "")
    60. RichTextBox1.TextRTF = Replace(RichTextBox1.TextRTF, URLSTART, "")
    61. RichTextBox1.TextRTF = Replace(RichTextBox1.TextRTF, URLEND, "")
    62. DetectURL RichTextBox1, True
    63.  
    64. End Sub
    65.  
    66. Private Sub DetectURL(p_RichText As Object, p_blnDetect As Boolean)
    67.     Dim lngRet As Long
    68.     Dim strText As String
    69.    
    70.     With p_RichText
    71.         ' this line is needed because the function will not update the
    72.         ' url if you had it before
    73.         strText = .TextRTF
    74.         ' send message to detect urls
    75.         ' notice the Abs function. This is needed to pass 0 or 1
    76.         ' in VB true is -1, so we have to get the absolute value of that
    77.         lngRet = SendMessage(RichTextBox1.hwnd, EM_AUTOURLDETECT, Abs(p_blnDetect), ByVal 0)
    78.         ' rewrite the text into the RichText so it will change all URLs if you
    79.         'had them before
    80.         .TextRTF = strText
    81.     End With
    82. End Sub
    83.  
    84. Private Sub Form_Load()
    85.  
    86.     RichTextBox1.Text = "[//b]hi[///b]The URL to click is [url]http://www.vbforums.com[/url].  Please click it." & vbCrLf
    87.    
    88. End Sub
    89.  
    90. Private Sub RichTextBox1_Change()
    91. '    DetectURL RichTextBox1, True
    92. '    RichTextBox1.SelStart = Len(RichTextBox1.Text)
    93. End Sub
    94.  
    95.  
    96. Private Sub RichTextBox1_Click()
    97.  
    98.     Dim lngRetVal As Long
    99.    
    100.     lngRetVal = SendMessage(RichTextBox1.hwnd, EM_GETSEL, 0, 0)
    101.    
    102.     Dim strBuffer As String, intInStr As Integer, intHi As Integer, intLo As Integer
    103.    
    104.     intHi = HiWord(lngRetVal) + 1
    105.     intLo = LoWord(lngRetVal) + 1
    106.    
    107.     intInStr = InStrRev(RichTextBox1.Text, " ", intLo)
    108.    
    109.     If intInStr = 0 Then 'no space
    110.         strBuffer = Mid(RichTextBox1.Text, 1, intLo)
    111.     Else
    112.         strBuffer = Mid(RichTextBox1.Text, intInStr + 1)
    113.     End If
    114.    
    115.     strBuffer = Trim(strBuffer)
    116.     intInStr = InStr(1, strBuffer, " ")
    117.    
    118.     If intInStr <> 0 Then
    119.         strBuffer = Mid(strBuffer, 1, intInStr - 1)
    120.     End If
    121.    
    122.     If InStr(1, strBuffer, "http:") = 0 And _
    123.         InStr(1, strBuffer, "file:") = 0 And _
    124.         InStr(1, strBuffer, "mailto:") = 0 And _
    125.         InStr(1, strBuffer, "ftp:") = 0 And _
    126.         InStr(1, strBuffer, "https:") = 0 And _
    127.         InStr(1, strBuffer, "gopher:") = 0 And _
    128.         InStr(1, strBuffer, "nntp:") = 0 And _
    129.         InStr(1, strBuffer, "prospero:") = 0 And _
    130.         InStr(1, strBuffer, "telnet:") = 0 And _
    131.         InStr(1, strBuffer, "news:") = 0 And _
    132.         InStr(1, strBuffer, "wais:") = 0 Then Exit Sub
    133.        
    134.     Debug.Print strBuffer
    135.    
    136. End Sub
    137.  
    138. Private Function LoWord(ByVal DWord As Long) As Long
    139.   If DWord And &H8000& Then
    140.     LoWord = DWord Or &HFFFF0000
    141.   Else
    142.     LoWord = DWord And &HFFFF&
    143.   End If
    144. End Function
    145.  
    146. Private Function HiWord(ByVal DWord As Long) As Long
    147.   HiWord = (DWord And &HFFFF0000) \ &H10000
    148. End Function

  20. #20

    Thread Starter
    Lively Member lavarock09's Avatar
    Join Date
    Jun 2005
    Posts
    124

    Re: Searching Text

    The url doesn't work, it turns into and underlined blue link, but when I click it, it does nothing

  21. #21

  22. #22
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Searching Text

    You probably want to check for more than a space after the end of the URL, as it is now it will include a "." (dot) character in the URL when you click it. This is because it only takes away from a space and the string after the URL is ". Please click it". Hence the dot. To make it foolproof, check for dots, question marks, exclamation marks, etc aswell.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  23. #23

    Thread Starter
    Lively Member lavarock09's Avatar
    Join Date
    Jun 2005
    Posts
    124

    Re: Searching Text

    Quote Originally Posted by chemicalNova
    You probably want to check for more than a space after the end of the URL, as it is now it will include a "." (dot) character in the URL when you click it. This is because it only takes away from a space and the string after the URL is ". Please click it". Hence the dot. To make it foolproof, check for dots, question marks, exclamation marks, etc aswell.

    chem
    Explain

    Marty, I in the box i have typed my website address, http://www.m3xvv.co.uk

    and if I click inbetween the 3 and the x it opens up IE, but the address bar and site only points to http://www.m3/

  24. #24

  25. #25

    Thread Starter
    Lively Member lavarock09's Avatar
    Join Date
    Jun 2005
    Posts
    124

    Re: Searching Text

    Thanks

  26. #26
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Searching Text

    Please follow the instructions in my signature to mark your thread as resolved if your question has been answered. Thanks.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  27. #27
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Searching Text

    Unfortunately I don't understand the HiWord and LoWord code but the problem seems to lie there. I found however that if you modify the RichTextBox1_Click like I show below, it will work. However since I really don't understand what's going on, you may have problems with other urls.

    VB Code:
    1. '    If intInStr = 0 Then 'no space
    2. '        strBuffer = Mid(RichTextBox1.Text, 1, intLo)
    3. '    Else
    4.         strBuffer = Mid(RichTextBox1.Text, intInStr + 1)
    5. '    End If

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