Getting the Selected Text in an Edit Control in Desktop Windows Applications

Applies to TestComplete 15.47, last modified on January 20, 2023

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.

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.

When you select text within an edit control, it often implies that you intend to obtain the selected text. You can select text in different ways:

You must select a portion of the text before using wSelection (see Selecting Text Within an Edit Control in Desktop Windows Applications), otherwise you will get an empty string. An example below demonstrates how you can obtain the selected text by using the wSelection property:

JavaScript, JScript

function Main()
{
  var p, Edit, SText;
  // 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("Do cats eat bats?");

  // Select a portion of the text and place it in a variable
  Edit.Keys("[Left]![Left]![Left]![Left]![Left]");
  SText = Edit.wSelection;

  // Posts the received value to the log
  Log.Message(SText);
}

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("Do cats eat bats?")

  # Select a portion of the text and place it in a variable
  Edit.Keys("[Left]![Left]![Left]![Left]![Left]")
  SText = Edit.wSelection

  # Posts the received value to the log
  Log.Message(SText)

VBScript

Sub Main
  Dim p, Edit, SText
  ' 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 "Do cats eat bats?"

  ' Select a portion of the text and place it in a variable
  Edit.Keys "[Left]![Left]![Left]![Left]![Left]"
  SText = Edit.wSelection

  ' Posts the received value to the log
  Log.Message(SText)
End Sub

DelphiScript

procedure Main;
  var p, Edit, SText: 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('Do cats eat bats?');

  // Select a portion of the text and place it in a variable
  Edit.Keys('[Left]![Left]![Left]![Left]![Left]');
  SText := Edit.wSelection;

  // Posts the received value to the log
  Log.Message(SText);
end;

C++Script, C#Script

function Main()
{
  var p, Edit, SText;
  // 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"]("Do cats eat bats?");

  // Select a portion of the text and place it in a variable
  Edit["Keys"]("[Left]![Left]![Left]![Left]![Left]");
  SText = Edit["wSelection"];

  // Posts the received value to the log
  Log["Message"](SText);
}

See Also

Working With Edit Controls in Desktop Windows Applications
Copying and Pasting Text in an Edit Control in Desktop Windows Applications
Selecting Text Within an Edit Control in Desktop Windows Applications
wSelection Property (Edit Controls)
Working With Text Edit Controls in Web Applications

Highlight search results