Winforms VS 2013 winform应用程序:如何将通用对象转换为按钮类型
我正在使用winform在visual studio 2013上设计windows计算器 应用程序。我想为模拟计算器键盘的WinForms应用程序上的数字按钮编写一个单按钮处理程序。但为此,我必须首先将对象^sender转换为按钮类型have 已经尝试过这样做: 按钮通用=按钮发送器 但它给出了以下错误:Winforms VS 2013 winform应用程序:如何将通用对象转换为按钮类型,winforms,c++-cli,visual-c++-2013,Winforms,C++ Cli,Visual C++ 2013,我正在使用winform在visual studio 2013上设计windows计算器 应用程序。我想为模拟计算器键盘的WinForms应用程序上的数字按钮编写一个单按钮处理程序。但为此,我必须首先将对象^sender转换为按钮类型have 已经尝试过这样做: 按钮通用=按钮发送器 但它给出了以下错误: Error 3 error C1506: unrecoverable block scoping Error 4 IntelliSense: type name is not
Error 3 error C1506: unrecoverable block scoping
Error 4 IntelliSense: type name is not allowed
下面是我需要编写的myform.h
部分:
#pragma endregion
private: System::Void MyForm_Load(System::Object^ sender,
System::EventArgs^ e) {
}
private: System::Void button12_Click(System::Object^ sender,
System::EventArgs^ e) {
}
private: System::Void button9_Click(System::Object^ sender,
System::EventArgs^ e) {
}
private: System::Void button3_Click(System::Object^ sender,
System::EventArgs^ e) {
//this is the button i want to change to a generic button
//Button generic = Button sender;
results->Text += "1";
}
};
}
我不确定我是否理解这个问题,但根据我认为我理解的,我将尝试回答 听起来您想为模拟计算器键盘的WinForms应用程序上的数字按钮编写一个单一按钮处理程序。您可以通过为按钮处理程序事件编写一个带有签名的方法,然后将每个键盘按钮的click事件处理程序指向该单个函数来实现这一点。“sender”参数应包含点击的实际来源;您必须查询按钮的某些方面,以确定它代表的数字 编辑-修改一点以澄清 假设您已经为单个事件处理程序编写了shell,该事件处理程序是上面显示的签名的事件方法,并将该处理程序命名为“allButtons\u click”,其一般形式如下:
`
private: System::Void allButtons_Click(System::Object^ sender, System::EventArgs^ e) {
Button^ foo = (Button^)sender;
// do stuff
}
`
)
要使每个按钮响应IDE中相同的Click事件处理程序,请执行以下操作:
Button^ myButton = (Button^)sender;
然后必须检查每个按钮,以确定它代表的值
您可以在运行时加载表单时执行此操作,但是我推断您的目标是在IDE中实现这一点。那些否决投票的人能证明他们的否决投票是正确的吗?在最后一行中,这额外的
}
是为了什么?它是为了关闭名称空间项目。您的意思是写一个类似于:Button^generic=static\u cast(sender)代码>?我想将System::Object^ sender更改为c语言中的按钮类型#它写为button b=(button)sender,但我不确定如何在c++中执行。是的,问题是如何精确地为所有数字按钮创建单个按钮处理程序。