-2

我正在使用 winform 应用程序在 Visual Studio 2013 上设计 Windows 计算器。我想为模拟计算器键盘的 WinForms 应用程序上的数字按钮编写一个按钮处理程序。但为了做到这一点,我必须首先将对象^发送者转换为按钮类型有。

已经尝试过这样做:

按钮通用 = 按钮发送者;

但它给出了以下错误:

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";
    }
};
}
4

1 回答 1

0

我不确定我是否理解这个问题,但根据我认为我理解的内容,我将尝试回答。

听起来您想为模拟计算器键盘的 WinForms 应用程序上的数字按钮编写单个按钮处理程序。为此,您可以编写一个带有按钮处理程序事件签名的方法,然后将每个键盘按钮的单击事件处理程序指向该单个函数。“sender”参数应该保存点击的实际来源;您将不得不询问按钮的某些方面以确定它代表的数字。

编辑- 修改一下以澄清

假设您已经为单个事件处理程序编写了 shell,这是您在上面显示的签名的事件方法,并将该处理程序命名为“allButtons_click”,其一般形式如下:

`
    private: System::Void allButtons_Click(System::Object^  sender, System::EventArgs^  e) {
        Button^ foo = (Button^)sender;
        // do stuff
    }

`

)

要使每个按钮响应 IDE 中的同一个 Click 事件处理程序:

  1. 在窗体的 GUI 设计器中,单击每个按钮,然后查看 Visual STudio 中的“属性”窗口
  2. 确保按下属性浏览器中的“事件”按钮(带有闪电图标)(启用)
  3. 寻找“点击”事件
  4. 在相邻的下拉列表中,选择“allButtons_click”
  5. 对每个键盘按钮重复该过程。

要将 Click 事件处理程序的 sender 参数转换为按钮,请尝试

Button^ myButton = (Button^)sender;

然后,您必须询问每个按钮以确定它代表的值。

您可以在表单加载时在运行时执行此操作,但我推断您的目标是在 IDE 中完成此操作。

于 2015-05-30T12:50:46.777 回答