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);
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.
After setting the SelectionStart and SelectionLength, the Caret is at the end of the selection.
How can I move the Caret to the beginning of the selection?
I now tried this: The question is not answered by the other threads
SelectionStart = selStart; // set the desired position
SelectionLength = 0;
MyLibs.WindowsDll.POINT point;
MyLibs.WindowsDll.GetCaretPos(out point); // save the position
SelectionLength = restOfWord.Length; // extend the selection
MyLibs.WindowsDll.SetCaretPos(point.X, point.Y); // restore caret position
GetCaretPos and SetCaretPos are working somehow, but not as it should.
Using the above code snippet, the caret is indeed blinking at the beginning of the selection.
But ... then I hit backspace or cursor left/right, the caret still behaves like it is still at the end of the selection.
Unfortunately, you can not do this using managed code like C#,
but to achieve this goal you may use C++, SetFocus() function,
for example:
//use SetFocus()
pEdit->SetFocus()
//place caret at the end:
pEdit->SendMessage (WM_KEYDOWN,VK_END,0);
//simulate keyup:
m_Edit->SendMessage (WM_KEYUP,VK_END,0);
//send the key code:
m_Edit->SendMessage(WM_CHAR,'A',0);
Have a look to MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/ms646312(v=vs.85).aspx
You may use also CEdit::SetSel to accomplish that:
CEdit* e = (CEdit*)GetDlgItem(IDC_EDIT1);
e->SetFocus();
e->SetSel(0,-1); // move cursor at the end
e->SetSel(-1); // remove selection
See details in Stackoverflow question: CEdit control MFC, placing cursor to end of string after SetWindowText
UPDATED:
If you need to set position to the beginning you can reset the Caret position to start before displaying the text using:
SetSel (0,0);
You can not move the caret to the beginning while keeping the text selected using basic C#/C++ functions, however you can override basic "Caret" logic using C++ or/and Assembler.
I'm attempting to automate the basic process of editing text in a TextFrame in Powerpoint via Interop, and I've hit a snag. I need to be able to begin the text editing process at a particular location on screen, and after crawling MSDN I still don't know of a way to do it. The use case boils down to this:
My service receives an X and Y coordinate
I tell Powerpoint to select the shape at that location
I tell Powerpoint to place the blinking cursor at the location it would be if the user had clicked there himself to start editing text.
It's the third step that's tripping me up. Word has RangeFromPoint, which returns a text range. In Powerpoint, though, that method returns a shape. I can use TextRange.Characters() to place the cursor manually within the shape's text range, but that accepts character index values rather than screen coordinates.
Anyone know how to go about this (other than brute forcing in mouse messages via Win32 calls)?
Every bit of text, down to the character level, can be treated as a Range; every text range has .BoundLeft, .BoundTop, .BoundHeight and .BoundWidth properties that return the coordinates of a rectangle that bounds the text range.
For example, this snippet will give you the left coordinate of the third text character in the currently selected shape:
With ActiveWindow.Selection.ShapeRange(1)
Debug.Print .TextFrame.TextRange.Characters(3, 1).BoundLeft
End With
Coordinates are returned in Points. It sounds like you already have a handle on converting screen coordinates to PPT coordinates.
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;
I'm currently developing a C# desktop application which is a simple virtual keyboard with word prediction facility.
The prediction process will start after typing the first three letters of the word, then provide the suggestions. I need to track the caret while typing, and I tried to use richTextBox events such as SelectionChanged but it requires regular expression check and position tracking manually ( declaring variables ... ).
My questions: is there any suggestions that can help me in doing this task ? What about Listeners? are they helpful?
Note: I have no long experience with .NET framework and I didn't use Listeners before.
Also note that the input method is eye gaze ! which means non of key- events will work !
Thank you.
Will this be in WPF or WinForms? I would tackle this problem as follows. Maybe not the fastest way but worth to try until you have something else.
OnKeyDown event of your RTB check if the last char was a space. If not get whole word from last space and check against list of words and update the list on screen.
to check where the cursor is at the time in your word just do the same as above and try to get the current word and than the indexof specific key.