Setting Insertion Point in an Edit Control in Desktop Windows Applications

Applies to TestComplete 15.45, last modified on December 01, 2022

Note: To learn how to simulate user actions over text edit box controls in web applications, see Working With Text Edit Controls in Web Applications.

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 in Desktop Windows Applications topic.

See Also

Working With Edit Controls in Desktop Windows Applications
Simulating Keystrokes
Placing Cursor Within a Multiline Edit Control in Desktop Windows Applications
Working With Text Edit Controls in Web Applications

Highlight search results