Caret position retention on text insertion

SyntaxEditor for Windows Forms Forum

Posted 18 years ago by Paul Hasselfeldt
Avatar
SyntaxEditor v3.1.0203, .NET 2.0, Windows XP

In one of my projects I have a need to insert text optionally before or after the caret keeping the current position of the caret intact. I am currently able to occomplish this with the Document.UndoableInsert function using the retainSelection for text inserted prior to the caret. When inserting values directly after the caret it moves the carets position to the end of the inserted text rather than appending the text to the end of the caret current position which is what I need. I have tried repositioning the selection after insertion but if the text being inserted is long then maintaining the previous relative screen position becomes difficult as scrolling begins to take over. There is no need to have the inserted text as an undoable action however I need to retain the undo history. Is there another means to accomplish this that I am not seeing? Could style of retainSelection be augmented or an new flag added to support this need?

Thanks,
Paul

Comments (5)

Posted 18 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Paul,

Just to make sure I understand correctly, retain selection is working fine EXCEPT in the single case where the text is inserted exactly at the caret's offset?


Actipro Software Support

Posted 18 years ago by Paul Hasselfeldt
Avatar
If you run the following example, my goal would be for the caret to stay at its original position and have the 5 characters to the right of the caret replaced with the text "Testing" when a <CTRL><ALT>F is typed by the user. When you run this example however the caret position is move to the end of the string "Testing".
        
private void editor_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.F && e.Modifiers == (Keys.Control | Keys.Alt))
    {
        e.SuppressKeyPress = true;
        editor.Document.UndoableInsert(DocumentModificationType.Custom, editor.SelectedView.Selection.StartOffset, 5, "Testing", true);
    }
}
Thanks,
Paul
Posted 18 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Ok this case is now fixed for the next maintenance release.


Actipro Software Support

Posted 18 years ago by Paul Hasselfeldt
Avatar
I just loaded the 3.1.0210 build and discovered that the issue above was fixed but it introduced two new issues.

First, The code below kept the caret position to the right of the inserted text "help" (as intended) but with the fix in the new build now moves the caret to the left of the word.

private void editor_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.F && e.Modifiers == (Keys.Control | Keys.Alt))
    {
        e.SuppressKeyPress = true;
        editor.Document.UndoableInsert(DocumentModificationType.Custom, 1, editor.SelectedView.Selection.StartOffset, "Help", true);
    }
}
Second, I am now receiving the following exception during successive updates to the document which did not occur in the previous build.

  System.ArgumentException was unhandled
  Message="The offset '75' added to the length '1' must be less than or equal to the count of characters in the document."
  Source="ActiproSoftware.SyntaxEditor.Net20"
  StackTrace:
       at ActiproSoftware.SyntaxEditor.Document.a(Int32 A_0, Int32 A_1)
       at ActiproSoftware.SyntaxEditor.Document.GetSubstring(Int32 offset, Int32 length)
       at ActiproSoftware.SyntaxEditor.Document.a(DocumentModificationType A_0, Int32 A_1, Int32 A_2, String A_3, Boolean A_4)
       at ActiproSoftware.SyntaxEditor.Document.UndoableDelete(DocumentModificationType type, Int32 offset, Int32 length, Boolean retainSelection)
       at ActiproSoftware.SyntaxEditor.Document.UndoableDelete(DocumentModificationType type, Int32 offset, Int32 length)
       at ActiproSoftware.SyntaxEditor.Commands.BackspaceCommand.Execute(EditCommandContext context)
       at ActiproSoftware.SyntaxEditor.MacroRecording.a(EditCommand A_0, EditCommandContext A_1)
       at ActiproSoftware.SyntaxEditor.EditorView.a(EditCommand A_0, Boolean A_1)
       at ActiproSoftware.SyntaxEditor.SyntaxEditor.a(EditorView A_0, Keys A_1, Char A_2, Boolean A_3)
       at ActiproSoftware.SyntaxEditor.SyntaxEditor.OnKeyDown(KeyEventArgs e)
       at System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)
       at System.Windows.Forms.Control.ProcessKeyMessage(Message& m)
       at System.Windows.Forms.Control.WmKeyChar(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at ActiproSoftware.SyntaxEditor.SyntaxEditor.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at LextEdit.Program.Main(String[] args)
       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
Thanks,
Paul
Posted 18 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Thanks, the first item is now tweaked to work for both cases correctly.

As for the second item, can you please either post or email us steps in our sample project to reproduce that?


Actipro Software Support

The latest build of this product (v24.1.0) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.