Using '= txtSelect->GetValue();' with a wxTextCtrl. Ok in wxButtonClick, Not Function.

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
AshtonC1
Earned some good credits
Earned some good credits
Posts: 100
Joined: Wed Feb 18, 2015 4:56 pm

Using '= txtSelect->GetValue();' with a wxTextCtrl. Ok in wxButtonClick, Not Function.

Post by AshtonC1 »

In a simple C++ test app in Code::Blocks on Linux, I'm trying to extract the contents of a wxTextCtrl to re-display it in a wxMessageBox.

I have used this common method in another applications on the same PC (and a Windows 7 dev PC), just a different project name -- It works perfectly.
(the other project code works without the declaration line *)
I have a wxTextCtrl named txtQuery, it contains: 'SELECT * FROM user;'

When I run the following in a Function, it Builds, but creates an Assertion Failed error.

Code: Select all

void getTest()
{
wxTextCtrl *txtQuery = new wxTextCtrl(); //*
   wxString sqlLine = txtQuery->GetValue();
   wxMessageBox(sqlLine, _("Opened !!..."));
   }
The ASSERT indicates an Invalid txt ctrl, not sure where to go with that, it does exist.

This is the assert & backtrace info:
  • ASSERT INFO:
    ../src/gtk/textctrl.cpp(987): assert "m_text != __null" failed in GetValue(): invalid text ctrl

    BACKTRACE:
    [1] wxTextCtrl::GetValue() const
    [2] wxSQLi_417Frame::OnButton3Click(wxCommandEvent&) /home/dan/Documents/wxW_Projs/wxSQLi_417/wxSQLi_417Main.cpp:241
    [3] wxAppConsoleBase::CallEventHandler(wxEvtHandler*, wxEventFunctor&, wxEvent&) const
    [4] wxEvtHandler::ProcessEventIfMatchesId(wxEventTableEntryBase const&, wxEvtHandler*, wxEvent&)
    [5] wxEvtHandler::SearchDynamicEventTable(wxEvent&)
    [6] wxEvtHandler::TryHereOnly(wxEvent&)
    [7] wxEvtHandler::ProcessEventLocally(wxEvent&)
    [8] wxEvtHandler::ProcessEvent(wxEvent&)
    [9] wxWindowBase::TryAfter(wxEvent&)
    [10] wxEvtHandler::SafelyProcessEvent(wxEvent&)
    [11] g_signal_emit_valist
    [12] g_signal_emit
    [13] g_closure_invoke
    [14] g_signal_emit_valist
    [15] g_signal_emit
    [16] g_closure_invoke
    [17] g_signal_emit_valist
    [18] g_signal_emit
    [19] gtk_propagate_event
    [20] gtk_main_do_event
    [21] g_main_context_dispatch
    [22] g_main_loop_run
    [23] gtk_main
    [24] wxGUIEventLoop::DoRun()
    [25] wxEventLoopBase::Run()
    [26] wxAppConsoleBase::MainLoop()
    [27] wxEntry(int&, wchar_t**)
    [28] main /home/dan/Documents/wxW_Projs/wxSQLi_417/wxSQLi_417App.cpp:17
    [29] __libc_start_main
    [30] _start
Any advice appreciated.

(Platform: Linux Mint C::B 16.01, wxWidgwets 3.02)
Last edited by AshtonC1 on Sun Apr 23, 2017 1:37 pm, edited 5 times in total.
coderrc
Earned some good credits
Earned some good credits
Posts: 141
Joined: Tue Nov 01, 2016 2:46 pm

Re: Using '= txtSelect->GetValue();' with a wxTextCtrl. Ok in one app not another.

Post by coderrc »

just a guess, but you've newed up a textctrl but you havent Create()-ed it yet.
I dont have the source handy, but I assume the default constructor simply calls init which sets all members to NULL.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7480
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Using '= txtSelect->GetValue();' with a wxTextCtrl. Ok in one app not another.

Post by ONEEYEMAN »

Hi,
As coderrc said, you need to either call Create() or call a real constructor not the default one.

Just check with your other application code.

Thank you.
AshtonC1
Earned some good credits
Earned some good credits
Posts: 100
Joined: Wed Feb 18, 2015 4:56 pm

Re: Using '= txtSelect->GetValue();' with a wxTextCtrl. Ok in one app not another.

Post by AshtonC1 »

you need to either call Create() or call a real constructor not the default one.
Ok, I have created a new wxFrame with a single wxTextCtrl, button, and message box (in same project).
This works perfectly.

My troubled frame has everything this frame has. Should it have an additional Create too?

Code: Select all

//****************** Copy text from wxTextCtrl1 to string and wxMessageBox Demo ********************
//***** Uses default control names ******

#include "wxForm1.h"
#include <wx/msgdlg.h>

//(*InternalHeaders(wxForm1)
#include <wx/string.h>
#include <wx/intl.h>
//*)

//(*IdInit(wxForm1)
const long wxForm1::ID_BUTTON1 = wxNewId();
const long wxForm1::ID_TEXTCTRL1 = wxNewId();
//*)

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

wxForm1::wxForm1(wxWindow* parent,wxWindowID id)
{
	//(*Initialize(wxForm1)
	Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("id"));
	SetClientSize(wxSize(400,214));
	Button1 = new wxButton(this, ID_BUTTON1, _("Grab Text"), wxPoint(264,128), wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
	TextCtrl1 = new wxTextCtrl(this, ID_TEXTCTRL1, _("SELECT * FROM customers;"), wxPoint(56,64), wxSize(224,29), 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));

	Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&wxForm1::OnButton1Click);
	//*)
}

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

void wxForm1::OnButton1Click(wxCommandEvent& event)
{
    wxString sqlLine = TextCtrl1->GetValue();
    wxMessageBox(sqlLine, _("Success !!..."));
}
Could this be caused by changing the name of a wxTextCtrl, after a build?

I could delete any wxTextCtrl that causes this error and create new ones, but I wanted to understand the underlying cause of this, and correct.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7480
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Using '= txtSelect->GetValue();' with a wxTextCtrl. Ok in one frame not another.

Post by ONEEYEMAN »

Hi
Please compare the code.

You call

Code: Select all

New wxTextCtrl  ()
[/code

vs

[code]
New wxTextCtrl (parent, wxID_ANY,...)
Thank you.
AshtonC1
Earned some good credits
Earned some good credits
Posts: 100
Joined: Wed Feb 18, 2015 4:56 pm

Re: Using '= txtSelect->GetValue();' with a wxTextCtrl. Ok in one frame not another.

Post by AshtonC1 »

Please compare the code.
You call

New wxTextCtrl ()
vs
New wxTextCtrl (parent, wxID_ANY,...)
I'm not getting much from your hint.

I will compare my working code with default constructor:

Code: Select all

TextCtrl1 = new wxTextCtrl(this, ID_TEXTCTRL1, _("SELECT * FROM customers"), wxPoint(56,64), wxSize(224,29), 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));

wxString sqlLine = TextCtrl1->GetValue();
That does run.

To the error code.

It has been a very long day, not being able to look at this until late, please excuse my correction.
Last edited by AshtonC1 on Sat Apr 22, 2017 4:03 am, edited 3 times in total.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7480
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Using '= txtSelect->GetValue();' with a wxTextCtrl. Ok in one frame not another.

Post by ONEEYEMAN »

Hi,
So in one case you use the default constructor.
In another case you use a constructor with parameters.

And you really need some sleep. ;-)

Thank you.
'
AshtonC1
Earned some good credits
Earned some good credits
Posts: 100
Joined: Wed Feb 18, 2015 4:56 pm

Re: Using '= txtSelect->GetValue();' with a wxTextCtrl. Ok in one frame not another.

Post by AshtonC1 »

Apparently a new constructor is needed in a function, not for a wxButtonClick.
Can you suggest the precise syntax?

I can't find an example of this in any of my projects, they all use the GetValue() under a wxButtonClick.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7480
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Using '= txtSelect->GetValue();' with a wxTextCtrl. Ok in wxButtonClick, Not Function.

Post by ONEEYEMAN »

Hi,
I'm sure the one you used second time will suffice.

Thank you.
AshtonC1
Earned some good credits
Earned some good credits
Posts: 100
Joined: Wed Feb 18, 2015 4:56 pm

Re: Using '= txtSelect->GetValue();' with a wxTextCtrl. Ok in wxButtonClick, Not Function.

Post by AshtonC1 »

Thanks for your hints Oneeyeman.

I'm not getting this, my syntax is incorrect.
The ones I used first, second, third, and fourth failed.

This is the simple function, which Builds, but gives an Assertion Error when run:

Code: Select all

//*** Default constructor ***
  txtSQL = new wxTextCtrl(this, ID_TEXTCTRL3, _("SELECT * FROM user;"), wxPoint(80,112), wxSize(456,29), 0, wxDefaultValidator, _T("ID_TEXTCTRL3"));

void refreshGrid()
{
   wxTextCtrl *txtSQL = new wxTextCtrl();  //** Necessary to build **
      wxString sqlLine = txtSQL->GetValue();
        wxMessageBox(sqlLine);
 }
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7480
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Using '= txtSelect->GetValue();' with a wxTextCtrl. Ok in wxButtonClick, Not Function.

Post by ONEEYEMAN »

AshtonC1 wrote: Thanks for your hints Oneeyeman.

I'm not getting this, my syntax is incorrect.
The ones I used first, second, third, and fourth failed.

This is the simple function, which Builds, but gives an Assertion Error when run:

Code: Select all

//*** Default constructor ***
  txtSQL = new wxTextCtrl(this, ID_TEXTCTRL3, _("SELECT * FROM user;"), wxPoint(80,112), wxSize(456,29), 0, wxDefaultValidator, _T("ID_TEXTCTRL3"));
Does this code located inside some function?
Please post the complete code you tried to compile and execute here.

Also which assertion do you get when executing a complete code you will post?
AshtonC1 wrote:

Code: Select all

void refreshGrid()
{
   wxTextCtrl *txtSQL = new wxTextCtrl();  //** Necessary to build **
      wxString sqlLine = txtSQL->GetValue();
        wxMessageBox(sqlLine);
 }
 
Are you using the global function here?
Or this is inside some wxWindow-derived class?

Please post a complete code and not pieces of it.

Thank you.
AshtonC1
Earned some good credits
Earned some good credits
Posts: 100
Joined: Wed Feb 18, 2015 4:56 pm

Re: Using '= txtSelect->GetValue();' with a wxTextCtrl. Ok in wxButtonClick, Not Function.

Post by AshtonC1 »

Thanks Oneeyman,
Does this code located inside some function?
Please post the complete code you tried to compile and execute here.
Also which assertion do you get when executing a complete code you will post?
The is a new complete test frame I created using >GetValue() in both wxButton and a function.
It compiles fine. The button1 getvalue works, button2 function call produces the below error.

Code: Select all

#include "wxForm1.h" //*** GetValue() test , Button and Function ***
#include <wx/msgdlg.h>

//(*InternalHeaders(wxForm1)
#include <wx/string.h>
#include <wx/intl.h>
//*)

//(*IdInit(wxForm1)
const long wxForm1::ID_BUTTON1 = wxNewId();
const long wxForm1::ID_TEXTCTRL1 = wxNewId();
const long wxForm1::ID_BUTTON2 = wxNewId();
//*)

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

wxForm1::wxForm1(wxWindow* parent,wxWindowID id)
{
	//(*Initialize(wxForm1)
	Create(parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("wxID_ANY"));
	SetClientSize(wxSize(400,214));
	Button1 = new wxButton(this, ID_BUTTON1, _("Using wxButtonClick"), wxPoint(48,96), wxSize(288,29), 0, wxDefaultValidator, _T("ID_BUTTON1"));
	TextCtrl1 = new wxTextCtrl(this, ID_TEXTCTRL1, _("SELECT * FROM customers"), wxPoint(80,56), wxSize(224,29), 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));
	Button2 = new wxButton(this, ID_BUTTON2, _("Using Function"), wxPoint(48,136), wxSize(288,29), 0, wxDefaultValidator, _T("ID_BUTTON2"));

	Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&wxForm1::OnButton1Click);
	Connect(ID_BUTTON2,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&wxForm1::OnButton2Click);
	//*)
}

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

void wxForm1::OnButton1Click(wxCommandEvent& event) //*** No Problem ***
{
    wxString sqlLine = TextCtrl1->GetValue();
    wxMessageBox(sqlLine);
}

void getTextBox()  //*** Has Assertion error *****
{
  wxTextCtrl *TextCtrl1 = new wxTextCtrl();
   wxString sqlLine = TextCtrl1->GetValue();
    wxMessageBox(sqlLine);
}

void wxForm1::OnButton2Click(wxCommandEvent& event)
{
    void getTextBox();
    getTextBox();
}
******************* Error details ***************************
Image

Assert & Backtrace details:
  • ASSERT INFO:
    ../src/gtk/textctrl.cpp(987): assert "m_text != __null" failed in GetValue(): invalid text ctrl

    BACKTRACE:
    [1] wxTextCtrl::GetValue() const
    [2] getTextBox() /home/dan/Documents/wxW_Projs/wxSQLi_417/wxForm1.cpp:50
    [3] wxForm1::OnButton2Click(wxCommandEvent&) /home/dan/Documents/wxW_Projs/wxSQLi_417/wxForm1.cpp:57
    [4] wxAppConsoleBase::CallEventHandler(wxEvtHandler*, wxEventFunctor&, wxEvent&) const
    [5] wxEvtHandler::ProcessEventIfMatchesId(wxEventTableEntryBase const&, wxEvtHandler*, wxEvent&)
    [6] wxEvtHandler::SearchDynamicEventTable(wxEvent&)
    [7] wxEvtHandler::TryHereOnly(wxEvent&)
    [8] wxEvtHandler::ProcessEventLocally(wxEvent&)
    [9] wxEvtHandler::ProcessEvent(wxEvent&)
    [10] wxWindowBase::TryAfter(wxEvent&)
    [11] wxEvtHandler::SafelyProcessEvent(wxEvent&)
    [12] g_signal_emit_valist
    [13] g_signal_emit
    [14] g_closure_invoke
    [15] g_signal_emit_valist
    [16] g_signal_emit
    [17] g_closure_invoke
    [18] g_signal_emit_valist
    [19] g_signal_emit
    [20] gtk_propagate_event
    [21] gtk_main_do_event
    [22] g_main_context_dispatch
    [23] g_main_loop_run
    [24] gtk_main
    [25] wxGUIEventLoop::DoRun()
    [26] wxEventLoopBase::Run()
    [27] wxAppConsoleBase::MainLoop()
    [28] wxEntry(int&, wchar_t**)
    [29] main /home/dan/Documents/wxW_Projs/wxSQLi_417/wxSQLi_417App.cpp:17
    [30] __libc_start_main
    [31] _start
Last edited by AshtonC1 on Mon Apr 24, 2017 3:41 am, edited 1 time in total.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7480
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Using '= txtSelect->GetValue();' with a wxTextCtrl. Ok in wxButtonClick, Not Function.

Post by ONEEYEMAN »

Hi,
Why do you want to use a global function? And poorly defined?

What is your background? What language?

Global functiom does not have access to the "this" pointer. You need to pass it there.

But in any case - just make whatever you want inside the handler. Or make that function a class one.

Thank you.
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Using '= txtSelect->GetValue();' with a wxTextCtrl. Ok in wxButtonClick, Not Function.

Post by doublemax »

Code: Select all

wxTextCtrl *TextCtrl1 = new wxTextCtrl();
wxString sqlLine = TextCtrl1->GetValue();
ONEEYEMAN already wrote it multiple times, now i will do the same:

If you create a wxTextCtrl (or any other class derived from wxWindow) with the default constructor (= constructor that takes no parameters), the control is not yet created and initialized internally. You must pass at least the "parent" parameter to the constructor, otherwise you can't use it.
Use the source, Luke!
Post Reply