在瓢之外访问变量(wxSmith Project C ++)

时间:2019-05-15 14:21:42

标签: c++ codeblocks wxwidgets

我正在CodeBlocks中使用wxSmith学习C ++。

我用两个框架构建了一个应用程序,我需要在顶层窗口中访问变量。

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);
}

我需要从第一个活动中知道用户名 (顶级窗口,textctrl1) Visual demo

enter image description here

2 个答案:

答案 0 :(得分:0)

在框架类声明中命名控件:

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

    wxTextCtrl *texctrl_user;
    wxTextCtrl *texctrl_pass;

    wxButton *button1;

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

在MyFrame ctor或类似位置创建控件

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

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

并绑定按钮单击处理程序:

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

现在,由于函数是文本类的成员,因此可以从类内部进行访问:

void MyFrame::OnButton1Click(wxCommandEvent& event)
{
    wxString str_user = texctrl_user->GetValue();
    wxString str_pass = texctrl_pass->GetValue();
    ...
}

答案 1 :(得分:0)

@smarch,

  1. 向用户询问凭据最好在对话框中而不是框架中完成。
  2. 在对话框实例中创建2个函数,这些函数将返回文本控件1和文本控件2。将这些函数公开,同时将控件本身保持私有(或受保护-取决于您使用的RAD工具)。
  3. 在主框架中执行以下操作:

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

  4. 测试 5.享受。

相关问题