wxButton调用函数的一个问题 Topic is solved

这是wxWidgets论坛的中文版本。在这里,您可以用您的母语汉语讨论上面任一子论坛所涉及的所有关于wxWidgets的话题。欢迎大家参与到对有价值的帖子的中英互译工作中来!
Post Reply
hats
Experienced Solver
Experienced Solver
Posts: 70
Joined: Wed Sep 16, 2009 3:50 pm
Location: China
Contact:

wxButton调用函数的一个问题

Post by hats »

我想点击一个button时,改变这个button的文本

Code: Select all

wxButton* button = new wxButton(this, ID_OK, _T("OK"), wxPoint(50,50), wxSize(80,20));//新增一个按钮button 
void MyFrame::OnButtonOK(wxCommandEvent& event)
{
    wxMessageBox(_T("You press me"),_T("Button"), wxOK | wxICON_INFORMATION, this);
    button->SetText("Test");//前面的正常,加上这句为什么编译不过?
    
}
当我在事件中加入button->SetText("Test");时,编译器就不通过,说是butonn未声明。要怎么办?[/size]
hats
Experienced Solver
Experienced Solver
Posts: 70
Joined: Wed Sep 16, 2009 3:50 pm
Location: China
Contact:

Post by hats »

不知道怎么回事,我用wxDev-C++新建的Dialog工程了用WxButton1->SetLabel(_T(" "));就能改变WxButton1的文本
Utensil
Moderator
Moderator
Posts: 423
Joined: Sun Feb 03, 2008 11:38 am
Location: China

Post by Utensil »

你的这句话写在哪里?

wxButton* button = new wxButton(this, ID_OK, _T("OK"), wxPoint(50,50), wxSize(80,20));

假设你写在构造函数里,那么button就只是构造函数的一个局部变量,在MyFrame::OnButtonOK里,当然就说没有这个变量啦~

只有当button是MyFrame类的成员变量时,才可能在MyFrame::OnButtonOK里访问到它。

wxDev自动帮你创建了这个成员变量,自然就可以了。请仔细阅读wxDEv为你自动生成的代码,并复习C++的基本知识。

另:应该使用setLabel来改变按钮的文字。

-Utensil

-Utensil
In fascination of creating worlds by words, and in pursuit of words behind the world.

On Github: http://utensil.github.com
Technical Blog in Chinese: http://utensil.iteye.com/
Post Reply