wxTextCtrl

Introduction

The wxTextCtrl (wxWidgets: wxTextCtrl Class Reference) control allows text to be displayed and edited. A wxTextCtrl can be single line or multiline.

Single Line Example

The wxTextCtrl control is easy to use. Below we show a simple example of a single line wxTextCtrl. The example is a simple modification of the MinimalApp2 example we presented in the minimal application tutorial. We simply added a wxTextCtrl to the frame contents. The full source for this example is available from our GitHub repository: wxWidgetsTutorials/StandardControls/WxTextCtrl1.

 File: StandardControls/WxTextCtrl1/src/WxTextCtrl1Frame.cpp
#include "WxTextCtrl1Frame.h"
#include <wx/panel.h>
#include <wx/textctrl.h>
#include <wx/sizer.h>

WxTextCtrl1Frame::WxTextCtrl1Frame(const wxString& title)
    : wxFrame(NULL, wxID_ANY, title)
{
    // Create a top-level panel to hold all the contents of the frame
    wxPanel* panel = new wxPanel(this, wxID_ANY);

    // Create the wxTextCtrl widget
    wxTextCtrl* textCtrl = new wxTextCtrl(panel, wxID_ANY,
        L"The quick brown fox jumps over the lazy dog",
        wxDefaultPosition, wxSize(350, 50));

    // Set up the sizer for the panel
    wxBoxSizer* panelSizer = new wxBoxSizer(wxHORIZONTAL);
    panelSizer->Add(textCtrl, 1, wxEXPAND);
    panel->SetSizer(panelSizer);

    // Set up the sizer for the frame and resize the frame
    // according to its contents
    wxBoxSizer* topSizer = new wxBoxSizer(wxHORIZONTAL);
    topSizer->Add(panel, 1, wxEXPAND);
    SetSizerAndFit(topSizer);
}

As you can see from the code above the creation of a wxTextCtrl control is very simple.

One thing to pay attention though is that by default the best size of wxTextCtrl is not based on its contents so you have to take care of resizing the control if you want its size to be updated following changes to its contents. In our case we simply specified the desired size at construction time.

In case you are wondering how the best size is computed by default, here is the implementation of the wxTextCtrl::DoGetBestSize() method. It shows that by default the best size is based on the size of a string of DEFAULT_ITEM_WIDTH (100) characters.

 Snippet of file: textctrl.cpp
wxSize wxTextCtrl::DoGetBestSize() const
{
    return DoGetSizeFromTextSize( DEFAULT_ITEM_WIDTH );
}

The result of this code is shown on Figure 1.

Figure 1: The WxTextCtrl1 Application

The rest of the files don't have any significant changes compared to the MinimalApp2 code but are shown below for completeness.

 File: StandardControls/WxTextCtrl1/src/WxTextCtrl1Frame.h
#ifndef _TUTORIALS_WXWIDGETS_WXTEXTCTRL1FRAME_H_
#define _TUTORIALS_WXWIDGETS_WXTEXTCTRL1FRAME_H_

#include <wx/frame.h>

class WxTextCtrl1Frame : public wxFrame
{
public:
    WxTextCtrl1Frame(const wxString& title);
};

#endif
 File: StandardControls/WxTextCtrl1/src/WxTextCtrl1App.h
#ifndef _TUTORIALS_WXWIDGETS_WXTEXTCTRL1APP_H_
#define _TUTORIALS_WXWIDGETS_WXTEXTCTRL1APP_H_

#include <wx/app.h>

class WxTextCtrl1App : public wxApp
{
public:
    virtual bool OnInit();
};

#endif
 File: StandardControls/WxTextCtrl1/src/WxTextCtrl1App.cpp
#include "WxTextCtrl1App.h"
#include "WxTextCtrl1Frame.h"

wxIMPLEMENT_APP(WxTextCtrl1App);

bool WxTextCtrl1App::OnInit()
{
    WxTextCtrl1Frame* frame = new WxTextCtrl1Frame("WxTextCtrl1");
    frame->Show(true);
    return true;
}

Multiline Example

The full source for this example is available from our GitHub repository: wxWidgetsTutorials/StandardControls/WxTextCtrl2.

 File: StandardControls/WxTextCtrl2/src/WxTextCtrl2Frame.cpp
#include "WxTextCtrl2Frame.h"
#include<wx/panel.h>
#include<wx/textctrl.h>
#include<wx/sizer.h>

WxTextCtrl2Frame::WxTextCtrl2Frame(const wxString& title)
    : wxFrame(NULL, wxID_ANY, title)
{
    // Create a top-level panel to hold all the contents of the frame
    wxPanel* panel = new wxPanel(this, wxID_ANY);

    // Create the wxTextCtrl widget
    wxTextCtrl* textCtrl = new wxTextCtrl(panel, wxID_ANY,
        L"The quick brown fox jumps over the lazy dog\nand runs away",
        wxDefaultPosition, wxSize(350, 150), wxTE_MULTILINE);

    // Set up the sizer for the panel
    wxBoxSizer* panelSizer = new wxBoxSizer(wxHORIZONTAL);
    panelSizer->Add(textCtrl, 1, wxEXPAND);
    panel->SetSizer(panelSizer);

    // Set up the sizer for the frame and resize the frame
    // according to its contents
    wxBoxSizer* topSizer = new wxBoxSizer(wxHORIZONTAL);
    topSizer->Add(panel, 1, wxEXPAND);
    SetSizerAndFit(topSizer);
}
 File: StandardControls/WxTextCtrl2/src/WxTextCtrl2App.h
#ifndef _TUTORIALS_WXWIDGETS_WXTEXTCTRL2APP_H_
#define _TUTORIALS_WXWIDGETS_WXTEXTCTRL2APP_H_

#include <wx/app.h>

class WxTextCtrl2App : public wxApp
{
public:
    virtual bool OnInit();
};

#endif
 File: StandardControls/WxTextCtrl2/src/WxTextCtrl2App.cpp
#include "WxTextCtrl2App.h"
#include "WxTextCtrl2Frame.h"

wxIMPLEMENT_APP(WxTextCtrl2App);

bool WxTextCtrl2App::OnInit()
{
    WxTextCtrl2Frame* frame = new WxTextCtrl2Frame("WxTextCtrl2");
    frame->Show(true);
    return true;
}
 File: StandardControls/WxTextCtrl2/src/WxTextCtrl2Frame.h
#ifndef _TUTORIALS_WXWIDGETS_WXTEXTCTRL2FRAME_H_
#define _TUTORIALS_WXWIDGETS_WXTEXTCTRL2FRAME_H_

#include <wx/frame.h>

class WxTextCtrl2Frame : public wxFrame
{
public:
    WxTextCtrl2Frame(const wxString& title);
};

#endif

Events