RichTextBox invisible selectable character at end of string - c#

When selecting text in a RichTextBox using the mouse, or arrow keys + shift, I can select an extra blank character at the end of the string. To reproduce:
type a few characters (or nothing at all) in a RichTextBox
set the cursor to the end of the string
hold shift and press the right arrow key
You'll see a narrow highlighted selection appear, which cannot be deleted.
This causes a problem in my application because the SelectionFont property returns null when the extra character is selected along with some valid text. Any ideas on how to disable this extra character, or work around it otherwise?
c# winforms, visual studio 2010

Interesting. (This is not Dr House, MD, speaking.)
When nothing is selected in an empty RTF edit control, SelectedRtf returns:
"{\rtf1\ansi\ansicpg1252\deff0\deflang2055\uc1 }"
When the "phantom" stuff is selected in the empty RTF edit control, SelectedRtf returns:
"{\rtf1\ansi\ansicpg1252\deff0\deflang2055{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
\viewkind4\uc1\pard\f0\fs17\par
}"
Of course, SelectedText returns the empty string in both cases.
So I suspect what is selected is the metainfo that makes sure that newly entered text that would replace the empty selection will be inserted with the correct font.
However, that seems to be nonsense since newly entered text will be inserted with the correct font even if nothing is selected.
So all that does not make sense. (Well, it does, kind of -- see last paragraph)
Which makes me believe this is a bug, or at least a glitch, in RichTextBox.
The formatting info is probably created by the selection routine which makes sure that selected non-empty text will be replaced by the newly typed text formatted in the same format as the text it replaces. For this to work, a selection must always contain the formatting info, even if no text is selected. I think. Maybe one can pre-select a char and paragraph formatting different from the default one, somehow, and then typing text uses that format.

FIred up a new winforms app in studio 2010 and wasn't really able to reproduce this. I can select the invisible 'character' but it doesn't cause me any problems. SelectedFont still returns a valid object for me. I can get the text without issue.

The SelectionFont property for the RichTextBox control can ONLY return a single font. If the selected range contains more than one font, reading the SelectionFont property will throw a NullReferenceException.
Most likely what is happening is that the RichTextBox Font property is different than the current font you are using on the selected range. That "extra" character selected at the end is your Font property, not your SelectionFont property.
If your RichTextBox is just using a single font, just make sure the Font and SelectionFont property are the same.
Otherwise, just check if it's null:
if (richTextBox1.SelectionFont != null) {
//do something
}

Related

Is it possible to define "Word boundary" characters for a text box?

(I cannot believe this has never been asked; most likely I failed at searching.)
Having a single line WinForms TextBox control with content in it:
When the user uses Ctrl+→ or Ctrl+←, the caret moves to the very end respectively very beginning of the content of the text box.
My goal
I want to configure the text box so that navigating with the above keys (or in addition with Shift), the caret stops at word boundaries.
E.g. when the caret is at position 0 and the user presses Ctrl+Shift+→ it would look like this:
I.e. it selects the first "word" only.
The standard behaviour is:
I.e. it selects the whole text.
Interestingly enough, the native Windows controls like e.g. in the "Run" dialog behave as described:
The address bar of Edge or Internet Explorer or even Windows Explorer also stops at word boundaries.
My question
Is it possible (maybe through P/Invoke) to configure certain stop characters for the TextBox control?
Update 1
I could think of handling keyboard events and doing this manually.
To answer your question.. definitely, but I wouldn't know how.
But
There is a control that has the behavior you're looking for, namely the RichTextBox.
It has the behavior you are asking for and has a property for disabling the default multiline behavior.
Properties to make the RichTextbox look like a regular textbox
Multiline - false
DetectUrls - false
height - 20
Note:
The richTextBox does NOT have autocomplete out of the box, like the regular textbox does.

Empty RichTextBox start with Bold

I have a problem. When i start working with RichTextBox (it is empty) I cannot change format to Bold.
I try to type (CTRL+B) but after I typed something the text is normal (not bold).
The text only changes format when I type something before. This happens again when I SelectAll and Clear all Content.
Is it bug in RichTextBox, or I can do something with it.
Best Regards,
_alexiej
Well, it's normal, usually you decide what is bold or not after you've written the text. If you want this button to act like ( on off ) you have to change it yourself.

SpellCheck Functionality in Textbox

I have an issue in Spellchecking the text that is entered in a textbox in wpf 3.5
<TextBox Name="tbxBack" SpellCheck.IsEnabled="True"/>
It will Check fine for the wrongly spell words in the sentence that I have entered in the text box, but when I right click on the wrongly spell word and try to make correct, it is only making the first word correct in the sentence.
for example:
the sentence is:
tey are plaing in playgroun (They are playing in playground)
when I right click on "tey", system is giving the appropriate results and setting the correct value ("They" for "tey"),
but when I right click on "plaing", system gives the correct values, but does not set it to correctvalue ("playing").
same for "playgroun" (doesn't set to playground), System is checking only the first word.
How to make it work for the whole sentence?

richtextbox text range attributes -- protected, hidden, backcolor, etc -- can i set them without selecting a range?

i want to be able to protect, hide, and highlight different ranges of text within a richtextbox without actually selecting the text -- which is what you seem to have to do for the SelectionProtected and SelectionBackColor or w/e properties.
how can i do this programatically? I looked around in reflection and found the SetCharFormat method, but I can't really tell how I'd use it to change the aforementioned properties on a specific range of text instead of the selection.

C# Correct way to design/implement this UI?

This is my first tryst with C#. The form that i have in mind consists
A textfield which will be supplied with the path of an executable.
A "Run" button which will call the executable(cosole app)
The executable console output should be displayed in the rich textbox.
Now when i click on a line in richtext box, i select and get the text in the line. This text maps to some other text info. I need to display this text info as a tooltip over the line.
More explanation:
The output of the exe is displayed in the text box as
Address1=Value
Address2=Value
Now when i click the line "Address1=Value", i map this text to find some info regarding what bits are set like
enable : 1
select : 0 ..etc
this info i need to display as tooltip over the line. Is such a thing possible? Is there better alternative to RTB/tooltip for this problem?
Thanks
Vivek
I would recommend using a ListBox for each string of data returned and then if you use a tooltip it makes alot more sense because you are hovering over a list item specifically not the whole text field.
Using the ListBox and items should make it alot easier to work with overall since it will be separating them into defined items instead of just appending lines to a text box.
Also I think you might have alot of work in store for you for trying to make the text box behave the way you want it to for it to treat each line differently dependent on the text of the line.
If you're using the textbox because later you want to be able to select all the output to copy and paste it I would have the textbox hidden by default and have a button that says like "Toggle Raw Output" that will show/hide the text field so users can get the text easily. While using the ListBox as the primary display for information.
What I understand from your question is that when you click on the line in the RTB, your code scans the text on that line, identifies the extra data associated with that line and then inserts it into the tooltip for the RTB.
Technically I believe that this is possible to do - although I am not 100% sure of the mechanics of inserting tooltip text. However as a user interface feature I would personally not do that as the tooltip text is displayed whenever the mouse pointer is anywhere over the RTB. Thus if a user clicks on line #1, (and sees the data associated from line #1) but hovers the mouse of line #3, they might think that the tooltip is associated with line #3.
You could alleviate my concerns with a strongly worded tooltip, but I feel that what you are doing is misusing the tooltip for something other than what it was intended to be used for. IMHO it may be that you are better off displaying your data with a tree control rather than with a RTB, as the tree control more naturally expresses the functionality that you desire (click on a node, expand it to see details etc).

Categories

Resources