problem geting values from TextCtrl Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
gtafan
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 201
Joined: Wed Mar 29, 2017 9:52 am

problem geting values from TextCtrl

Post by gtafan »

So here my main code from cpp files:

MyPanel implementation

Code: Select all

//(*IdInit(MyPanel)
const long MyPanel::ID_STATICTEXT1 = wxNewId();
const long MyPanel::ID_TEXTCTRL1 = wxNewId();
//*)

BEGIN_EVENT_TABLE(MyPanel,wxPanel)
	//(*EventTable(MyPanel)
	//*)
END_EVENT_TABLE()

MyPanel::MyPanel(wxWindow* parent, const wxString& l, wxWindowID id,const wxPoint& pos,const wxSize& size)
{
	//(*Initialize(MyPanel)
	Create(parent, id, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("id"));
	BoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
	label = new wxStaticText(this, ID_STATICTEXT1, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT1"));
	BoxSizer1->Add(label, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	valueCtrl = new wxTextCtrl(this, ID_TEXTCTRL1, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));
	BoxSizer1->Add(valueCtrl, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	SetSizer(BoxSizer1);
	BoxSizer1->Fit(this);
	BoxSizer1->SetSizeHints(this);

	Connect(ID_TEXTCTRL1,wxEVT_COMMAND_TEXT_ENTER,(wxObjectEventFunction)&MyPanel::OnvalueCtrlTextEnter);
	//*)
	valueCtrl->Bind(wxEVT_KILL_FOCUS, &MyPanel::OnKillFocus, this);
	label->SetLabel(l);
	wxFloatingPointValidator<float> val(3, &value, wxNUM_VAL_ZERO_AS_BLANK);
    val.SetRange(-10000, 10000);
    valueCtrl->SetValidator(val);
}

MyPanel::~MyPanel()
{
	//(*Destroy(MyPanel)
	//*)
}


void MyPanel::OnvalueCtrlTextEnter(wxCommandEvent& event)
{
    TransferDataFromWindow();
    wxLogMessage("%s=%.3f", label->GetLabel(), value);
    event.Skip();
}

void MyPanel::OnKillFocus(wxFocusEvent& event)
{
    TransferDataFromWindow();
    wxLogMessage("%s=%.3f", label->GetLabel(), value);
    event.Skip();
}
MyFrame implementation

Code: Select all

//(*IdInit(MyFrame)
const long MyFrame::ID_CUSTOM1 = wxNewId();
const long MyFrame::ID_CUSTOM2 = wxNewId();
const long MyFrame::ID_CUSTOM3 = wxNewId();
const long MyFrame::ID_PANEL2 = wxNewId();
const long MyFrame::ID_TEXTCTRL1 = wxNewId();
const long MyFrame::ID_PANEL1 = wxNewId();
const long MyFrame::ID_NOTEBOOK1 = wxNewId();
//*)

BEGIN_EVENT_TABLE(MyFrame,wxFrame)
    //(*EventTable(MyFrame)
    //*)
END_EVENT_TABLE()

MyFrame::MyFrame(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(MyFrame)
    Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("id"));
    SetClientSize(wxSize(640,480));
    BoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
    Notebook1 = new wxNotebook(this, ID_NOTEBOOK1, wxDefaultPosition, wxDefaultSize, 0, _T("ID_NOTEBOOK1"));
    Panel1 = new wxPanel(Notebook1, ID_PANEL1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL1"));
    BoxSizer2 = new wxBoxSizer(wxVERTICAL);
    Panel2 = new wxPanel(Panel1, ID_PANEL2, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL2"));
    BoxSizer3 = new wxBoxSizer(wxHORIZONTAL);
    Custom1 = new MyPanel(Panel2, "X");
    BoxSizer3->Add(Custom1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    Custom2 = new MyPanel(Panel2, "Y");
    BoxSizer3->Add(Custom2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    Custom3 = new MyPanel(Panel2, "Z");
    BoxSizer3->Add(Custom3, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    Panel2->SetSizer(BoxSizer3);
    BoxSizer3->Fit(Panel2);
    BoxSizer3->SetSizeHints(Panel2);
    BoxSizer2->Add(Panel2, 1, wxEXPAND, 5);
    TextCtrl1 = new wxTextCtrl(Panel1, ID_TEXTCTRL1, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE|wxTE_READONLY, wxDefaultValidator, _T("ID_TEXTCTRL1"));
    BoxSizer2->Add(TextCtrl1, 9, wxEXPAND, 5);
    Panel1->SetSizer(BoxSizer2);
    BoxSizer2->Fit(Panel1);
    BoxSizer2->SetSizeHints(Panel1);
    Notebook1->AddPage(Panel1, _("Page name"), false);
    BoxSizer1->Add(Notebook1, 1, wxEXPAND, 5);
    SetSizer(BoxSizer1);
    SetSizer(BoxSizer1);
    Layout();
    //*)
    wxLog::SetActiveTarget(new wxLogTextCtrl(TextCtrl1));
}
Everithing compiles well but when I am typing some value into 1 of 3 enterable textCtrls and presing Enter Button nothing hepends.
With Tab it worcks, but once the cursor is inside the loger textCtrl it can´t be mooved using Tab.
Last edited by gtafan on Wed Jan 23, 2019 2:57 pm, edited 1 time in total.
Manolo
Can't get richer than this
Can't get richer than this
Posts: 828
Joined: Mon Apr 30, 2012 11:07 pm

Re: problem geting values from TextCtrl

Post by Manolo »

Hi
Sorry, your code is too obfuscated. You don't show how the frame is created, why it contains several wxPanel/MyPanel, etc.

If using Connect() or better Bind(), you don't need BEGIN_EVENT_TABLE macros

The textctrl with parent=Panel1 seems not to be bound to any message handler.

If you want a wxTextCtrl to generate "enter-key pressed" event then use the style wxTE_PROCESS_ENTER for it.

TransferDataFromWindow is a member of a wxValidator, so I can't see how your code compiles, because of using it as a member of MyPanel.

But the worst of the post is that you don't explain the expected behaviour and result.

If I were you, I'd remove anything but a wxTextCtrl and its validator. Or start with something I know it works perfectly, and then add the control & validator & event handler. Study the docs, experiment different styles, and so on. If not, when a problem arises, you don't know where it comes from.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7477
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: problem geting values from TextCtrl

Post by ONEEYEMAN »

Hi,
Better yet - start with one of the samples provided with the library.
Make sure it works and then put some code on top of it.

Thank you.
gtafan
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 201
Joined: Wed Mar 29, 2017 9:52 am

Re: problem geting values from TextCtrl

Post by gtafan »

Manolo wrote:Hi
Sorry, your code is too obfuscated. You don't show how the frame is created, why it contains several wxPanel/MyPanel, etc.

If using Connect() or better Bind(), you don't need BEGIN_EVENT_TABLE macros

The textctrl with parent=Panel1 seems not to be bound to any message handler.

If you want a wxTextCtrl to generate "enter-key pressed" event then use the style wxTE_PROCESS_ENTER for it.

TransferDataFromWindow is a member of a wxValidator, so I can't see how your code compiles, because of using it as a member of MyPanel.

But the worst of the post is that you don't explain the expected behaviour and result.

If I were you, I'd remove anything but a wxTextCtrl and its validator. Or start with something I know it works perfectly, and then add the control & validator & event handler. Study the docs, experiment different styles, and so on. If not, when a problem arises, you don't know where it comes from.
I am using wxSmith for creating my wxWidget projects, as doing it manually is a huge pain in the as. MyPanel is the control I am using to get float values. The textctrl that has no event handler is just for loging.
Manolo
Can't get richer than this
Can't get richer than this
Posts: 828
Joined: Mon Apr 30, 2012 11:07 pm

Re: problem geting values from TextCtrl

Post by Manolo »

I am using wxSmith for creating my wxWidget projects, as doing it manually is a huge pain in the as.
That huge "pain" will teach you how to do things, and so how wxSmith works. Get rid of "automagic" tools unless you understand the underlaying "magic".
MyPanel is the control I am using to get float values.
From the docs
A panel is a window on which controls are placed.
You can not get any value from a wxPanel. Use a wxTextCtrl instead, or some more featured but complex control (grid, tree, etc)
The textctrl that has no event handler is just for loging
That may be right for TextCtrl1 with parent Panel1 created in MyFrame, although in this case you don't need Panel1, just use "this" (i.e. the MyFrame instance) as the parent of the logging-textctrl.
But valueCtrl of MyPanel is the one you need to bind() a handler, and the one to set a wxFloatingPointValidator:
This validator can be used with wxTextCtrl or wxComboBox (and potentially any other class implementing wxTextEntry interface)
gtafan
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 201
Joined: Wed Mar 29, 2017 9:52 am

Re: problem geting values from TextCtrl

Post by gtafan »

Manolo wrote:
I am using wxSmith for creating my wxWidget projects, as doing it manually is a huge pain in the as.
That huge "pain" will teach you how to do things, and so how wxSmith works. Get rid of "automagic" tools unless you understand the underlaying "magic".
MyPanel is the control I am using to get float values.
From the docs
A panel is a window on which controls are placed.
You can not get any value from a wxPanel. Use a wxTextCtrl instead, or some more featured but complex control (grid, tree, etc)
The textctrl that has no event handler is just for loging
That may be right for TextCtrl1 with parent Panel1 created in MyFrame, although in this case you don't need Panel1, just use "this" (i.e. the MyFrame instance) as the parent of the logging-textctrl.
But valueCtrl of MyPanel is the one you need to bind() a handler, and the one to set a wxFloatingPointValidator:
This validator can be used with wxTextCtrl or wxComboBox (and potentially any other class implementing wxTextEntry interface)
Typing code manually is not the bigest problem, since wxWidgets is not part of ANSI C++ there are some other ugly things to do, so I can worck with it, this thing should be done by the IDE, so I don´t have to care about it.
After enabling wxTE_PROCESS_ENTER there is a reaction when presing Enter on the textCtrl, so I can get the values.
Post Reply