Solved

Find text in Richtextbox and then add it to a string

Posted on 2001-09-12
19
419 Views
Last Modified: 2007-11-27
Dear all,

I am doing the following:
-Find something in a richtextbox control.
   a=richtextbox.find(Searchfor,0)
-Find somethine else in the same richtextbox control.
   b=richtextbox.find(Searchfor2,a)

Then I'll get two numbers.. a=index of the first found and b=index of the second found.
My problem is then to put this into a string.
What I've done is;

String=mid(richtextbox.text,a,b-a)

The textbox control downloads text from webpages, and with some of them it works fine, so I wonder if a richtextbox control may have hidden characters for the mid function?

Please help =)
0
Comment
Question by:thomas_mathiesen
  • 10
  • 4
  • 4
  • +1
19 Comments
 
LVL 4

Expert Comment

by:wileecoy
Comment Utility
Actually, the RTB does have hidden chrs, but only if you were using RichTextBox.RTFText.

Since you are using .Text, it will be what is displayed in the RTB.

Since you are doing webpages, might it be the html tags that are in your way?

I have done a lot with this.  I have a whole app just for displaying RTB's and collecting data dynamically from each file.

I use the find function in addition to the InStr and InStrRev functions to get the position of certain text.

If you can post your code and I can test it, I am close to 100% sure that I can figure it out (much sweat getting my app to work).

Let me know if it's the html tags, or post your code and I will test.

Also - let me know if e-mail would be better.

Thanks

Wileecoy.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
If all you want to do is concatenate to two string than this would be the way to go:

Debug.Print Mid(RichTextBox.Text, a, Len(Searchfor)) & Mid(RichTextBox.Text, b, Len(Searchfor2))

Anthony
0
 
LVL 1

Author Comment

by:thomas_mathiesen
Comment Utility
Wileecoy,
sounds good, but first let me ask u;
Can I turn off the RTB function in the textbox? I need to use the richtextbox control, because the normal textbox cannot contain the number of characters I need.
And, yes, I am looking for tags, and I need to rip text between two tags. That's why I first find the first and then the second. At the end; grab the string between these two.

I'll send u the code (email).

Acperkins,
What I do:
Text: "blah1 blah2 blah3 blah4"
a=Find"blah1"
b=Find"blah4"
String=mid(textbox,a,b-a)
So that the string will be "blah1 blah2 blah3 balh4"
0
 
LVL 1

Author Comment

by:thomas_mathiesen
Comment Utility
Wileecoy,
sounds good, but first let me ask u;
Can I turn off the RTB function in the textbox? I need to use the richtextbox control, because the normal textbox cannot contain the number of characters I need.
And, yes, I am looking for tags, and I need to rip text between two tags. That's why I first find the first and then the second. At the end; grab the string between these two.

I'll send u the code (email).

Acperkins,
What I do:
Text: "blah1 blah2 blah3 blah4"
a=Find"blah1"
b=Find"blah4"
String=mid(textbox,a,b-a)
So that the string will be "blah1 blah2 blah3 balh4"
0
 
LVL 1

Author Comment

by:thomas_mathiesen
Comment Utility
What is ur email Wileecoy?
0
 
LVL 1

Author Comment

by:thomas_mathiesen
Comment Utility
What is ur email Wileecoy?
0
 
LVL 1

Author Comment

by:thomas_mathiesen
Comment Utility
What is ur email Wileecoy?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
I suspect that Wileecoy is right about the HTML characters, but to answer your question
>>
What I do:
Text: "blah1 blah2 blah3 blah4"
a=Find"blah1"
b=Find"blah4"
String=mid(textbox,a,b-a)
So that the string will be "blah1 blah2 blah3 balh4"
<<

If a < b Then
 String = mid(textbox, a, b + Len("blah4") - 1)
else
 String = mid(textbox, b, a + Len("blah1") - 1)
end if
0
 
LVL 4

Expert Comment

by:wileecoy
Comment Utility
Please send to JakeAustinLane@aol.com

Thanks.

Wileecoy.

p.s. - I will post any pertinent code so that anyone in the future who 'buys' this question will be able to see the progress and resolution.

0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 1

Author Comment

by:thomas_mathiesen
Comment Utility
Here's the code:
I have a richtextbox control (txttemp).

Public Sub FindText()
On Error GoTo FindTextERR
Dim FoundFile As String
Dim a, b As Long
a = 0
b = 0
Do While a > -1
    a = frmMain.txtTemp.Find("a href", b)
    b = frmMain.txtTemp.Find(">", a)
    FoundFile = Mid(frmMain.txtTemp.Text, a, b - a)
    DoEvents
Loop
Exit Sub
FindTextERR:
End Sub

I am looking for files linked on a webpage, which should be the FoundFile string.
I suspect that a and b get different index'es than u could use in a Mid function.
Any idea?
0
 
LVL 1

Author Comment

by:thomas_mathiesen
Comment Utility
Do you think has something to do with it?
http://support.microsoft.com/support/kb/articles/Q284/9/48.ASP

/Thomas
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
This is a sample from this page:
     <a class="topMenuLink" href="http://www.devx.com/free/premierclub/premierclub.asp" target="_top">PremierClub</a>

If all you need is http://www.devx.com/free/premierclub/premierclub.asp

Than
a = 0
b = 0
Do While a > -1
   a = frmMain.txtTemp.Find("href=", b)
   b = frmMain.txtTemp.Find("""", a + Len("href=") + 1)
   FoundFile = Mid(frmMain.txtTemp.Text, a, b - a)
   DoEvents
Loop
0
 
LVL 4

Expert Comment

by:wileecoy
Comment Utility
thomas,

What the find method does is returns the character position of the beginning of your search text.

So - you will need to include enough padding to only extract the text you want... for example... the following worked for me.

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<meta name=ProgId content=Word.Document>
<meta name=Generator content="Microsoft Word 9">
<meta name=Originator content="Microsoft Word 9">
<link rel=File-List href="./computers_files/filelist.xml">
<link rel=Edit-Time-Data href="./computers_files/editdata.mso">
<!--[if !mso]>
<style>
v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style>
<![endif]-->
<title>Computer Features</title>
<!--[if gte mso 9]><xml>


I had to change the code to:

Public Sub FindText()
On Error GoTo FindTextERR
Dim FoundFile As String
Dim a, b As Long
a = 0
b = 0
Do While a > -1
   a = frmMain.txtTemp.Find("a href=", b)
   b = frmMain.txtTemp.Find(">", a)
   FoundFile = Mid(frmMain.txtTemp.Text, a + 8, b - 8 - a)
   DoEvents
Loop
Exit Sub
FindTextERR:
End Sub


Also - I would suggest using a search string that is exclusive but all inclusive to your needs.

By this I mean that you should include 'a href=' as a opposed to 'a href'.  the difference will be your word padding in the Mid function, but also the possibility of having the wrong spacing if there is a new or old html tag that doesn't follow the same format as your search string.

Anyway - hope this helps.

hth.

Wileecoy.
0
 
LVL 4

Expert Comment

by:wileecoy
Comment Utility
thomas,

forgive me for bad coding - I would never suggest what I posted above:

FoundFile = Mid(frmMain.txtTemp.Text, a + 8, b - 8 - a)


I was simply stepping through the code and changing it as I went.

It is better to incorporate code more conducive to dynamic functioning.

For example...

dim sFindStr as string

sFindStr = "a href="

....etc

FoundFile = Mid(frmMain.txtTemp.Text, a + len(sFindStr), b - len(sFindStr) - a)

....etc

apologies as my tail is clearly between my legs.

Wileecoy.
0
 
LVL 1

Author Comment

by:thomas_mathiesen
Comment Utility
Guys, I will check this tomorrow.. busy today.
0
 
LVL 1

Author Comment

by:thomas_mathiesen
Comment Utility
Wileecoy,
You showed me just what I had already done.
The thing is that if u download a textfile that comes from a unix box, (using the inet control), the text in the richtextbox and the character's index is not correct. That means that using the mid(string) will see another string than the textbox sees.
Microsoft has tried to explain the problem for me, so what I need is someone that knows how to convert this string into the same format as the richtextbox is.

A very easy example:

Put a few lines in a richtextbox.
Search for the string "Thomas".
It will give u an index where this starts in the textbox.
Use the Found=mid(string,foundindex,6) so that Found=Thomas, BUT if the downloaded text is from a unix box, this is not working.

Any ideas?
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 300 total points
Comment Utility
You may want to take a look at the MidB, InStrB and LenB functions.

Anthony
0
 
LVL 3

Expert Comment

by:jrspano
Comment Utility
you might also have to convert it to unicode.  I don't know what text format unix uses, but vb uses unicode.  what you do see on the unix stuff?  does it give an error?
0
 
LVL 1

Author Comment

by:thomas_mathiesen
Comment Utility
I see the the text as a normal textbox.. can't see anything unusal. The problem is that for each time it finds what I am looking for, the value of a seems to increase too much (and it increases per time).

I thought this could be because I am going futher down into the file, so there are more "strange/invisibile" characters found and added. I don't think my mid function sees the same string as the richtextbox control does.
When I convert it to unicode, it adds a square between every character.

/Thomas
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now