|
-
Jul 19th, 2005, 03:50 PM
#1
Thread Starter
Lively Member
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
-
Jul 19th, 2005, 03:57 PM
#2
Re: Searching Text
You search like this:
VB Code:
Dim pos as integer
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.
-
Jul 19th, 2005, 04:05 PM
#3
Thread Starter
Lively Member
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
-
Jul 19th, 2005, 04:12 PM
#4
Re: Searching Text
You won't be able to do that in a textbox but you can with a RichTextbox. Give me a few minutes and I'll have an example.
-
Jul 19th, 2005, 04:14 PM
#5
Thread Starter
Lively Member
Re: Searching Text
richtextbox is fine, as long as it works
-
Jul 19th, 2005, 04:35 PM
#6
Re: Searching Text
OK here's an example.
VB Code:
Option Explicit
Private Const BOLDSTART = "[//b]"
Private Const BOLDEND = "[///b]"
Private Sub Form_Load()
End Sub
Private Sub RichTextBox1_Change()
Dim intStart As Integer
Dim intEnd As Integer
intStart = InStr(1, RichTextBox1.Text, BOLDSTART)
intEnd = InStr(1, RichTextBox1.Text, BOLDEND)
If intStart > 0 And intEnd > 0 Then
RichTextBox1.Text = Replace(RichTextBox1.Text, BOLDSTART, "")
RichTextBox1.Text = Replace(RichTextBox1.Text, BOLDEND, "")
RichTextBox1.SelStart = intStart - 1
RichTextBox1.SelLength = intEnd - intStart
RichTextBox1.SelBold = True
RichTextBox1.SelStart = Len(RichTextBox1.Text)
RichTextBox1.SelBold = False
End If
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.
-
Jul 19th, 2005, 04:40 PM
#7
Thread Starter
Lively Member
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?
-
Jul 19th, 2005, 04:46 PM
#8
Thread Starter
Lively Member
Re: Searching Text
Ah, I now have a problem
I have added Underline
this is my code
VB Code:
Option Explicit
Private Const BOLDSTART = "[//b]"
Private Const BOLDEND = "[///b]"
Private Const UNDERSTART = "[//u]"
Private Const UNDEREND = "[///u]"
Private Sub RichTextBox1_Change()
Dim intStart As Integer
Dim intEnd As Integer
intStart = InStr(1, RichTextBox1.Text, BOLDSTART)
intEnd = InStr(1, RichTextBox1.Text, BOLDEND)
End Sub
Private Sub Command1_Click()
Dim intStart As Integer
Dim intEnd As Integer
intStart = InStr(1, RichTextBox1.Text, BOLDSTART)
intEnd = InStr(1, RichTextBox1.Text, BOLDEND)
If intStart > 0 And intEnd > 0 Then
RichTextBox1.Text = Replace(RichTextBox1.Text, BOLDSTART, "")
RichTextBox1.Text = Replace(RichTextBox1.Text, BOLDEND, "")
RichTextBox1.SelStart = intStart - 1
RichTextBox1.SelLength = intEnd - intStart
RichTextBox1.SelBold = True
RichTextBox1.SelStart = Len(RichTextBox1.Text)
RichTextBox1.SelBold = False
End If
Dim intStart2 As Integer
Dim intEnd2 As Integer
intStart2 = InStr(1, RichTextBox1.Text, UNDERSTART)
intEnd2 = InStr(1, RichTextBox1.Text, UNDEREND)
If intStart2 > 0 And intEnd2 > 0 Then
RichTextBox1.Text = Replace(RichTextBox1.Text, UNDERSTART, "")
RichTextBox1.Text = Replace(RichTextBox1.Text, UNDEREND, "")
RichTextBox1.SelStart = intStart2 - 1
RichTextBox1.SelLength = intEnd2 - intStart
RichTextBox1.SelUnderline = True
RichTextBox1.SelStart = Len(RichTextBox1.Text)
RichTextBox1.SelUnderline = False
End If
End Sub
but it makes some letters after the [///b] bold
-
Jul 19th, 2005, 04:56 PM
#9
Re: Searching Text
I haven't tested it but it looks like this may be a part of the problem.
RichTextBox1.SelLength = intEnd2 - intStart
should be
RichTextBox1.SelLength = intEnd2 - intStart2
-
Jul 19th, 2005, 05:00 PM
#10
Thread Starter
Lively Member
Re: Searching Text
In the richtextbox I have typed,
[//b]Hi[///b]
[//u]Hi[///u]
and it outputs
Hi
Hi
-
Jul 19th, 2005, 05:14 PM
#11
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.
-
Jul 19th, 2005, 05:15 PM
#12
Thread Starter
Lively Member
Re: Searching Text
I'm using the click event, I changed it
-
Jul 19th, 2005, 05:54 PM
#13
Re: Searching Text
I just got this working in the changed event: seems to work
VB Code:
Private Sub RichTextBox1_Change()
Static intStart1 As Integer
Static intEnd1 As Integer
Static intStart2 As Integer
Static intEnd2 As Integer
intStart1 = InStr(1, RichTextBox1.Text, BOLDSTART)
If intStart1 > 0 Then
intEnd1 = InStr(intStart1, RichTextBox1.Text, BOLDEND)
End If
If intEnd1 > 0 Then
RichTextBox1.Text = Replace(RichTextBox1.Text, BOLDSTART, "")
RichTextBox1.Text = Replace(RichTextBox1.Text, BOLDEND, "")
RichTextBox1.SelStart = intStart1 - 1
RichTextBox1.SelLength = intEnd1 - intStart1
RichTextBox1.SelBold = True
RichTextBox1.SelStart = Len(RichTextBox1.Text)
RichTextBox1.SelBold = False
intEnd1 = 0
End If
intStart2 = InStr(1, RichTextBox1.Text, UNDERSTART)
If intStart2 > 0 Then
intEnd2 = InStr(intStart2, RichTextBox1.Text, UNDEREND)
End If
If intEnd2 > 0 Then
RichTextBox1.Text = Replace(RichTextBox1.Text, UNDERSTART, "")
RichTextBox1.Text = Replace(RichTextBox1.Text, UNDEREND, "")
RichTextBox1.SelStart = intStart2 - 1
RichTextBox1.SelLength = intEnd2 - intStart2
RichTextBox1.SelUnderline = True
RichTextBox1.SelStart = Len(RichTextBox1.Text)
RichTextBox1.SelUnderline = False
intEnd2 = 0
End If
End Sub
-
Jul 19th, 2005, 06:03 PM
#14
Re: Searching Text
What's the point of making the variables Static?
-
Jul 19th, 2005, 06:27 PM
#15
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.
-
Jul 20th, 2005, 08:13 AM
#16
Thread Starter
Lively Member
Re: Searching Text
 Originally Posted by dglienna
I just got this working in the changed event: seems to work
VB Code:
Private Sub RichTextBox1_Change()
Static intStart1 As Integer
Static intEnd1 As Integer
Static intStart2 As Integer
Static intEnd2 As Integer
intStart1 = InStr(1, RichTextBox1.Text, BOLDSTART)
If intStart1 > 0 Then
intEnd1 = InStr(intStart1, RichTextBox1.Text, BOLDEND)
End If
If intEnd1 > 0 Then
RichTextBox1.Text = Replace(RichTextBox1.Text, BOLDSTART, "")
RichTextBox1.Text = Replace(RichTextBox1.Text, BOLDEND, "")
RichTextBox1.SelStart = intStart1 - 1
RichTextBox1.SelLength = intEnd1 - intStart1
RichTextBox1.SelBold = True
RichTextBox1.SelStart = Len(RichTextBox1.Text)
RichTextBox1.SelBold = False
intEnd1 = 0
End If
intStart2 = InStr(1, RichTextBox1.Text, UNDERSTART)
If intStart2 > 0 Then
intEnd2 = InStr(intStart2, RichTextBox1.Text, UNDEREND)
End If
If intEnd2 > 0 Then
RichTextBox1.Text = Replace(RichTextBox1.Text, UNDERSTART, "")
RichTextBox1.Text = Replace(RichTextBox1.Text, UNDEREND, "")
RichTextBox1.SelStart = intStart2 - 1
RichTextBox1.SelLength = intEnd2 - intStart2
RichTextBox1.SelUnderline = True
RichTextBox1.SelStart = Len(RichTextBox1.Text)
RichTextBox1.SelUnderline = False
intEnd2 = 0
End If
End Sub
This still doesn't stop this happening,
Input
[//b]Hi[///b]
[//u]Hi[///u]
Output
Hi
Hi
-
Jul 20th, 2005, 10:14 AM
#17
Re: Searching Text
Sorry about that. There were some mistakes in my original code.
VB Code:
Option Explicit
Private Const BOLDSTART = "[//b]"
Private Const BOLDEND = "[///b]"
Private Const UNDERSTART = "[//u]"
Private Const UNDEREND = "[///u]"
Private Sub Command1_Click()
Dim intStart As Integer
Dim intEnd As Integer
Dim intStart2 As Integer
Dim intEnd2 As Integer
intStart = InStr(1, RichTextBox1.Text, BOLDSTART) + Len(BOLDSTART)
intEnd = InStr(1, RichTextBox1.Text, BOLDEND) - 1
intStart2 = InStr(1, RichTextBox1.Text, UNDERSTART) + Len(UNDERSTART)
intEnd2 = InStr(1, RichTextBox1.Text, UNDEREND) - 1
If intStart > 0 And intEnd > 0 Then
RichTextBox1.SelStart = intStart - 1
RichTextBox1.SelLength = intEnd - intStart + 1
RichTextBox1.SelBold = True
RichTextBox1.SelStart = Len(RichTextBox1.Text)
RichTextBox1.SelBold = False
End If
If intStart2 > 0 And intEnd2 > 0 Then
RichTextBox1.SelStart = intStart2 - 1
RichTextBox1.SelLength = intEnd2 - intStart2 + 1
RichTextBox1.SelUnderline = True
RichTextBox1.SelStart = Len(RichTextBox1.Text)
RichTextBox1.SelUnderline = False
End If
RichTextBox1.TextRTF = Replace(RichTextBox1.TextRTF, BOLDSTART, "")
RichTextBox1.TextRTF = Replace(RichTextBox1.TextRTF, BOLDEND, "")
RichTextBox1.TextRTF = Replace(RichTextBox1.TextRTF, UNDERSTART, "")
RichTextBox1.TextRTF = Replace(RichTextBox1.TextRTF, UNDEREND, "")
-
Jul 20th, 2005, 02:08 PM
#18
Thread Starter
Lively Member
Re: Searching Text
Thanks, thats alot better, but how would I go about doing URL's and IMG's
-
Jul 20th, 2005, 02:50 PM
#19
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:
Option Explicit
Private Const WM_USER As Long = &H400
Private Const EM_AUTOURLDETECT As Long = (WM_USER + 91)
Private Const EM_GETSEL As Long = &HB0
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
Private Const BOLDSTART = "[//b]"
Private Const BOLDEND = "[///b]"
Private Const UNDERSTART = "[//u]"
Private Const UNDEREND = "[///u]"
Private Const URLSTART = "[//url]"
Private Const URLEND = "[///url]"
Private Sub Command1_Click()
Dim intStart As Integer
Dim intEnd As Integer
Dim intStart2 As Integer
Dim intEnd2 As Integer
intStart = InStr(1, RichTextBox1.Text, BOLDSTART) + Len(BOLDSTART)
intEnd = InStr(1, RichTextBox1.Text, BOLDEND) - 1
intStart2 = InStr(1, RichTextBox1.Text, UNDERSTART) + Len(UNDERSTART)
intEnd2 = InStr(1, RichTextBox1.Text, UNDEREND) - 1
If intStart > 0 And intEnd > 0 Then
RichTextBox1.SelStart = intStart - 1
RichTextBox1.SelLength = intEnd - intStart + 1
RichTextBox1.SelBold = True
RichTextBox1.SelStart = Len(RichTextBox1.Text)
RichTextBox1.SelBold = False
End If
If intStart2 > 0 And intEnd2 > 0 Then
RichTextBox1.SelStart = intStart2 - 1
RichTextBox1.SelLength = intEnd2 - intStart2 + 1
RichTextBox1.SelUnderline = True
RichTextBox1.SelStart = Len(RichTextBox1.Text)
RichTextBox1.SelUnderline = False
End If
RichTextBox1.TextRTF = Replace(RichTextBox1.TextRTF, BOLDSTART, "")
RichTextBox1.TextRTF = Replace(RichTextBox1.TextRTF, BOLDEND, "")
RichTextBox1.TextRTF = Replace(RichTextBox1.TextRTF, UNDERSTART, "")
RichTextBox1.TextRTF = Replace(RichTextBox1.TextRTF, UNDEREND, "")
RichTextBox1.TextRTF = Replace(RichTextBox1.TextRTF, URLSTART, "")
RichTextBox1.TextRTF = Replace(RichTextBox1.TextRTF, URLEND, "")
DetectURL RichTextBox1, True
End Sub
Private Sub DetectURL(p_RichText As Object, p_blnDetect As Boolean)
Dim lngRet As Long
Dim strText As String
With p_RichText
' this line is needed because the function will not update the
' url if you had it before
strText = .TextRTF
' send message to detect urls
' notice the Abs function. This is needed to pass 0 or 1
' in VB true is -1, so we have to get the absolute value of that
lngRet = SendMessage(RichTextBox1.hwnd, EM_AUTOURLDETECT, Abs(p_blnDetect), ByVal 0)
' rewrite the text into the RichText so it will change all URLs if you
'had them before
.TextRTF = strText
End With
End Sub
Private Sub Form_Load()
RichTextBox1.Text = "[//b]hi[///b]The URL to click is [url]http://www.vbforums.com[/url]. Please click it." & vbCrLf
End Sub
Private Sub RichTextBox1_Change()
' DetectURL RichTextBox1, True
' RichTextBox1.SelStart = Len(RichTextBox1.Text)
End Sub
Private Sub RichTextBox1_Click()
Dim lngRetVal As Long
lngRetVal = SendMessage(RichTextBox1.hwnd, EM_GETSEL, 0, 0)
Dim strBuffer As String, intInStr As Integer, intHi As Integer, intLo As Integer
intHi = HiWord(lngRetVal) + 1
intLo = LoWord(lngRetVal) + 1
intInStr = InStrRev(RichTextBox1.Text, " ", intLo)
If intInStr = 0 Then 'no space
strBuffer = Mid(RichTextBox1.Text, 1, intLo)
Else
strBuffer = Mid(RichTextBox1.Text, intInStr + 1)
End If
strBuffer = Trim(strBuffer)
intInStr = InStr(1, strBuffer, " ")
If intInStr <> 0 Then
strBuffer = Mid(strBuffer, 1, intInStr - 1)
End If
If InStr(1, strBuffer, "http:") = 0 And _
InStr(1, strBuffer, "file:") = 0 And _
InStr(1, strBuffer, "mailto:") = 0 And _
InStr(1, strBuffer, "ftp:") = 0 And _
InStr(1, strBuffer, "https:") = 0 And _
InStr(1, strBuffer, "gopher:") = 0 And _
InStr(1, strBuffer, "nntp:") = 0 And _
InStr(1, strBuffer, "prospero:") = 0 And _
InStr(1, strBuffer, "telnet:") = 0 And _
InStr(1, strBuffer, "news:") = 0 And _
InStr(1, strBuffer, "wais:") = 0 Then Exit Sub
Debug.Print strBuffer
End Sub
Private Function LoWord(ByVal DWord As Long) As Long
If DWord And &H8000& Then
LoWord = DWord Or &HFFFF0000
Else
LoWord = DWord And &HFFFF&
End If
End Function
Private Function HiWord(ByVal DWord As Long) As Long
HiWord = (DWord And &HFFFF0000) \ &H10000
End Function
-
Jul 20th, 2005, 03:36 PM
#20
Thread Starter
Lively Member
Re: Searching Text
The url doesn't work, it turns into and underlined blue link, but when I click it, it does nothing
-
Jul 20th, 2005, 05:43 PM
#21
Re: Searching Text
Change the bottom of the Richtextbox click event to
VB Code:
'Debug.Print strBuffer
Call ShellExecute(Me.hwnd, "OPEN", strBuffer, vbNullString, vbNullString, 5)
-
Jul 20th, 2005, 06:34 PM
#22
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
-
Jul 21st, 2005, 10:21 AM
#23
Thread Starter
Lively Member
Re: Searching Text
 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/
-
Jul 21st, 2005, 11:39 AM
#24
Re: Searching Text
As I mentioned it's not my code but I'll try to figure out what is wrong.
-
Jul 21st, 2005, 11:44 AM
#25
Thread Starter
Lively Member
-
Jul 21st, 2005, 11:51 AM
#26
Re: Searching Text
Please follow the instructions in my signature to mark your thread as resolved if your question has been answered. Thanks.
-
Jul 21st, 2005, 11:55 AM
#27
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:
' If intInStr = 0 Then 'no space
' strBuffer = Mid(RichTextBox1.Text, 1, intLo)
' Else
strBuffer = Mid(RichTextBox1.Text, intInStr + 1)
' 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|