Richtext SelectedText Selectionstart

  • Thread starter Thread starter Gary Shell
  • Start date Start date
G

Gary Shell

I have a very ODD situation.

I have a RichTextBox and a button. In the button click event I have the
following:

me.richtextbox1.SelectedText="test"

Assuming I start with "aaaabbbbcccc" in the richtext box and I place the
caret between the a and b and hit the button. I get "aaaatestbbbbcccc" as
expected. Then I click between the b and the c and hit the button I get:
"testaaaatestbbbbcccc".

I added some watches on richtextbox1.SelectionStart and find that the first
time it is 5 as expected. But on the second button press it is zero! Also
the SelectionLength is zero on the second press even if I set the cursor
between the b and c and drag across the first three of the c's.

This all occurs in my actual application, so I created a simple form with
only these two elements and it works fine. So obviously something else is
interfering with this operation in my real application. I am at a loss as
to what!

In the real app the richtextbox is bound to a dataset, but I can't see why
that should cause this behavior. I don't see any other events firing that
might somehow screw up the SelectionStart like this. The Richtextbox is on
a tabpage (and so is the button), but again I see no reason why that should
have an effect.

Anybody else ever struggle with this?

Gary
 
I just tested this out as you say and it works OK for me.



--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
 
This turned out to be very strange indeed.

The field displayed in the RichTextbox was one that required some
translation of tokens to words before display and words back to tokens
before storage back in the database. I had this field bound to the
RichTextbox. Right after I populated the data set, I grabbed the
Me.dsStuff.Tables("Stuff").Rows(0).Item("RichText")

and did the conversion from tokens to words and stuffed it back into the
dataset. The binding then displayed it in the RichTextbox. When I inserted
text into the RichTextBox I was setting the selection color to red so I
could readily see the new text. I noticed that initially the new text was
red, but it reverted to black on the second click of the button! That I
knew was a clue, but what was it telling me? What it was telling me was
that something was replacing the text in the RichTextBox, something that was
stripping out the "rich" part of the text (in this case the color setting)
and resetting the SelectionStart. Suspecting it might be that the binding
that was updating the underlying dataset might also be reloading the
RichTextbox, I unbound the RichTextbox. Since I was always doing a
conversion on the text from tokens to words on load and words to tokens on
exit, I didn't need it bound anyway. And... Voila' Without any other code
changes, it worked just fine. I am pretty sure this had less to do with
the fact that it was a bound RichTextbox and more to do with the fact that I
was forcing a change in the dataset immediately after loading it. But
that's just a hunch based on the results Terry reported. I think I threw
the binding a curve ball by changing it's contents the way I did at form
load time.

I literally put a breakpoint on the entry point of EVERY single procedure in
the module just so I could be damn sure my code was not resetting the
SelectionStart like this. The only events fired were the button click event
and the TextChanged event, which had ONE like of code in it:
"boolRichTextDirty = true".

Now it works great and the new text stays red no matter how many times I use
the button to paste in new text. (Of course once the data makes a round
trip to the server where I store ONLY the text portion the color attributes
are stripped off, but that's EXACTLY what I want to happen. The point of
the red was to show uncommitted text.)

Thanks Terry and others who responded.

Gary
 
Back
Top