【问题标题】:Accessing variables outside scoop (wxSmith Project c++)访问 scoop 外部的变量(wxSmith Project c++)
【发布时间】:2019-05-15 14:21:42
【问题描述】:

我正在 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

【问题讨论】:

  • 就像在第一个函数中那样调用TextCtrl1->GetValue()
  • 来自另一个框架?我不这么认为
  • 为了让它工作,我不得不在顶部声明 wxString test1 和 test2 ,这很难看......
  • 不,你没有。您可以在调用下一个函数时直接使用调用结果。如在例如mynewwindow->TextCtrl1->ChangeValue(TextCtrl1->GetValue());实验!
  • 每当您引用 textctrl1 时,它都在其上下文中。我有两个带有名为 textctrl1 的 textctrl 的框架。顺便说一句,在全局范围内声明这两个变量使其工作......丑陋

标签: c++ codeblocks wxwidgets


【解决方案1】:

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

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

现在,因为函数 textctrls 是同一个类的成员,所以任何一个都可以从类内部访问:

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

【讨论】:

  • 我即将使用 mysql 功能更新代码......并且 youtube 链接(视频)已更新。
【解决方案2】:

@smarch,

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

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

  4. 测试 5.享受。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-09
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    相关资源
    最近更新 更多