0

I'm learning C++ with wxSmith inside CodeBlocks.

I've built an app with two frames and I need to access variables in the top level window.

void test12052019Frame::OnButton1Click(wxCommandEvent& event)
{
wxString test1 = "";
wxString test2 = "";

test1 = TextCtrl1->GetValue();
test2 = TextCtrl2->GetValue();

// compare/parse userid/password
// Access ERP system and get credential schema
// build the treeview

if(test1 == "titou" && test2 == "123123"){
// todo auth. against Mysql
    wxMessageBox("You're in !!\n");
    TreeCtrl1->Show();
    TreeCtrl1->ExpandAll();
}else
    wxMessageBox("You're out !!\nWrong userid/password");
}
void test12052019Frame::OnTreeCtrl1ItemActivated(wxTreeEvent& event)
{
//TreeCtrl1 is my tree
//when I click on any option of my tree
//it activate a wxMessageBox with the label
//of the option selected...
//just let go your imagination :)

NewFrameActivities *mynewwindow = new NewFrameActivities(this);

wxString thelabel;
wxTreeItemId test3;

test3 = TreeCtrl1->GetSelection();
thelabel = TreeCtrl1->GetItemText(test3);

wxMessageBox(thelabel);

mynewwindow->SetLabel(thelabel);
//mynewwindow->StaticBox1->SetLabel(tosomething...);

//I have a textctrl in this event (textctrl1) and
//textctrl(textctrl1) in another event 

mynewwindow->TextCtrl1->ChangeValue("thetest\nsetvalue\n");
mynewwindow->Show(TRUE);
}

I need to know the username from the first event (top level window, textctrl1) Visual demo

enter image description here

smarch
  • 13
  • 4
  • Just call `TextCtrl1->GetValue()` like you do in the first function? – Some programmer dude May 15 '19 at 14:25
  • from another frame ? I don't think so – smarch May 15 '19 at 15:01
  • To make it works, I had to declare wxString test1 and test2 at the top which is pretty ugly... – smarch May 15 '19 at 15:16
  • No you don't. You can use the result of the call directly when calling your next function. As in e.g. `mynewwindow->TextCtrl1->ChangeValue(TextCtrl1->GetValue());`. *Experiment!* – Some programmer dude May 15 '19 at 15:25
  • whenever you reference textctrl1, it's within its context. I have two frames with a textctrl named textctrl1. And btw declaring those 2 variables globally make it works...ugly – smarch May 15 '19 at 15:40
  • Better names would make the code easier to read. Showing your declarations would also lead to less confusion. – JaMiT May 15 '19 at 16:58
  • Your question would be easier to understand if you were to describe what you are trying to accomplish, using the code to *help* explain. (Rather than relying on the non-working code as the primary way of conveying what you are trying to accomplish.) Try to explain in broad terms, at a high level where you don't need to refer to variable names. – JaMiT May 15 '19 at 17:01
  • Inside a member function of the `test12052019Frame` class, the symbol `TextCtrl1` will always reference `test12052019Frame::TextCtrl1`. In fact, `TextCtrl1->GetValue()` is actually equal to `this->TextCtrl1->GetValue()`. – Some programmer dude May 15 '19 at 17:16
  • what the famous 'this' ?!?! i will modify accordingly 'this' – smarch May 15 '19 at 17:40
  • 'this' refer to current instance in the event OnTreeCtrl1ItemActivated(wxTreeEvent& event) I need variables test1 and test2 from event OnButton1Click – smarch May 15 '19 at 17:50
  • It seems you could use [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to read. – Some programmer dude May 16 '19 at 06:10
  • agree, I'm a C programmer, C++ is new to me and playing around with wxwidgets and RAD tools is a little bit to much – smarch May 16 '19 at 12:39

2 Answers2

0

Name the controls in the frame class declaration:

class MyFrame : public wxFrame
{
    .... ctors, etc

    wxTextCtrl *texctrl_user;
    wxTextCtrl *texctrl_pass;

    wxButton *button1;

    //Function for button handling
    void OnButton1Click(wxCommandEvent& event);
    ....
};

Create the controls at MyFrame ctor, or similar

texctrl_user = new wxTextCtrl(....);
texctrl_pass = new wxTextCtrl(....);

button1 = new wxButton(.......);

And bind the button click handler:

button1 ->Bind(wxEVT_BUTTON, &MyFrame::OnButton1Click, this, button1->GetId());

Now, because the function and the textctrls are members of the same class, any one is accesible from inside the class:

void MyFrame::OnButton1Click(wxCommandEvent& event)
{
    wxString str_user = texctrl_user->GetValue();
    wxString str_pass = texctrl_pass->GetValue();
    ...
}
Ripi2
  • 7,031
  • 1
  • 17
  • 33
  • I'm about to update code with mysql functionalities... and the youtube link (video) is updated. – smarch May 17 '19 at 14:54
0

@smarch,

  1. Asking the user for credentials will better be done in the dialog and not a frame.
  2. Inn the dialog instance create 2 functions that will return text control 1 and text control 2. Make those functions public, while keeping the control itself private (or protected - depends on the RAD tool you use).
  3. In the main frame do the following:

    void MainFrame::AskForCredentials() { MyCredentialsDialog dlg; int result = dlg.ShowModal(); if( result == wxID_OK ) { wxString userID = dlg.GetUserIDCtrl()->GetValue(); wxString pass = dlg.GetPasswordCtrl()->GetValue(); } }

  4. Test 5.Enjoy.

Igor
  • 5,620
  • 11
  • 51
  • 103