C# - 如何识别表格3上表1中单击的每个按钮?

发布于 2025-02-12 05:57:15 字数 537 浏览 0 评论 0原文

我对此遇到了很多麻烦,

我在Visual Studio上使用了3个表单,第一个(表格1)是一个状态屏幕,带有许多绿色按钮,每个按钮代表机器。单击一个按钮后,应该打开一个基本的登录屏幕以验证您有权访问下一步(表格2),并且登录后,还有另一个Winform(表格3)来生成故障报告。提交失败报告后,它应标识表格1上的单击按钮,并更改按钮的颜色和文本。

这里的交易是我不知道如何识别按钮,我在表格3上有此代码:

private void button1_Click(object sender, EventArgs e)
    {
       foreach (Form1 f1 in Application.OpenForms.OfType<Form1>())
        {
            f1.button1.Text = "your text here"; // Provided button1 is public.

        }
    }

如果我以这种方式离开代码,它将仅适用于form1的按钮1。我希望它标识任何点击按钮

I'm having a lot of troubles with this

I'm using 3 forms on visual studio, the first one (form 1) is a status screen with a lot of green buttons, each one representing a machine. Once you click one of the buttons it should open a basic login screen to verify you have rights to access to the next step (Form 2), and once you log in, there's another winform (Form 3) to generate a failure report. Once you submit the failure report, it should identify the clicked button on form 1 and change the color and text of the button.

The deal here is that i don't know how to identify the button, i have this code on form 3:

private void button1_Click(object sender, EventArgs e)
    {
       foreach (Form1 f1 in Application.OpenForms.OfType<Form1>())
        {
            f1.button1.Text = "your text here"; // Provided button1 is public.

        }
    }

If i leave the code this way, it will work only for the button1 of Form1. I want it to identify any clicked button

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

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

发布评论

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

评论(1

撞了怀 2025-02-19 05:57:18

发件人实际上是触发事件的按钮

铸造发件人按钮您可以访问其各种属性以执行您喜欢的任何事情,例如,检查button name> name> name> name> name>。

var button = sender as Button:
if (button.Name == "myButton1") {
   // do this and that for button 1
}

此答案旨在直接回答您的问题,没有对设计的评论,这可能是次优的。

sender is actually the Button that triggered the event.

After casting sender to Button you can access its various properties to do whatever you like, e.g. check the Button name or change its text.

var button = sender as Button:
if (button.Name == "myButton1") {
   // do this and that for button 1
}

This answer is intended to answer your question directly, no comments on design which might be suboptimal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文