473,790 Members | 2,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Richtext SelectedText Selectionstart

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 "aaaabbbbcc cc" in the richtext box and I place the
caret between the a and b and hit the button. I get "aaaatestbbbbcc cc" as
expected. Then I click between the b and the c and hit the button I get:
"testaaaatestbb bbcccc".

I added some watches on richtextbox1.Se lectionStart 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
Nov 21 '05 #1
2 2373
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/cvsotAhsfbuTpmv ujpotXjui/OFU".ToCharArra y()
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))
--
"Gary Shell" <gs****@fuse.ne t> wrote in message
news:ei******** ******@TK2MSFTN GP12.phx.gbl...
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 "aaaabbbbcc cc" in the richtext box and I place the
caret between the a and b and hit the button. I get "aaaatestbbbbcc cc" as
expected. Then I click between the b and the c and hit the button I get:
"testaaaatestbb bbcccc".

I added some watches on richtextbox1.Se lectionStart 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

Nov 21 '05 #2
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.Tabl es("Stuff").Row s(0).Item("Rich Text")

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:
"boolRichTextDi rty = 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

"Gary Shell" <gs****@fuse.ne t> wrote in message
news:ei******** ******@TK2MSFTN GP12.phx.gbl...
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 "aaaabbbbcc cc" in the richtext box and I place the
caret between the a and b and hit the button. I get "aaaatestbbbbcc cc" as
expected. Then I click between the b and the c and hit the button I get:
"testaaaatestbb bbcccc".

I added some watches on richtextbox1.Se lectionStart 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

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1600
by: Pete Griffint | last post by:
Howdy! I want to get the following in a Richtext control: Paragraph 1.... Paragraph 2... Obviously I want to do something like rtb.SelectedText="Paragraph 1..."; rtb.SelectedText=<paragraph break>; (*)
0
1193
by: Lukas Thanei | last post by:
Hi, Im trying for 2 days to solve a problem but now Im on a point where all ideas exhausted. Im writing a software to print out some cards containing richtext and images. To print the richtext im using an windows api call (Sendmessage EM_FORMATRANGE - http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/wnf_RichTextBox.asp). Because I have to print background images, the richtext background should be
2
4615
by: Du | last post by:
I like richtext textbox and its shortcut, but I don't need the richtext format. How do I tell the richtext text box to strips all the format and convert everything to plain text? Thanks
3
2042
by: rigamonk | last post by:
Is there a way to save richtext from a richtextbox into an access DB using vb.net? thanks patrick
2
2452
by: andreas | last post by:
When I select a text in a richtextbox to set de selected text in bold, regular, italic .. or non I want to do that with one click Thus I must find out if the selected text is already in bold, regular, italic...... Is there any code for this Thanks for any response
0
1166
by: giddy | last post by:
hi , i made a little RtfEncapsulator. Its a class thats called RichText , it has an RtfElementCollection which takes any class that exposes the IRtfElement interface. I managed to make an RtfTextElement which works really well. So to add text this has to be done: Code:
5
3504
by: Jon | last post by:
If I add text to a rich text box, when there's too much to display, the vertical scroll bar stays at the top. I'd like it to be at the bottom, so I can see the last item of text added. How do I do this?
3
1353
by: Soulless | last post by:
Hi, I am going crazy trying to resolve a stupid issue. I have a RTB control and created a method to accept a string and (hopefully) set the font and size and alignment... Here is the method: private void AddToRTFReport(string asText, object aoColor, string asFont,object aoFontStyle, int aiSize, object aoAlignment) { int liLen = rtb1.Text.Length; if (liLen <= 0) {
1
1380
by: IMAFriend | last post by:
So, if I have 2 richtextboxes, call them rtbSource and rtbTarget. I can say rtbTarget.rtf = rtbSource.rtf and it works. But if rtbTarget already has something in it, how can I 'insert' the text from source into target? If I use rtbTarget.selectedtext = rtbSource.text, it inserts plaintext.
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10413
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10145
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9986
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7530
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5422
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4094
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3707
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.