I am dynamically adding text in a RichTextBox.
How can I set Focus on the last line so the user can see it?
This will move the caret to the last line:
richTextBox.CaretPosition = richTextBox.Document.ContentEnd;
But it will not scroll the richTextBox to make the caret visible. To accomplish that you also have to call ScrollToEnd():
richTextBox.CaretPosition = richTextBox.Document.ContentEnd;
richTextBox.ScrollToEnd();
Related
As the title suggests, how do I find the caret position of a WPF textbox in screen or form-space coordinates? So far, I've only been able to find how to get the character position of the caret in the textbox.
Perhaps I'm getting ahead of myself though, because really, I'm trying to ensure that the current caret position of the selected TextBox (of dynamic height) contained within a DataGrid is visible to the user. So, if there's a way to do this without knowing the coordinates of the caret, then I'm all ears. So far, I've tried calling the DataGrid's ScrollIntoView method, but this can fail if the TextBox is taller than the available screen space.
I don't believe you can do it natively, but you call use interop to call the Win32 GetGUIThreadInfo(). See here for an example: How to call GetGUIThreadInfo in c#
I would suggest something but I don't know if it is applicable in your case. I needed to create a triangular caret to replace the vertical-line caret of a WPF textbox. I control its location with a translate transform and it is always following the default textbox caret. In this way you know where is your new caret is inside the text box (by knowing the X and Y of its translate transform) and then you can use PointToScreen to convert it to screen coordinates.
I have a Grid in silverlight having a lot of different controls.
In the last row of Grid, I have a RichTextBox.
To write something in RichTextBox, first we have to scroll down to that because controls are too much.
Each time we open that Grid, all the controls are initiated with some initial data.
Now there is a problem with this line
this.rtb.Selection.Text = "Initial Text";
What this line do is, it set text into RichTextBox control and also set focus on it, as a result my scroll bar move to the bottom, which is very annoying.
I want this text assigned to it but scroller should stay at top.
Try this:
// create a paragraph
Paragraph prgParagraph = new Paragraph();
// create some text, and add it to the paragraph
Run rnMyText = new Run();
rnMyText.Text = "This is some example text with a ";
prgParagraph.Inlines.Add(rnMyText);
// add the paragraph to the RTB
rbtMyRichTextBox.Blocks.Add(prgParagraph);
How to check whether the caretposition is inside the visible region of a window.
I am using Richtextbox inside a Scrollviewer.Whenever the cursor moves to the non-visible area of Richtextbox I need to call the scrollviewer's line down.
Not sure, but could you just set the RichTextBox.VerticalScrollBarVisibility, instead of putting your RichTextBox in a ScrollViewer?
I would like to trim the contents of a TextBlock at the bottom of the container when it runs out of space. The image below shows what I am trying to achieve.
The left cell contains text that is being wrapped at the end of the line, but is being cropped on the final line. The right cell (which I edited by hand) shows the effect I want to achieve by trimming the text on the last line.
Is there an (easy) way to achieve this in WPF?
Did you try the TextTrimming property?
Let's say that I have a RichTextBox and its contents take up about 3 times the height compared to the visible height. There's no color formatting, I want to highlight keywords. If I use SelectionStart, SelectionLength and SelectionColor, then I have to set SelectionStart back to the caret's original position.
If, for example, I'm looking at the first page and my caret is half-way down the page, but I want a keyword highlighted near the end, when the caret is returned, the RichTextBox will only scroll up far enough for the caret to be on the top visible line, so my visible position has moved.
Is there any way for me to format colors out of view without affecting the view position? Or, is there any way for me to get and set/reset the view position after formatting?
The solution is here:
"Get/Set the ScrollBars’ positions"
http://codebetter.com/blogs/patricksmacchia/archive/2008/07/07/some-richtextbox-tricks.aspx
After I return the caret to its original position with SelectionStart, I can use the code shown on the blog to return the view to its original position.