Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Realtime richtext box updating while editing 2

Status
Not open for further replies.

Merlin88

Programmer
Aug 20, 2005
9
Hello, I am glad to have come across a seemingly helpful community as this one! I am a vb 6 developer and a few friends and i are currently stumped on an interesting problem. We need to create a rich text box that can be updated while a user is typing somewhere else in the document. Pretty much we need to update text that is not at the cursor. We have created our own user control, but still haven't been able to figure out how to do that.

we've briefly looked into data grid controls but they do not have the formatting we are looking for and ultimately just isn't as clean.

Any help is greatly appreciated. Thanks!
 
Could you not just set the RichTextBox's text to the text of the document?

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Use the "Sel" properties for any of the many usual font properties like color, size, italics, underline etc.
Eg. Writes "Hello" in yellow BOLD on a blue background
Richtextbox1.Backcolor=VbBlue
Richtextbox1.Text=""

'Then every time you add an extra word -
Richtextbox1.Setfocus
Richtextbox1.SelStart=10000 'Eg set cursor to end of text
Richtextbox1.SelColor=vbYellow (or any VB colour)
Richtextbox1.SelfontSize=20
Richtextbox1.Selbold=True
Richtextbox1.SelText="Hello"' or extra word to add

Optionally set the focus back to the previous control.
Use Richtextbox1.Text="" to start again at the top of the box

 
The problem with the "Sel" properties is that is moves the cursor, and interrupts what the current user is doing, as his/her data is being updated with the most recent copy of the data.

Thank you for your input though, any other suggestions would be appreciated.
 
Ah, I though you meant update it by another user.
Therefore the other way is to create the rich text in a string from variables and feed it to the .textRTF of the box

You could set up a once only template string by reading the rich text from a sample good box you have created with Sel etc.
Then replace all text words with an array in ascending order such as MyWord(1 to 10) and store it in the string.

To fill the box you make a copy of the template (MyString) and use the Replace function to replace each MyWord() with a the new text or phrase you want to show, then

Richtextbox1.textRTF = MyString

You might have to make a few different templates to suit each requirement and you can even change colors and sizes by changing with the color values in the rich text if you are brave.
 
here is a more detailed description.

we want to update the RichTextBox on a different line than what a user is typing without interrupting the user (or moving the RTB cursor).

so as im typing a line, a different line will be updated above/below this user without moving the cursor (because if the RTB cursor moves, the text that is being typed will float with the cursor thus being in the wrong location).

i do like the template idea for changing the colors... we may put that to use once we get this updating feature working.
 
>The problem with the "Sel" properties is that is moves the
cursor

Sure - but you could simply save the current SelStart and SelLength properties, move the selection point, make the changes and then restore the saved settings (and no, the text being typed by the user shouldn't be inserted at the temporary new position; the single-threaded nature of VB should ensure that). Mind you there are some other challenges you might then have to face.

A simpler solution is to use the tom (Text Object Model) interface to the RichTextBox ... Add a reference to tom (if not available in your Referenced browser, use the Browse button and select riched20.dll (which should be inyour System32 folder). Then something like this should work:
Code:
[blue]Option Explicit

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_GETOLEINTERFACE = (WM_USER + 60)

Private Sub InsertText(TargetRTB As RichTextBox, InsertionPoint As Long, strInsertionText As String)
    Dim myIUnknown As IUnknown 'Yes, yes, this is really what Object is ...
    Dim TextRange As ITextRange
    Dim tomDoc As ITextDocument
    
    SendMessage TargetRTB.hwnd, EM_GETOLEINTERFACE, 0&, myIUnknown
    Set tomDoc = myIUnknown
    Set TextRange = tomDoc.Range(InsertionPoint, InsertionPoint)
    TextRange.Text = strInsertionText
End Sub[/blue]

If you want to replace/overwrite text, then you just need to modify the second parameter of

tomDoc.Range(InsertionPoint, InsertionPoint)

appropriately (I'll leave you to figure that out)

Note that you may get some scrolling issues in the RTB is the insertion text is being placed before the RTB's SelStart and the insertion point is within the visible portion of the text.
 
If you are typing on a line below the text being changed, if someone else adds extra letters above where you are typing, every extra added letter will also advance the characters at the point where you are typing. If the cursor does not advance with it then you will overwrite the last letter typed.

Your method would probably only be manageable if you only overwrite existing characters without changing the length of the text.

Perhaps you should look at entering manual text in a separate entry box and only add it to the main rich text when you say press a full stop or return key?
Then the cursor and external alterations in the main box could be managed better independently.

I can just imagine the frustration of someone trying to type while someone else is correcting a previous spelling mistake only to have the text jump so the wrong letter gets corrected or printed!
 
thanks strongm, i will try that code out a little later.

now you understand tedsmith... so here would be my other idea i have. using the same concept... would some sort of datagrid work while doing this? update one cell while another cell would be updated at the same time without moving the cursor; i tried this but didnt see an easy way to do this (am continuing to look into this). on top of that we would run into more issues with coloring different cells different colors, etc.

im sure there has to be a way to do this.

thank you guys for your input, i will try the code a little later this evening and post a response with results.
 
If the data is in discrete batches and not continuous text then you could put it into rows in a an MSAssess table and connect it to a VB6 datagrid.
Only the box you are using will have a cursor.

Using a datagrid is only another more complicated way of having more than one entry box but is easier to manage if your data is simply numbers, money or dates.

Other data cells can be changed independently by using a recordset however you have to refresh the grid to show the original user any changes by others. This also could momentarily interrupt the main user typing if it happened at exactly the same time. Same problem with using a rich text box.
Or you could show the changes only when the user pressed a full stop or return or if no typing had occured for 5 minutes.

You could have a little red light blink to show changes had been made and a button to see them happen.

Another possibility is if the alterations are being done on another computer or by another person, do them on another temporary rich text box then only confirm the changes to the main box when the main user presses a full stop or enter key.
After the change is complete then always go to the end of text where you would want to be anyway! (Set cursor position to a any large number)
 
i did a quick test and it seems that this may work. i just put a timer in a quick project and called the procedure and noticed the scrolling issue... ill have to play with that...

i am still curios if this could be easily done with a datagrid? update on a Cell by Cell basis... any suggestions would be helpfull. im going to see if i can adapt this code to a datagrid...

thanks a bunch guys!
 
Yes,
Is your data in a database or kept in a series of text files?
If in a database table, modifications are easy by changing the table directly with a recordset rather than altering the grid directly, then you have to refresh the datagrid to see the changes (and set the focus back to the last used cell).
If you use text only then you can edit the text then feed it back to the datagrid cell.
Otherwise if you manipulate the table cells directly, the focus changes to that cell giving you the same problem you have with using the same richtext control to show everything.

Whatever way you still have to return the focus to where you were before the edit was shown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top