Placing Cursor Within a Multiline Edit Control in Desktop Windows Applications

Applies to TestComplete 15.65, last modified on June 25, 2024

When working with multiline controls, you may need to place the cursor to a specified position, for example, if you want to select any word within a phrase, or modify text, or insert new text. Generally, performing this operation is similar to setting insertion points within a single-line edit control. This approach is described in the Setting Insertion Point in an Edit Control in Desktop Windows Applications topic.

While testing edit controls, you can use specific properties and methods of the corresponding program object to perform certain actions and obtain data stored in controls. You can call these methods and properties from your keyword tests, as well as from scripts. This topic describes how to work with the needed properties and methods from your scripts. However, when testing a control from your keyword test, you can use the same methods and properties calling them from keyword test operations. For more information, see Keyword Tests Basic Operations.

You can place the cursor to the desired position by clicking on it. You can perform this operation by using the Click action. The coordinates of the insertion point are specified in pixels.

Note: The coordinates you specify are relative to object and are not screen coordinates.

An example below demonstrates how to set the insertion point by simulating a click:

JavaScript, JScript

function Main()
{
  var p, Edit;
  // Run Notepad
  WshShell.Run("notepad.exe", SW_SHOWNORMAL);
 
  // Obtain the edit object and fill it with text
  p = Sys.Process("NOTEPAD");
  Edit = p.Window("Notepad").Window("Edit");
  Edit.Keys("What else have you got in your pocket?[Enter]");
  Edit.Keys("Only a thimble...[Enter]");
  Edit.Keys("Hand it over here!!");

  // Move the cursor to the desired position
  Edit.Click(55,20);
}

Python

def Main():
  # Run Notepad 
  WshShell.Run("notepad.exe", SW_SHOWNORMAL)
 
  # Obtain the edit object and fill it with text 
  p = Sys.Process("NOTEPAD")
  Edit = p.Window("Notepad").Window("Edit")
  Edit.Keys("What else have you got in your pocket?[Enter]")
  Edit.Keys("Only a thimble...[Enter]")
  Edit.Keys("Hand it over here!!")

  # Move the cursor to the desired position
  Edit.Click(55,20)

VBScript

Sub Main
  Dim p, Edit
  ' Run Notepad
  Call WshShell.Run("notepad.exe", SW_SHOWNORMAL)

  ' Obtain the edit object and fill it with text
  Set p = Sys.Process("NOTEPAD")
  Set Edit = p.Window("Notepad").Window("Edit")
  Edit.Keys "What else have you got in your pocket?[Enter]"
  Edit.Keys "Only a thimble...[Enter]"
  Edit.Keys "Hand it over here!!"

  ' Move the cursor to the desired position
  Edit.Click 55,20
End Sub

DelphiScript

procedure Main();
  var p, Edit : OleVariant;
begin
  // Run Notepad
  WshShell.Run('notepad.exe', SW_SHOWNORMAL);

  // Obtain the edit object
  p := Sys.Process('NOTEPAD');
  Edit := p.Window('Notepad').Window('Edit');
  Edit.Keys('What else have you got in your pocket?[Enter]');
  Edit.Keys('Only a thimble...[Enter]');
  Edit.Keys('Hand it over here!!');

  // Move the cursor to the desired position
  Edit.Click(55,20);
end;

C++Script, C#Script

function Main()
{
  var p, Edit;
  // Run Notepad
  WshShell["Run"]("notepad.exe", SW_SHOWNORMAL);

  //Obtain the edit object and fill it with text
  p = Sys.Process("NOTEPAD");
  Edit = p.Window("Notepad").Window("Edit");
  Edit["Keys"]("What else have you got in your pocket?[Enter]");
  Edit["Keys"]("Only a thimble...[Enter]");
  Edit["Keys"]("Hand it over here!!");

  // Move the cursor to the desired position
  Edit["Click"](55,20);
 }

Using the Click action implies that you must find the starting position's coordinates for the selection. You can detect the mouse position, for example, by recording a script (see Recording Automated Tests). This approach is rather difficult, so it is easier to place the cursor in the following way:

  • Simulate Click without any parameters. In this case the cursor will be placed at the end of the text. If the edit control you are working with is already active, you can omit this operation.
  • Use keystrokes to move the cursor to the desired position. Placing the cursor via simulating keystrokes is described in the Setting Insertion Point in an Edit Control in Desktop Windows Applications topic. The only difference between single-line and multiline control is that you can only select horizontal position within single-line edit controls, and both vertical and horizontal within multiline edit controls. Use the Up and Down keystrokes to move the cursor from one string to another. Moving the cursor within the string is similar to working with the single-line edit control. You can use the Home an End keystrokes, which allow you to position the cursor at the beginning or at the end of the string.

    The following code example simulates the typing of text and places the cursor before 'else' within the text.

    JavaScript, JScript

    function Main()
    {
      var p, Edit;
      // Run Notepad
      WshShell.Run("notepad.exe", SW_SHOWNORMAL);
     
      // Obtain the edit object and fill it with text
      p = Sys.Process("NOTEPAD");
      Edit = p.Window("Notepad").Window("Edit");
      Edit.Keys("What else have you got in your pocket?[Enter]");
      Edit.Keys("Only a thimble...[Enter]");
      Edit.Keys("Hand it over here!!");

      // Move the cursor to the desired position
      Edit.Keys("[Up][Up][Home]");
      Edit.Keys("[Right][Right][Right][Right][Right]");
    }

    Python

    def Main():
      # Run Notepad 
      WshShell.Run("notepad.exe", SW_SHOWNORMAL)
     
      # Obtain the edit object and fill it with text 
      p = Sys.Process("NOTEPAD")
      Edit = p.Window("Notepad").Window("Edit")
      Edit.Keys("What else have you got in your pocket?[Enter]")
      Edit.Keys("Only a thimble...[Enter]")
      Edit.Keys("Hand it over here!!")
    
      # Move the cursor to the desired position
      Edit.Keys("[Up][Up][Home]")
      Edit.Keys("[Right][Right][Right][Right][Right]")

    VBScript

    Sub Main
      Dim p, Edit
      ' Run Notepad
      Call WshShell.Run("notepad.exe", SW_SHOWNORMAL)

      ' Obtain the edit object and fill it with text
      Set p = Sys.Process("NOTEPAD")
      Set Edit = p.Window("Notepad").Window("Edit")
      Edit.Keys "What else have you got in your pocket?[Enter]"
      Edit.Keys "Only a thimble...[Enter]"
      Edit.Keys "Hand it over here!!"

      ' Move the cursor to the desired position
      Edit.Keys "[Up][Up][Home]"
      Edit.Keys "[Right][Right][Right][Right][Right]"
    End Sub

    DelphiScript

    procedure Main;
      var p, Edit : OleVariant;
    begin
      // Run Notepad
      WshShell.Run('notepad.exe', SW_SHOWNORMAL);

      // Obtain the edit object
      p := Sys.Process('NOTEPAD');
      Edit := p.Window('Notepad').Window('Edit');
      Edit.Keys('What else have you got in your pocket?[Enter]');
      Edit.Keys('Only a thimble...[Enter]');
      Edit.Keys('Hand it over here!!');

      // Move the cursor to the desired position
      Edit.Keys('[Up][Up][Home]');
      Edit.Keys('[Right][Right][Right][Right][Right]');
    end;

    C++Script, C#Script

    function Main()
    {
      var p, Edit;
      // Run Notepad
      WshShell["Run"]("notepad.exe", SW_SHOWNORMAL);

      //Obtain the edit object and fill it with text
      p = Sys.Process("NOTEPAD");
      Edit = p.Window("Notepad").Window("Edit");
      Edit["Keys"]("What else have you got in your pocket?[Enter]");
      Edit["Keys"]("Only a thimble...[Enter]");
      Edit["Keys"]("Hand it over here!!");

      // Move the cursor to the desired position
      Edit["Keys"]("[Up][Up][Home]");
      Edit["Keys"]("[Right][Right][Right][Right][Right]");
     }

See Also

Working With Multiline Edit Controls - Specifics in Desktop Windows Applications
Setting Insertion Point in an Edit Control in Desktop Windows Applications
Recording Automated Tests

Highlight search results