Setting Insertion Point in an Edit Control

Applies to TestComplete 14.20, last modified on September 11, 2019

When you perform any operations over the text within the edit control, you may need to set the insertion point, which determines the start position of the text modification. For example, you can select the text or add new text starting from this position.

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 move the cursor to the desired position within the edit control by simulating keystrokes, which is performed via the Keys action. The following example demonstrates how to set the insertion point within the edit control:

JavaScript, JScript

function KeysActionExample()
{
  var p, Edit, Find;
  // 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("That's very important");
  Edit.Keys("^f");
  Find = p.Window("#32770").Window("Edit");

  // Place the cursor to the specified position
  Find.Keys("vry");
  Find.Keys("[Left][Left]");
  Find.Keys("e");
}

Python

def KeysActionExample():
  # 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("That's very important")
  Edit.Keys("^f")
  Find = p.Window("#32770").Window("Edit")

  # Place the cursor to the specified position
  Find.Keys("vry")
  Find.Keys("[Left][Left]")
  Find.Keys("e")

VBScript

Sub KeysActionExample
  Dim p, Edit, Find
  ' 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 "That's very important"
  Edit.Keys "^f"
  Set Find = p.Window("#32770").Window("Edit")

  ' Place the cursor to the specified position
  Find.Keys "vry"
  Find.Keys "[Left][Left]"
  Find.Keys "e"
End Sub

DelphiScript

procedure KeysActionExample;
  var p, Edit, Find: OleVariant;
begin
  // 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('That''s very important');
  Edit.Keys('^f');
  Find := p.Window('#32770').Window('Edit');

  // Place the cursor to the specified position
  Find.Keys('vry');
  Find.Keys('[Left][Left]');
  Find.Keys('e');
end;

C++Script, C#Script

function KeysActionExample()
{
  var p, Edit, Find;
  // 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"]("That's very important");
  Edit["Keys"]("^f");
  Find = p["Window"]("#32770").Window("Edit");

  // Place the cursor to the specified position
  Find["Keys"]("vry");
  Find["Keys"]("[Left][Left]");
  Find["Keys"]("e");
}

Note: Setting the insertion point within a multiline edit control has some specifics, which are described in the Placing Cursor Within a Multiline Edit Control topic.

See Also

Working With Edit Controls
Simulating Keystrokes
Placing Cursor Within a Multiline Edit Control

Highlight search results