User forums > Help

wxWidgets Setting textbox value can't update programmatically

(1/2) > >>

DrOli:
wxWidgets Setting textbox value can't update programmatically

I have created a Tool that helps navigate the current page in the CB editor.  It is connected via the usual means, and is passed ${ACTIVE_EDITOR_FILENAME}.

This works as expected with the tool as a consoal app.

I would like to upgrade the Tool with a wxWidget inteface (c.f. consol).  The command line information is captured in the bool CB_SRList_wxApp::OnInit() function via wxApp::argc, and wxApp::argv, and is converted to a string containing the file name of the file in the CB editor window; that works correctly.

The difficulty arises when I try to write the value of the parsed string obtained from the command line to a textbox in the wx frame.

0) a direct attempt to setvalue errors out on "private".  Changing the textbox to public introduces other errors, and in any case is overwritten the next time the resource is edited.

1) a line such as: FileName_TextCtrl1 -> SetValue("hello"); ... I get error: 'FileName_TextCtrl1' was not declared in this scope

2) a line such as: CB_SRList_wxFrame::OnFileName_TextCtrl1TextEnter("hello")  ... I get " not a member of"

3) another experiment was to create a separate sub to handle the assignment:

void CB_SRList_wxFrame::SetValue_of_FileName_TextCtrl1(const char *t)
{
    this->FileName_TextCtrl1->SetValue(t);
}

with hopes of calling that form ::OnInit() , there using    SetValue_of_FileName_TextCtrl1("hello"); ... this results with the error is "not declared in scope"


I've tried various permutations with "extern",  and this that and the other thing, but no joy.


Surely, there must be some direct way to set the value of text box programmatically, and from the ::OpenInit() structure, or indeed from "anywhere" within the project.

Indeed, full effect of the tool requires filling a grid with values obtained from an external DLL, which is also executed in ::OpenInit().

I've spent quite a bit of time searching this subject on the web, and the context there seem to be different, or for project structures not created by CB scripts.


In any case, any suggestions would be appreciated to set the value of the Frame's elements programatically, and generally from "outside" of the Frame's core structure.

DrOli:
According to the wxWidgets Forums:

Given a member textctrl named FileName_TextCtrl1 in a wxs, in CB_SRList_wxMain.cpp, make the following sub a member of CB_SRList_wxFrame

void CB_SRList_wxFrame::SetValue_of_FileName_TextCtrl1(wxString t)
{
        this->FileName_TextCtrl1->SetValue(t);
}

Then in CB_SRList_wxApp.cpp  I should be able to use

   CB_SRList_wxFrame.SetValue_of_FileName_TextCtrl1( InitialFileName_wxs);

in the OnInit() block to affect a change in the txtCtrl on the screen, where InitialFileName_wxs is wxString,

... it  errors out with: error: expected unqualified-id before '.' token

... replacing the "." with "::" or any normal variations produces other errors, such as

cannot call member function 'void CB_SRList_wxFrame::SetValue_of_FileName_TextCtrl1(wxString)' without object


I've tried a number of the "event" strategies, but quite frankly, those are beyond my experience.


Please advise.

All I need to be able to do is to set a text ctrl with a string obtained during initialisation.  Unfortunately, the OnInit() is processed AFTER the frame is set, so while i can set the text ctlr to a variable string in the class, that occurs prior to OnInit, so the text is NULL (after the OnInit, the text is correct, but then can't get it to the widget on screen ... very frustrating).

BlueHazzard:

Are you using the wxSmith and it is somehow producing unexpected code?

If not: Why is this codeblocks relevant?
This is a programming/wxWidgets problem, and this is the wrong forum for this kind of questions...


--- Quote from: DrOli on April 17, 2016, 05:38:39 am ---0) a direct attempt to setvalue errors out on "private".  Changing the textbox to public introduces other errors, and in any case is overwritten the next time the resource is edited.

--- End quote ---
Create a function in your frame class, what accesses your control and is not private?


--- Quote from: DrOli on April 17, 2016, 05:38:39 am ---1) a line such as: FileName_TextCtrl1 -> SetValue("hello"); ... I get error: 'FileName_TextCtrl1' was not declared in this scope

--- End quote ---
Without code no idea what you are talking about, but the error message is quite clear..... function not declared...


--- Quote from: DrOli on April 17, 2016, 05:38:39 am ---2) a line such as: CB_SRList_wxFrame::OnFileName_TextCtrl1TextEnter("hello")  ... I get " not a member of"

--- End quote ---
Without code, no idea what you are talking about....


--- Quote from: DrOli on April 17, 2016, 05:38:39 am ---3) another experiment was to create a separate sub to handle the assignment:

void CB_SRList_wxFrame::SetValue_of_FileName_TextCtrl1(const char *t)
{
    this->FileName_TextCtrl1->SetValue(t);
}

with hopes of calling that form ::OnInit() , there using    SetValue_of_FileName_TextCtrl1("hello"); ... this results with the error is "not declared in scope"

--- End quote ---

Without context (code) not possible to help... but here again, the error is quite clear...


--- Quote from: DrOli on April 17, 2016, 05:38:39 am ---I've tried various permutations with "extern",  and this that and the other thing, but no joy.

--- End quote ---
????


--- Quote from: DrOli on April 17, 2016, 05:38:39 am ---Surely, there must be some direct way to set the value of text box programmatically, and from the ::OpenInit() structure, or indeed from "anywhere" within the project.

--- End quote ---
http://docs.wxwidgets.org/3.0.0/classwx_text_entry.html#a90f876b2dd83ba5c97ba0c193b386e9f

All your questions are standard programming/architecture  questions, but have nothing to do with codeblocks...

i would recommend to ask this question at the wxWidgets forum, or a general programming forum, like stack overflow

greetings

DrOli:
Many thanks for the extensive commentary. 

Though, our original post was in two parts, and clearly the second part had overcome the vast majority of issues that were raised in the first part.  As such, and while we appreciate your time, we are not sure why you had taken time to comment on so many items, and only, in the first part, that had been clearly resolved in the second part, and  prior to your response (e.g. all the non declared bits, just for example).

Regarding some of your queries:

1) The code is produced in CB with wxSmith as a wxWidget project, and amongst other things:

a) It creates the code automatically.

b) Much of that code can't be changed in any manageable way, since every time wxSmith has a go at it, it deletes/re-writes large chunks of code, so any edits we had made in those parts vanish.  That is decidedly not how a pure wxWidgets programme seems to work, and clearly it is what contributed the some of the issues in part 1 of the original post, but which was overcome by the second part of the original post.

c) The structure of the code produced automatically by wxSmith seems to be different is subtle, and perhaps important ways, compared to what we have seen in our research of "pure" wx code.  We are too new at this to be able to comment on the implications of that.

d) Thank you for the link (http://docs.wxwidgets.org/3.0.0/classwx_text_entry.html#a90f876b2dd83ba5c97ba0c193b386e9f), but it is of little help, as clearly we had been using that already (as in the code example in our original post).  The issue is that wxSmith declares the widgets as Private, so there is no direct way to use/change them from outside of the core code.

Indeed, as the second part of our post demonstrates, wx doesn't like its bits being changed from the "outside" (such as from OnInit()), and that is why, when we found on the wxWidget forum (as stated in the post prior to your response), we used

void CB_SRList_wxFrame::SetValue_of_FileName_TextCtrl1(wxString t)
{
        this->FileName_TextCtrl1->SetValue(t);
}

which (as must be clear from the post) we managed to make a "member" and we managed to "declare", once we figured out that wxSmith kept deleting our edits, and so we required a different approach, and which apparently worked (wrt to creating a member "manually", as we could not figure a way to do it from wxSmith directly, which seemed to would make this a CB question for that reason alone).

e) As such, and given this is essentially our first proper wxWidget/wsSmith project, we could not be sure how much of the difficulties were a CB issue or a wx issue, or possibly some combination of both.

... and thus it seemed reasonably to ask the question on the CB forum also, though clearly, the attention is on the SECOND part of the original post.

... as it happens we also asked the question on the wxWidget forum, as yet there has not been a response ... I hope they don't tell us that it's a CB matter, go back to CB.

Any way, for the record, and I am not sure how much of this you want (as in the past we've been told not to create long posts), but here is the code (the crucial line is in CB_SRList_wxApp.cpp, further down, and I have commented it ... if a line has something like //DrO in it, then it is one of ours), :

CB_SRList_wxMain.cpp:
/***************************************************************
 * Name:      CB_SRList_wxMain.cpp
 * Purpose:   Navigation Tool/Plug-In for Fortran procedures, etc
 * Author:     ()
 * Created:   2016-04-15
 * Copyright:  ()
 * License:
 **************************************************************/

#include "wx_pch.h"
#include "CB_SRList_wxMain.h"
#include <wx/msgdlg.h>

//!#include <wx/event.h>       //! DrO

#include <iostream>         //! DrO

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

//helper functions
enum wxbuildinfoformat {
    short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

//(*IdInit(CB_SRList_wxFrame)
const long CB_SRList_wxFrame::ID_STATICTEXT1 = wxNewId();
const long CB_SRList_wxFrame::ID_CHOICE1 = wxNewId();
const long CB_SRList_wxFrame::ID_GRID1 = wxNewId();
const long CB_SRList_wxFrame::ID_TEXTCTRL1 = wxNewId();
const long CB_SRList_wxFrame::ID_BUTTON1 = wxNewId();
const long CB_SRList_wxFrame::ID_PANEL1 = wxNewId();
const long CB_SRList_wxFrame::ID_MENUITEM1 = wxNewId();
const long CB_SRList_wxFrame::idMenuQuit = wxNewId();
const long CB_SRList_wxFrame::ID_MENUITEM2 = wxNewId();
const long CB_SRList_wxFrame::idMenuAbout = wxNewId();
const long CB_SRList_wxFrame::ID_STATUSBAR1 = wxNewId();
//*)

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

CB_SRList_wxFrame::CB_SRList_wxFrame(wxWindow* parent,wxWindowID id)
{
   void SetValue_of_FileName_TextCtrl1( wxString t);            // DrO

    //(*Initialize(CB_SRList_wxFrame)
    wxMenuItem* MenuItem2;
    wxMenuItem* MenuItem1;
    wxMenu* Menu1;
    wxMenuBar* MenuBar1;
    wxMenu* Menu2;

    Create(parent, id, _("Fortran Navigation"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("id"));
    SetClientSize(wxSize(459,592));
    Move(wxPoint(900,39));
    Panel1 = new wxPanel(this, ID_PANEL1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL1"));
    StaticText1 = new wxStaticText(Panel1, ID_STATICTEXT1, _("Sort by: "), wxPoint(216,8), wxSize(72,24), wxALIGN_RIGHT, _T("ID_STATICTEXT1"));
    Choice1 = new wxChoice(Panel1, ID_CHOICE1, wxPoint(296,8), wxDefaultSize, 0, 0, 0, wxDefaultValidator, _T("ID_CHOICE1"));
    Grid1 = new wxGrid(Panel1, ID_GRID1, wxPoint(0,104), wxSize(447,436), 0, _T("ID_GRID1"));
    Grid1->CreateGrid(30,5);
    Grid1->EnableEditing(true);
    Grid1->EnableGridLines(true);
    Grid1->SetRowLabelSize(25);
    Grid1->SetDefaultColSize(70, true);
    Grid1->SetColLabelValue(0, _("Line"));
    Grid1->SetColLabelValue(1, _("Type"));
    Grid1->SetColLabelValue(2, _("Procedure"));
    Grid1->SetColLabelValue(3, _("Kind"));
    Grid1->SetColLabelValue(4, _("References"));
    Grid1->SetDefaultCellFont( Grid1->GetFont() );
    Grid1->SetDefaultCellTextColour( Grid1->GetForegroundColour() );
    FileName_TextCtrl1 = new wxTextCtrl(Panel1, ID_TEXTCTRL1, _("FileName"), wxPoint(8,56), wxSize(432,21), 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));
    ReCalcButton = new wxButton(Panel1, ID_BUTTON1, _("ReCalc"), wxPoint(32,16), wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
    MenuBar1 = new wxMenuBar();
    Menu1 = new wxMenu();
    MenuItem3 = new wxMenuItem(Menu1, ID_MENUITEM1, _("Open"), wxEmptyString, wxITEM_NORMAL);
    Menu1->Append(MenuItem3);
    Menu1->AppendSeparator();
    MenuItem1 = new wxMenuItem(Menu1, idMenuQuit, _("Quit\tAlt-F4"), _("Quit the application"), wxITEM_NORMAL);
    Menu1->Append(MenuItem1);
    MenuBar1->Append(Menu1, _("&File"));
    Menu3 = new wxMenu();
    MenuItem4 = new wxMenuItem(Menu3, ID_MENUITEM2, _("ReCalc"), wxEmptyString, wxITEM_NORMAL);
    Menu3->Append(MenuItem4);
    MenuBar1->Append(Menu3, _("Action"));
    Menu2 = new wxMenu();
    MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("About\tF1"), _("Show info about this application"), wxITEM_NORMAL);
    Menu2->Append(MenuItem2);
    MenuBar1->Append(Menu2, _("Help"));
    SetMenuBar(MenuBar1);
    StatusBar1 = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1"));
    int __wxStatusBarWidths_1[1] = { -1 };
    int __wxStatusBarStyles_1[1] = { wxSB_NORMAL };
    StatusBar1->SetFieldsCount(1,__wxStatusBarWidths_1);
    StatusBar1->SetStatusStyles(1,__wxStatusBarStyles_1);
    SetStatusBar(StatusBar1);

    Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&CB_SRList_wxFrame::OnQuit);
    Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&CB_SRList_wxFrame::OnAbout);
    //*)

}

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

void CB_SRList_wxFrame::OnQuit(wxCommandEvent& event)
{
    Close();
}

void CB_SRList_wxFrame::OnAbout(wxCommandEvent& event)
{
    wxString msg = wxbuildinfo(long_f);
    wxMessageBox(msg, _("Welcome to..."));
}

void CB_SRList_wxFrame::SetValue_of_FileName_TextCtrl1(wxString t)         // DrO
{
        this->FileName_TextCtrl1->SetValue(t);
}

CB_SRList_wxMain.h

/***************************************************************
 * Name:      CB_SRList_wxMain.h
 * Purpose:   Defines Application Frame
 * Author:     ()
 * Created:   2016-04-15
 * Copyright:  ()
 * License:
 **************************************************************/

#ifndef CB_SRLIST_WXMAIN_H
#define CB_SRLIST_WXMAIN_H

//(*Headers(CB_SRList_wxFrame)
#include <wx/stattext.h>
#include <wx/menu.h>
#include <wx/textctrl.h>
#include <wx/panel.h>
#include <wx/grid.h>
#include <wx/choice.h>
#include <wx/button.h>
#include <wx/frame.h>
#include <wx/statusbr.h>
//*)

class CB_SRList_wxFrame: public wxFrame
{
    public:

        CB_SRList_wxFrame(wxWindow* parent,wxWindowID id = -1);
        virtual ~CB_SRList_wxFrame();

        void SetValue_of_FileName_TextCtrl1( wxString t);            // DrO


    private:

        //(*Handlers(CB_SRList_wxFrame)
        void OnQuit(wxCommandEvent& event);
        void OnAbout(wxCommandEvent& event);
        void OnSetFocus(wxFocusEvent& event);
        void OnPaint(wxPaintEvent& event);
        void OnFileName_TextCtrl1TextEnter(wxCommandEvent& event);
        //*)

        //(*Identifiers(CB_SRList_wxFrame)
        static const long ID_STATICTEXT1;
        static const long ID_CHOICE1;
        static const long ID_GRID1;
        static const long ID_TEXTCTRL1;
        static const long ID_BUTTON1;
        static const long ID_PANEL1;
        static const long ID_MENUITEM1;
        static const long idMenuQuit;
        static const long ID_MENUITEM2;
        static const long idMenuAbout;
        static const long ID_STATUSBAR1;
        //*)

        //(*Declarations(CB_SRList_wxFrame)
        wxTextCtrl* FileName_TextCtrl1;
        wxMenu* Menu3;
        wxMenuItem* MenuItem4;
        wxPanel* Panel1;
        wxStaticText* StaticText1;
        wxGrid* Grid1;
        wxButton* ReCalcButton;
        wxMenuItem* MenuItem3;
        wxStatusBar* StatusBar1;
        wxChoice* Choice1;
        //*)

        DECLARE_EVENT_TABLE()
};

#endif // CB_SRLIST_WXMAIN_H


CB_SRList_wxApp.cpp, I have put a comment at the line where we the difficulties in

/***************************************************************
 * Name:      CB_SRList_wxApp.cpp
 * Purpose:   Code for Application Class
 * Author:     ()
 * Created:   2016-04-15
 * Copyright:  ()
 * License:
 **************************************************************/

#include "wx_pch.h"
#include "CB_SRList_wxApp.h"

#include <iostream>         //! DrO

//(*AppHeaders
#include "CB_SRList_wxMain.h"
#include <wx/image.h>
//*)


IMPLEMENT_APP(CB_SRList_wxApp);

static wxString InitialFileName_wxs;         // DrO
bool lInitialFileName_wxs;            // DrO



bool CB_SRList_wxApp::OnInit()
{
    //(*AppInitialize
    bool wxsOK = true;
    wxInitAllImageHandlers();
    if ( wxsOK )
    {
       CB_SRList_wxFrame* Frame = new CB_SRList_wxFrame(0);
       Frame->Show();
       SetTopWindow(Frame);
    }
    //*)


    int CmdArgC = wxApp::argc;              //! DrO

    wxChar ** CmdArgV = wxApp::argv;       //! DrO


    lInitialFileName_wxs = FALSE;   // DrO


    //! also create count, for latter "full Char(Len=n)" assignment

      if( CmdArgC != 2)   // DrO
      {
            return wxsOK;
      };

    int CmdArgIndex = 1;   // DrO
    int CharCount = 0;   // DrO
    for (int j = 0; CmdArgV[CmdArgIndex][j] != '\0'; ++j)   // DrO
    {
      CharCount = CharCount + 1;

    }
      if( CharCount < 1 )   // DrO
      {
            return wxsOK;
      };


    char ArgVCharFull[CharCount+1] = {'\0'};   // DrO

    //! CONVERT TO Char(Len=CharCount)
    //! ------------------------------

    //! Start at 1 to skip the program name, since [0] just give prog name
    //! -----------------------------------

    for ( int j = 0; j<CharCount ; ++j)   // DrO
    {
      ArgVCharFull[j] = CmdArgV[CmdArgIndex][j];
    }

    lInitialFileName_wxs = TRUE;   // DrO
    InitialFileName_wxs = wxString::FromUTF8(ArgVCharFull);   // DrO

    CB_SRList_wxFrame::SetValue_of_FileName_TextCtrl1( InitialFileName_wxs);          // DrO// <<<<<<<<<<<<<< This is where the problems are

    return wxsOK;

}


/***************************************************************
 * Name:      CB_SRList_wxApp.h
 * Purpose:   Defines Application Class
 * Author:     ()
 * Created:   2016-04-15
 * Copyright:  ()
 * License:
 **************************************************************/

#ifndef CB_SRLIST_WXAPP_H
#define CB_SRLIST_WXAPP_H

#include <wx/app.h>

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

#endif // CB_SRLIST_WXAPP_H

BlueHazzard:
1) please use code tags if you post code (The # symbol in the new post editor: Press it and insert your code between the two tags)
2) Sry, i didn't read anywhere that you used wxSmith... It could be possible that i have forgotten it after reading all thix text. Young people have only a attention span of a few lines text...
3) Yes wxSmith removes code if you put it in the wrong places. This behaviour is normal for all code generators i have worked until now (mostly for embedded things). To avoid this simply don't put your code between the

--- Code: ---//(* //*)
--- End code ---
parts. wxSmith will ignore all code outside this tags and overwrite all things inside it
4) wxSmith does not generate different code. wxWdigets is a huge library, and c++ is a huge language. There are many ways to do things, but all roads lead to Rome...

Thank you for the code, now i am able to help you. But as i see this are all c++ errors and this post will probably get locked because c++ questions are not allowed here...
I will try to help you anyway and point you in some directions...


--- Code: ---CB_SRList_wxFrame::CB_SRList_wxFrame(wxWindow* parent,wxWindowID id)
{
   void SetValue_of_FileName_TextCtrl1( wxString t);            // DrO
--- End code ---
not valid c++ code


--- Code: ---    int CmdArgC = wxApp::argc;              //! DrO

    wxChar ** CmdArgV = wxApp::argv;       //! DrO
--- End code ---
i am not sure if this is the correct way to access these variables (it has something to do with base class and derived class)... you can use only argc and argv , without wxApp... http://docs.wxwidgets.org/trunk/overview_app.html


--- Code: ---    //! CONVERT TO Char(Len=CharCount)
    //! ------------------------------

    //! Start at 1 to skip the program name, since

    just give prog name


    //! -----------------------------------
--- End code ---

Not a valid comment, wont compile

--- Code: --- CB_SRList_wxFrame::SetValue_of_FileName_TextCtrl1( InitialFileName_wxs);          // DrO// <<<<<<<<<<<<<< This is where the problems are

--- End code ---
Of course this won't work... You access a member function of a class without instance. This works only for static member functions... This is a big misunderstanding of c++ so i recommend you to read a bit about c++.

But to defend your point, there is some "problem" with the code from wxSmith. You can't access the

--- Code: ---CB_SRList_wxFrame* Frame = new CB_SRList_wxFrame(0);
--- End code ---
variable within the wxApp code.
@devx: How is the right way to solve this issue? Normally there would be a "Is memeber" in the resource editor, to make the frame a member of the App class. but this option is missing...

i will try to create a workaround. Give me some time..

greetings

Navigation

[0] Message Index

[#] Next page

Go to full version