Getting Text of a Multiline Edit Control in Desktop Windows Applications

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

When working with a multiline edit control, you may need to obtain a portion of the text, in particular, you may need to get a whole string or a set of strings. You can perform this operation using the approach that is similar to working with a single-line edit control, for example you can select the desired string and copy it to the clipboard (for more information about selecting text see Selecting Text in a Multiline Edit Control in Desktop Windows 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.

For multiline edit controls there is another approach, which allows you to get the needed string without simulating keystrokes and using actions. You can perform the desired operation by using built-in routines, which are provided by TestComplete and are available to all scripting languages. The GetListItem Method routine allows you to get any string specified by a number. The example below demonstrates how to get a string from a multiline edit control.

JavaScript, JScript

function Main()
{
  var p, Edit, Text, Str;
  // Run Notepad
  WshShell.Run("notepad.exe", SW_SHOWNORMAL);
 
  // Obtain the edit object
  p = Sys.Process("NOTEPAD");
  Edit = p.Window("Notepad").Window("Edit");

  Edit.Keys("ALL PERSONS MORE THAN A MILE HIGH TO LEAVE THE COURT.[Enter]");
  Edit.Keys("Everybody looked at Alice.[Enter]");
  Edit.Keys("'I'M not a mile high,' said Alice.[Enter]");
  Edit.Keys("''You are,' said the King.");
  Text = Edit.wText;

  // Get the string and post it to the log
  aqString.ListSeparator = "\r\n";
  Str = aqString.GetListItem(Text,1);
  Log.Message(Str);
}

Python

def Main():
  # Run Notepad 
  WshShell.Run("notepad.exe", SW_SHOWNORMAL)
 
  # Obtain the edit object
  p = Sys.Process("NOTEPAD")
  Edit = p.Window("Notepad").Window("Edit")

  Edit.Keys("ALL PERSONS MORE THAN A MILE HIGH TO LEAVE THE COURT.[Enter]")
  Edit.Keys("Everybody looked at Alice.[Enter]")
  Edit.Keys("'I'M not a mile high,' said Alice.[Enter]")
  Edit.Keys("''You are,' said the King.")
  Text = Edit.wText

  # Get the string and post it to the log
  aqString.ListSeparator = "\r\n"
  Str = aqString.GetListItem(Text,1)
  Log.Message(Str)

VBScript

Sub Main
  Dim p, Edit, Text, Str
  ' 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 "ALL PERSONS MORE THAN A MILE HIGH TO LEAVE THE COURT.[Enter]"
  Edit.Keys "Everybody looked at Alice.[Enter]"
  Edit.Keys "'I`M not a mile high,' said Alice.[Enter]"
  Edit.Keys "''You are,' said the King."
  Text = Edit.wText

  ' Get the string and post it to the log
  aqString.ListSeparator = VbCrLf
  Str = aqString.GetListItem(Text,1)
  Log.Message(Str)
End Sub

DelphiScript

procedure Main;
  var p, Edit, Text, Str : 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('ALL PERSONS MORE THAN A MILE HIGH TO LEAVE THE COURT.[Enter]');
  Edit.Keys('Everybody looked at Alice.[Enter]');
  Edit.Keys('''I`M not a mile high,'' said Alice.[Enter]');
  Edit.Keys('''You are,'' said the King.');
  Text := Edit.wText;

  // Get the string and post it to the log
  Str := aqString.GetListItem(Text,1);
  Log.Message(Str);
end;

C++Script, C#Script

function Main()
{
  var p, Edit, Text, Str;
  // Run Notepad
  WshShell["Run"]("notepad.exe", SW_SHOWNORMAL);
  
  // Obtain the edit object
  p = Sys["Process"]("NOTEPAD");
  Edit = p["Window"]("Notepad")["Window"]("Edit");

  Edit["Keys"]("ALL PERSONS MORE THAN A MILE HIGH TO LEAVE THE COURT.[Enter]");
  Edit["Keys"]("Everybody looked at Alice.[Enter]");
  Edit["Keys"]("'I`M not a mile high,' said Alice.[Enter]");
  Edit["Keys"]("'You are,' said the King.");
  Text = Edit["wText"];

  // Get the string and post it to the log
  aqString["ListSeparator"] = "\r\n";
  Str = aqString["GetListItem"](Text,1);
  Log["Message"](Str);
}

Note: String numeration within multiline controls is zero-based, that is, the index of the first string is 0. If you specify a string index that is out of bounds, an error occurs.

See Also

Working With Multiline Edit Controls - Specifics in Desktop Windows Applications
GetListItem Method
Selecting Text Within an Edit Control in Desktop Windows Applications
Selecting Text in a Multiline Edit Control in Desktop Windows Applications
Copying and Pasting Text in an Edit Control in Desktop Windows Applications

Highlight search results