Replacing Text in an Edit Control in Desktop Windows Applications

Applies to TestComplete 15.62, last modified on March 19, 2024

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 work with an edit control, you may need to replace part of the text with other text. To perform this operation you must select the part of the text to be replaced (see Selecting Text Within an Edit Control in Desktop Windows Applications) and then replace it by any of the following methods:

  • Typing new text. For more information about entering text, see the Entering Text into an Edit Control in Desktop Windows Applications topic. The sample code example below illustrates how to replace text by entering new text:

    JavaScript, JScript

    function Main()
    {
      // Run Notepad
      WshShell.Run("notepad.exe", SW_SHOWNORMAL);

      // Obtain the edit object and fill it with text
      var p = Sys.Process("NOTEPAD");
      var Edit = p.Window("Notepad").Window("Edit");
      Edit.Keys("A smile without a cat");

      // Select a portion of text and replace it
      Edit.Keys("[Home][Right][Right]![Right]![Right]![Right]![Right]![Right]");
      Edit.Keys("grin");
    }

    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("A smile without a cat")
    
      # Select a portion of text and replace it
      Edit.Keys("[Home][Right][Right]![Right]![Right]![Right]![Right]![Right]")
      Edit.Keys("grin")

    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 "A smile without a cat"

      ' Select a portion of text and replace it
      Edit.Keys "[Home][Right][Right]![Right]![Right]![Right]![Right]![Right]"
      Edit.Keys "grin"
    End Sub

    DelphiScript

    procedure Main;
    var p, Edit;
    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('A smile without a cat');

      // Select a portion of text and replace it
      Edit.Keys('[Home][Right][Right]![Right]![Right]![Right]![Right]![Right]');
      Edit.Keys('grin');
    end;

    C++Script, C#Script

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

      // Obtain the edit object and fill it with text
      var p = Sys["Process"]("NOTEPAD");
      var Edit = p["Window"]("Notepad").Window("Edit");
      Edit["Keys"]("A smile without a cat");

      // Select a portion of text and replace it
      Edit["Keys"]("[Home][Right][Right]![Right]![Right]![Right]![Right]![Right]");
      Edit["Keys"]("grin");
    }

  • Pasting new text from the clipboard. The approaches used to paste text are described in the Copying and Pasting Text in an Edit Control in Desktop Windows Applications topic. The sample code example below illustrates how to replace text via pasting:

    JavaScript, JScript

    function Main()
    {
      // Run Notepad
      WshShell.Run("notepad.exe", SW_SHOWNORMAL);

      // Obtain the edit object and fill it with text
      var p = Sys.Process("NOTEPAD");
      var Edit = p.Window("Notepad").Window("Edit");
      Edit.Keys("A smile without a cat");
      Sys.Clipboard = "grin";

      // Select a portion of the text and replace it
      Edit.Keys("[Home][Right][Right]![Right]![Right]![Right]![Right]![Right]");
      Edit.Keys("^v");
    }

    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("A smile without a cat")
      Sys.Clipboard = "grin"
    
      # Select a portion of the text and replace it
      Edit.Keys("[Home][Right][Right]![Right]![Right]![Right]![Right]![Right]")
      Edit.Keys("^v")

    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 "A smile without a cat"
      Sys.Clipboard = "grin"

      ' Select a portion of the text and replace it
      Edit.Keys "[Home][Right][Right]![Right]![Right]![Right]![Right]![Right]"
      Edit.Keys "^v"
    End Sub

    DelphiScript

    procedure Main;
    var p, Edit;
    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('A smile without a cat');
      Sys.Clipboard := 'grin';

      // Select a portion of the text and replace it
      Edit.Keys('[Home][Right][Right]![Right]![Right]![Right]![Right]![Right]');
      Edit.Keys('^v');
    end;

    C++Script, C#Script

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

      // Obtain the edit object and fill it with text
      var p = Sys["Process"]("NOTEPAD");
      var Edit = p["Window"]("Notepad")["Window"]("Edit");
      Edit["Keys"]("A smile without a cat");
      Sys["Clipboard"] = "grin";

      // Select a portion of the text and replace it
      Edit["Keys"]("[Home][Right][Right]![Right]![Right]![Right]![Right]![Right]");
      Edit["Keys"]("^v");
    }

To replace the entire text in the control with new text, simply use the SetText action or the wText property to specify new text, like in the example below. Note that in this case, the insertion point is automatically moved to the beginning of the control’s text.

JavaScript, JScript

function Main()
{
  // Run Notepad
  WshShell.Run("notepad.exe", SW_SHOWNORMAL);

  // Obtain the edit object
  var p = Sys.Process("NOTEPAD");
  var Edit = p.Window("Notepad").Window("Edit");
  Edit.SetText("Why?");
  Delay(1000);

  // Replace all of the text within Edit control
  Edit.SetText("Why not?");
}

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.SetText("Why?")
  Delay(1000)

  # Replace all of the text within Edit control
  Edit.SetText("Why not?")

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.SetText "Why?"
  Delay 1000

  ' Replace all of the text within Edit control
  Edit.SetText "Why not?"
End Sub

DelphiScript

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

  // Obtain the edit object
  p := Sys.Process('NOTEPAD');
  Edit := p.Window('Notepad').Window('Edit');
  Edit.SetText('Why?');
  Delay(1000);

  // Replace all of the text within Edit control
  Edit.SetText('Why not?');
end;

C++Script, C#Script

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

  // Obtain the edit object
  var p = Sys["Process"]("NOTEPAD");
  var Edit = p["Window"]("Notepad")["Window"]("Edit");
  Edit["SetText"]("Why?");
  Delay(1000);

  // Replace all of the text within Edit control
  Edit["SetText"]("Why not?");
}

See Also

Working With Edit Controls in Desktop Windows Applications
Entering Text into an Edit Control 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
wText Property (Edit Controls)
SetText Action (Edit and Combo Box Controls)
Working With Text Edit Controls in Web Applications

Highlight search results