Win 表单计算器:按钮 0 - 9 用于执行重复任务的事件处理程序

发布于 2024-07-11 07:22:51 字数 216 浏览 10 评论 0原文

我有一个看起来像典型计算器的 win 表单 UI。 当然,我不想为每个数字按钮(0-9)重写相同的逻辑。 我希望能够知道单击了哪个按钮,以便我可以根据它的文本属性执行计算。 我是否应该创建一个接受按钮对象作为参数的方法来促进代码重用? 有没有更好的办法? 我想听听更多终身 Win Form 应用程序开发人员将如何处理这个问题。 我试图将我的逻辑排除在用户界面之外。

谢谢!

I have a win form UI that looks like a typical calculator. Naturally I do not want to rewrite the same logic for every single numeric button(0-9). I want to be able to know which button was clicked so I can perform calculations based on it's text property. Should I just make a method that accepts the button object as a parameter to promote code reuse? Is there a better way? I would like to hear how more tenured Win Form app devs would handle this. I am attempting to keep my logic out of the UI.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北城挽邺 2024-07-18 07:22:51

事件处理程序的典型签名是void EventHandler(object sender, EventArgs e)。 其中重要的部分是对象发送者。 这是触发事件的对象。 对于 Button 的单击事件,发送者将是该 Button

void digitButton_Click(object sender, EventArgs e)
{
    Button ButtonThatWasPushed = (Button)sender;
    string ButtonText = ButtonThatWasPushed.Text; //the button's Text
    //do something

    //If you store the button's numeric value in it's Tag property
    //things become even easier.
    int ButtonValue = (int)ButtonThatWasPushed.Tag;
}

The typical signature of an event handler is void EventHandler(object sender, EventArgs e). The important part there is the object sender. This is the object that fired the event. In the case of a Button's click event, the sender will be that Button.

void digitButton_Click(object sender, EventArgs e)
{
    Button ButtonThatWasPushed = (Button)sender;
    string ButtonText = ButtonThatWasPushed.Text; //the button's Text
    //do something

    //If you store the button's numeric value in it's Tag property
    //things become even easier.
    int ButtonValue = (int)ButtonThatWasPushed.Tag;
}
不羁少年 2024-07-18 07:22:51

当指定事件处理程序时,您可以注册相同的函数来处理多个事件(如果在 VS.Net 中,请转到属性,选择事件部分(闪电按钮),单击下拉菜单以单击)。 这样,您将编写一个事件处理函数来处理所有按钮。

示例 (C#) 如果按钮创建和事件注册是在代码中完成的:

public void digitButtons_Click(object sender, EventArgs eventArgs) {
    if(sender is Button) {
        string digit = (sender as Button).Text;
        // TODO: do magic
    }
}

public void createButtons() {
    for(int i = 0; i < 10; i++) {
        Button button = new Button();
        button.Text = i.ToString();
        button.Click += digitButtons_Click;
        // TODO: add button to Form
    }
}

When specifying the event handler, you can register the same function to handle multiple events (if in VS.Net, go to properties, select the events section (lightning button), click on the drop down for Click). This way you will write one event handler function to handle all the buttons.

Example (C#) if button creation and event registration are done in code:

public void digitButtons_Click(object sender, EventArgs eventArgs) {
    if(sender is Button) {
        string digit = (sender as Button).Text;
        // TODO: do magic
    }
}

public void createButtons() {
    for(int i = 0; i < 10; i++) {
        Button button = new Button();
        button.Text = i.ToString();
        button.Click += digitButtons_Click;
        // TODO: add button to Form
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文