如何同时调用多个按钮

发布于 2024-11-09 01:11:51 字数 335 浏览 0 评论 0原文

我在同时调用多个按钮时遇到问题,因为每个按钮的工作进程不同,有超过 78 个文件夹。

我想在一个名为“button4”的按钮中同时调用所有按钮。现在它仅调用button1,而不适用于button2。

有什么办法可以同时调用这些按钮吗?

我的代码是:

    private void button4_Click_1(object sender, EventArgs e)
    {

        button1.PerformClick();
        button2.PerformClick();


    }

提前致谢。

I am having a problem in calling multiple buttons at the same time because each buttons works a different process there are more than 78 folders.

I want to call all the buttons at the same time in a single button called button4. Now it's calling button1 only and not working for button2.

Is there any way to call these buttons at the same time?

My code is:

    private void button4_Click_1(object sender, EventArgs e)
    {

        button1.PerformClick();
        button2.PerformClick();


    }

Thanks in Advance.

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

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

发布评论

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

评论(4

っ左 2024-11-16 01:11:51

通常,您不应该通过对其他按钮执行 UI 样式的单击来调用它们的行为。

只需调用您想要“单击”的按钮的相应事件处理方法即可。

示例代码:

private void button4_Click_1(object sender, EventArgs e)
{
   button1_Click_1(null, EventArgs.Empty);
   button2_Click_1(null, EventArgs.Empty);
   // and so on
}

You should in general not perform UI-style clicks on other buttons in order to invoke their behaviour.

Just call the respective event handling methods of the buttons you would like to "click".

example code:

private void button4_Click_1(object sender, EventArgs e)
{
   button1_Click_1(null, EventArgs.Empty);
   button2_Click_1(null, EventArgs.Empty);
   // and so on
}
梦屿孤独相伴 2024-11-16 01:11:51

您应该重构其他事件以调用命名良好的方法。

假设button1做了一些初始化;它应该看起来像这样:

private void button1_Click(object sender, EventArgs e)
{
    Initialize();
}

假设按钮2完成了初始化;它应该看起来像这样:

private void button2_Click(object sender, EventArgs e)
{
    FinalizeInitialization();
}

然后如果button4 完成所有这些;它应该看起来像这样:

private void button4_Click(object sender, EventArgs e)
{
    Initialize();
    FinalizeInitialization();

    WhateverElseButton4ShouldDo();
}

You should refactor the other events to call well-named methods.

Say button1 does some initialization; it should look like this:

private void button1_Click(object sender, EventArgs e)
{
    Initialize();
}

Say button2 finalizes that intialization; it should look like this:

private void button2_Click(object sender, EventArgs e)
{
    FinalizeInitialization();
}

Then if button4 does all of this; it should look like this:

private void button4_Click(object sender, EventArgs e)
{
    Initialize();
    FinalizeInitialization();

    WhateverElseButton4ShouldDo();
}
只为一人 2024-11-16 01:11:51

在大多数情况下,您根本不应该调用 PerformClick()。相反,您应该调用事件处理程序调用的相同方法。因此,如果单击按钮 3 的行为应与单击按钮 1 然后单击按钮 2 的行为相同,则您应该具有如下代码:(

private void button1_Click(object sender, EventArgs e)
{
    SomeAction();
}

private void button2_Click(object sender, EventArgs e)
{
    AnotherAction();
}

private void button3_Click(object sender, EventArgs e)
{
    SomeAction();
    AnotherAction();
}

作为旁注,您的按钮应具有描述性名称,而不是 button1 等.)

Under most circumstances, you shouldn't call PerformClick() at all. Instead, you should call the same methods your event handlers call. So, if clicking button 3 should behave as click clicking button 1 and then button 2, you should have code like this:

private void button1_Click(object sender, EventArgs e)
{
    SomeAction();
}

private void button2_Click(object sender, EventArgs e)
{
    AnotherAction();
}

private void button3_Click(object sender, EventArgs e)
{
    SomeAction();
    AnotherAction();
}

(As a side note, your buttons should have descriptive names, not button1 and the like.)

哭了丶谁疼 2024-11-16 01:11:51

我们无法说出这些按钮单击处理程序的作用。所以很难说哪里出了问题。但尝试将代码从按钮单击处理程序移开。创建一些包含单击按钮后应运行的代码的类。然后从按钮单击处理程序调用此类的方法。调试和测试该代码会更容易。

    public class ButtonActions
    {
        public void DoSomething() {...}
        public void DoSomething2() {...}
        public void DoSomething3() {...}

        public void DoAll()
        {
            DoSomething();
            DoSomething2();
            DoSomething3();
        }

    }

    // here instead of clicking all buttons call method that does it all
    protected void button_Click(object sender, EventArgs e)
    {
        var buttonActions = new ButtonActions();
        buttonActions.DoAll();
    }

We can't say what those button click handlers do. So it's hard to say what's wrong. But try moving the code away from button click handlers. Create some class that contains code that should run after button click. Then call this class' methods from button click handlers. It will be easier to debug and test that code.

    public class ButtonActions
    {
        public void DoSomething() {...}
        public void DoSomething2() {...}
        public void DoSomething3() {...}

        public void DoAll()
        {
            DoSomething();
            DoSomething2();
            DoSomething3();
        }

    }

    // here instead of clicking all buttons call method that does it all
    protected void button_Click(object sender, EventArgs e)
    {
        var buttonActions = new ButtonActions();
        buttonActions.DoAll();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文