I've got a RichTextBox containing lines of text (each line a paragraph), and I have a line number that represents an error. I want to set the caret to be at the start of that line. How?
I haven't got a working installation of C# on my machine at the moment, but from looking at the docs it seems you should be able to first locate the paragraph you want (probably by enumerating through them, there does not seem to be an index function) in yourFlowDocument.Blocks and then:
TextPointer paragraphStart = paragraph.ElementStart;
richTextBox.CaretPosition = paragraphStart;
Related
Hi I use the Free Text Box in my .net application.
I was wondering if there was a way to prevent someone typing one single huge line of text.
Can you set a wrap boundary on it?
I had a short read of the documentation, and I don't think you'll find a native way to do so.
I guess you could use the FreeTextBox.TextChanged event, and check every line in the FreeTextBox.Text property. Mind the fact that it is HTML, and you will probably have to parse it with another third party tool (doing it manually will only give you headaches).
If any of these lines has more characters than an arbitrary length you'll set, create a new line of text with the surplus character, and move the caret to this line.
I've a RichEditBox that I'd like to add keyboard shortcut support to enable moving to the start or the end of the line that the caret is currently on. I've already figured out shortcuts supporting going to start or end of sentence as that involved simply reviewing the text value of the content and finding the nearest period. However, I believe what I'm looking for now is more involved as it requires working through the raw content of the file, which I'm not comfortable with nor have managed to find any resources with the guidance I'm after (I've mostly found answers on detecting the EOL character type rather than moving the caret to the EOL).
If you want to move caret, we can use the RichEditBox.Document.Selection.Move method to move the insertion point forward or backward by the specified number of units.
We can set TextRangeUnit.Line to the Move method, it will move caret to start or end of line in a RichEditBox.
For example:
myRichEdit.Document.Selection.Move(TextRangeUnit.Line, 1);
There was a point where I was using GetPositionFromCharIndex() to get the location of the start of a particular string in my RichTextBox. Due to other requirements, I made a change to increase the font size of some words. If a word happens to get enlarged (and the string whose position I want remains the same), the string I'm measuring gets pushed down but the Y position reads as being the same for some reason.
I wrote a quick test app to verify this and can replicate it. Below is all of the code I am using (apart from the designer of course).
public Form1()
{
InitializeComponent();
richTextBox1.Text = "Test statement";
//Select "Test" and increase the size
richTextBox1.SelectionStart = richTextBox1.Text.IndexOf("Test");
richTextBox1.SelectionLength = 4;
richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, 16);
Point test = richTextBox1.GetPositionFromCharIndex(richTextBox1.Text.IndexOf("Test"));
Point statement = richTextBox1.GetPositionFromCharIndex(richTextBox1.Text.IndexOf("statement"));
//"statement" is pushed down due to "Test" being enlarged,
//Yet the Y value is the same for both words
label1.Text = $"Test location: {test.X},{test.Y}";
label2.Text = $"statement location: {statement.X},{statement.Y}";
}
And a screenshot of the results:
Is this something I can get around? Clearly the word "statement" doesn't truly have the same Y value. Seems like as far as the RichTextBox is concerned, the word does start up that high so it just measures that, but if that is the case, there doesn't seem to be an obvious solution to me.
GetPositionFromCharIndex is working as designed.
The docs state:
This method enables you to determine where in the control a specific
character index is located. You can use this method for such tasks as
displaying shortcut menu items or help information for a word in the
control. For example, if you wanted to display a menu of options to
the user when the user right clicks on a word in the control, you can
use this method to determine the position of the word to properly
display a ContextMenu control.
Given that, it makes sense for both words to return the same Y value - since you would expect right-click to act consistently for both words (so even if you right click slightly above the statement word it could still work as expected).
I try to highlight a specifiable line of a RichEditControl of DevExpress.
Here is a sample that uses selection. I don't want to select the line to highlight and I don't have any idea how to calculate the range of a line. The only information I have is the line number to select. There is another sample that uses the CaretPosition to select the line.
Both samples are working perfect but I don't know how to set the selection or CaretPositon by line number.
How can I calculate the DocumentPosition or range of a single line to use the samples to highlight it?
Take a look at:
https://documentation.devexpress.com/#WindowsForms/CustomDocument5890
related with formatting of Text .
I'm writing an very simple text editor with highlight of digitis, in some keywords and special chars. Now I'm implementing the line numbers,for do it I'm using a ListView, that is updated to each new line added.
The problem that is when I down scroll using cursor,the number of lines is not updated,then if I jump to 30 line, the listView remains where it was, in line 10, for example for this reason I'm looking an way to get scroll the coordinates of RichTextBox to sincronize with scroll of listView.
Instead of developing your own text editor, you can try SharpDevelop's editor. You don't have to fully install it. Just download its source and compile only ICSharpCode.AvalonEdit
It has a property ShowLineNumbers :)
TextPointer textPointer = MyRichTextBox.Document.ContentStart.GetPositionAtOffset(8);
if (textPointer != null)
{
MyRichTextBox.CaretPosition = textPointer;
MyRichTextBox.Focus();
MyRichTextBox.Selection.Select(MyRichTextBox.CaretPosition, MyRichTextBox.CaretPosition);
}