|
-
Jul 22nd, 2009, 09:07 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] How to find character number in a Rich Text Box
I have a (physically) large rich text box into which is imported a pretty large text file comprising of 250,000 characters plus.
I also have loaded the selfsame text into a STRING variable.
Using the mouse, up/down keys and scrollbars I can navigate my way around the large amount of text in the RT Box. So far, so good.
My need is now to be able to left-click the mouse at a point in the text and to find out what is the character number in the total sequence upon which I have clicked. For example, if about half way through a 250,000 character sequence, the procedure should return to me somehow the number 125,000.
The character sequence number is needed in order subsequently to perform STRING manipulation (left$, mid$, right$, instr etc.)
Hopefully there is a way to do this from the mouseclick event of the RT Box?
Suggestions appreciated please.
camoore
Wales, UK
Last edited by camoore; Jul 22nd, 2009 at 09:09 AM.
Reason: typo
-
Jul 22nd, 2009, 09:11 AM
#2
Re: How to find character number in a Rich Text Box
Isn't the rtb's .SelStart property what you are looking for?
-
Jul 22nd, 2009, 09:17 AM
#3
Thread Starter
Hyperactive Member
Re: How to find character number in a Rich Text Box
Thank you LaVolpe. I will look this up when I get home (2-3 days).
Much obliged for you help once more. This has to do with the importing of .txt, .rtf and .doc files into a RTB (with which you assisted previously). . I am now working to add to this .dbx (e-mail files).
Will make a proper answer once home. VMT.
camoore
-
Jul 23rd, 2009, 10:49 AM
#4
Thread Starter
Hyperactive Member
Re: How to find character number in a Rich Text Box
Hi LaVolpe.
I tried your selstart idea while still on my walkabout. It works, but only up to a point.
What I am trying to achieve is the ability to click on a character of a text message (or other text content) of a Rich Text Box and have my program display to me what the character is and what is its ASCII value. Why ? you may ask. The reason is that when I load various files such as .rtf, .doc and latest .dbx into the RTB using the loadTEXT method, all sorts of "wierd" characters appear which show as just a broad vertical black line. What I need to do is to analyse what these characters are, so that I can write software to extract the VALID TEXT from all the background stuff.
So I want to be able to left-click on a character, to find out what the character number is in the RTB content and then to analyse it.
The selstart routine you suggested returns a number. Let's say it is 2345.
For analysis, what I then want to do is to go to a STRING variable which contains the same data as has been loaded into the RTB and to use :
STRINGVAR = RichTextBox1.Text
CHAR = Mid$(STRINGVAR,2345,1) to return and display the character CHAR (in a text box or similar, plus its ascii value).
The problem is this. Selstart seems to return the DISPLAYED character number of that clicked on reliably. However the associated STRING variable also contains a load of characters which the RTB does not display (eg. line feeds, paragraphs etc.) Thus when I search the STRING for character 2345, I find something, but not what I clicked on, because all those other non-RTB displayed characters are also counted.
The process ONLY works on the first line of text in the RTB, before any end-of-line codes have been added. The more lines through a text I seek to click on, the worse is the "offset".
So I seek further suggestions as to what to try please. I hope that I have explained the problem adequately.
camoore
Wales, UK
-
Jul 23rd, 2009, 11:21 AM
#5
Re: How to find character number in a Rich Text Box
I think you are going to have a bit of an issue by just clicking on a character
Let's say the text is this: "Hello" & vbCrLF & "World"
If you click after the letter o in Hello, .SelStart returns the letter o
If you click just before the letter W, .SelStart would return vbLF
So as you can see, you will never get the vbCr (Ascii 13) by just using .SelStart. You may have to get the character before and after the cursor by also using .SelStart-1.
Keep in mind that if cursor is in front of 1st char, 1st line, .SelStart is zero, so .SelStart-1 will be an error.
Another thing to keep in mind is that a richtextbox won't display all characters. If an RTF file is loaded, all that RTF encoding is not displayed but the text is bolded, colored, etc, etc. Therefore the length of the visible richtextbox.text will always be < the data loaded into a string variable.
Have you considered possibly using a standard text box, granted its text length is limited to about 64kb. And a last resort which is not GUI is to load the string into a byte array and start searching for any bytes that are not printable: usually in the range of 0 to 31, but 10=vbLF, 13=vbCR, 9=vbTab
-
Jul 23rd, 2009, 03:12 PM
#6
Re: How to find character number in a Rich Text Box
I have to agree with the Fox. The rich text box probably only displays about 80% of the ASCII characters that the RTF file actually contains, if that. There are embedded (and hidden) characters everywhere that control fonts, styles, paragraphs, indents, tabs, etc. Also, embedded images occupy oodles of space.
Your best bet to find the exact byte location of the cursor relative to the text that is shown is to use the standard text box.
A few 3rd-party proprietary controls, such as TxText, will allow a rather close approximation, but even that is somewhat involved.
-
Jul 23rd, 2009, 07:02 PM
#7
Frenzied Member
Re: How to find character number in a Rich Text Box
Okay, so let me see if I have this problem right...
OP is trying to keep selstart and actual byte position of file equal to each other...
Okay, if and only if each new line is equal to vbNewLine or vbCrLf meaning two bytes then could you not get the line count of current cursors position and multiply it by two?
Code:
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 Const WM_USER = &H400
Private Const EM_EXLINEFROMCHAR = (WM_USER + 54)
Private SomeSub()
Dim CursorPosition As Long, RowCount As Long
CursorPosition = RTB.SelStart
RowCount = SendMessage(RTB.hwnd, EM_EXLINEFROMCHAR, 0, ByVal CursorPosition) + 1
MsgBox "Cursor Is On Line " & RowCount
End Sub
Good Luck
Option Explicit should not be an Option!
-
Jul 27th, 2009, 04:16 PM
#8
Thread Starter
Hyperactive Member
Re: How to find character number in a Rich Text Box
This is a quick acknowledgement, of further thanks for comments / suggestions.
I am still on walkabout, and will respond better when I get home, in two days.
I re-wrote the area of code I was using to apply the SELSTART process in the RTB, and I now have it working. Maybe I had made a coding error - very likely. I will post my working code as a conclusion to this topic.
One thing which would help a lot. Having left-clicked on a character and got it displayed (which now works well) I would like to be able to use the left - right arrow keys to move the "clicked point" back or forwards through the text and to have the appropriate adjacent character displayed. At present I am using two command keys to do this, but this is a bit awkward.
Since not at home, my ability to work on code is limited and I have to rely on what is stored in the brain rather than my notes and text books. The brain grows old!
I think that the main topic has been solved. It just remains to find out how to use those l - r arrow keys to modify the selstart property I think.
camoore
Wales, UK
-
Jul 27th, 2009, 05:19 PM
#9
Re: How to find character number in a Rich Text Box
Use your keyup events. If the KeyCode is vbKeyLeft or vbKeyRight then the user released the key and you can check the new selstart that was set
Last edited by LaVolpe; Jul 27th, 2009 at 05:30 PM.
-
Aug 2nd, 2009, 05:43 PM
#10
Thread Starter
Hyperactive Member
Re: How to find character number in a Rich Text Box
This post is to acknowledge with thanks the help received.
I am trying to use the Richtextbox as a method of importing different types of files (.txt, .rtf, .doc and .dbx) into a VB6 program, and then to extract from them their basic text content - for subsequent parsing of data content.
The above suggestions in this thread have enabled me to solve the immediate problems.
Using the KEYUP event, I have now been able to implement l/r/up/down and am able faithfully to extract and display a character from a RT Box.
There continues to be a problem with a Chr(13) [linefeed] since examination of the RTB seems to skip over these (using the left arrow) rather than allowing them to be displayed.
I can live with this for now, but am continuing to work on it.
The Rich Text Box seems to be a powerful and essential tool in interfacing between a VB6 program and other file types. Yet in all the good VB6 textbooks I have, very little space is allocated to it, if any! The SELSTART property solved my immediate problem (thanks) but I wonder where is to be found a really thorough description of what the RTBox can do?
Thank you all for your replies. I will mark this thread as solved.
camoore
Wales. UK
Last edited by camoore; Aug 2nd, 2009 at 05:51 PM.
Reason: typos
-
Aug 2nd, 2009, 06:58 PM
#11
Re: [RESOLVED] How to find character number in a Rich Text Box
Camoore said, "The Rich Text Box seems to be a powerful and essential tool in interfacing between a VB6 program and other file types. Yet in all the good VB6 textbooks I have, very little space is allocated to it, if any! The SELSTART property solved my immediate problem (thanks) but I wonder where is to be found a really thorough description of what the RTBox can do?"
-----------------
It is truly sad that Microsoft discontinued to support enhancements to the VB Rich Text Box control years ago. So, a thorough description of what the RTBox can do is going to be almost impossible to find, and if you find one, it will likely be out of date.
I have worked with RTF files for years, but the only way that I could truly extract from them their most powerful assets was to abandon the VB6 RTF control and use a 3rd-party control. Regardless, even that control was still somewhat limited in what it could do with that file structure.
Today an RTF file created using MS Word will contain so much file overhead (over 50% is useless) that I am surprised that anyone would ever want to use them at all in their application (.Net or VB6). I have recently written code that compresses RTF files containing images, and that code will compress them by better than 80%. To me, that means that 80% of the information stored in the file by MS Word is complete garbage.
Last edited by Code Doc; Aug 3rd, 2009 at 03:11 PM.
Doctor Ed
-
Aug 3rd, 2009, 05:36 AM
#12
Thread Starter
Hyperactive Member
Re: [RESOLVED] How to find character number in a Rich Text Box
A pity indeed, since it would seem that the RTB is (was) a very capable control.
This thread is connected with another query which I raised as thread #575717 (resolved thanks to Forum help).
I have found that the following rather curious code sequence allows the importation of .txt, .rtf, .doc and .dbx files into the RT Box :
Dim Path as String 'then set path to, for example, C:\testdoc.doc, or any other valid path to a document file of one of the above types
Dim Import as String
RichTextBox1.LoadFile Path, rtfText
IMPORT = RichTextBox1 'Note these two lines. Wierd, but essential
RichTextBox1.Text = IMPORT
Note that the Loadfile Path method used is always rtf.Text.
If I just use : RichTextBox1.LoadFile Path, rtfText then the other file types load into the RT Box, but their characters are not properly displayed - I get a vast number of thick black vertical line characters displayed. But by adding the strange extra 2 lines of code, all the characters become visible.
I have subsequently managed to write a program in VB6 which cuts off the huge overheads of code in .rtf, .doc and .dbx files and just extracts the basic text - which was my objective. The program does not deal with graphics and images, just the pure text element.
I needed the answer to the question of this thread in order to be able to navigate my way around the RT Box once loaded and analyse the exact character sequences in order to write string processing software which parses out the valid text content from all the "dross".
It works! With .dbx files I am left with just the occasional "gash" characters, but they are perfectly readable. The test .dbx file I have used contained almost 1,000,000 characters.
Thanks again for your help and comments.
camoore
Wales, UK
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
|