禁用按钮

发布于 2024-12-11 17:06:47 字数 168 浏览 0 评论 0原文

我想防止用户在按钮已经执行且过程尚未完成时单击按钮两次。

我使用的是紧凑框架 3.5,当用户在已经执行的按钮或其他按钮上单击两次时,我会遇到问题。我想在程序执行时禁用所有按钮,并在进程完成时再次启用它们。

操作系统:Windows mobile 6.1
框架:.NET 3.5 CF

I want to prevent the user clicking two times on a button when it has been already executing and the process is not finished.

I am using compact framework 3.5 and I have problems when the user clicks two times on a button that is already executing or some other button. I want to disable all buttons when the program is executing and enable them again when the process is done.

OS: Windows mobile 6.1
Framework: .NET 3.5 CF

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

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

发布评论

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

评论(3

骄傲 2024-12-18 17:06:47

尝试在 Click 处理程序的范围内添加 this.Enabled = false 第一件事(这是有问题的表单)。完成后请务必将其设置回 true。如果这一切都在处理程序的范围内,您可能需要 Application.DoEvents() 或 Update() 来显示可见的进度。进行任何扩展处理的首选方法可能是生成一个后台线程并使用 Invoke 和 BeginInvoke 从中更新您的 UI。

Try adding this.Enabled = false first thing (this being the form in question) in the scope of your Click handler. Be sure to set it back to true when done. You may need to Application.DoEvents() or Update() to display visible progress if this all in the scope of the handler. Probably the preferred way to do any extended processing though would be to spawn a background thread and update your UI from it using Invoke and BeginInvoke.

静若繁花 2024-12-18 17:06:47

我发现在构建 Windows Mobile 应用程序时需要经常执行此操作,因此创建了一个简单的实用程序类。

public static class FormUtility
{
    /// <summary>
    /// Lock the form whilst processing
    /// </summary>
    /// <param name="controlCollection"></param>
    /// <param name="enabled"></param>
    public static void FormState(Control.ControlCollection controlCollection, bool enabled)
    {
        foreach (Control c in controlCollection)
        {
            c.Enabled = enabled;
            c.Invalidate();
            c.Refresh();
        }
    }
 }

我需要做的就是拨打一条热线来锁定表格。

FormUtility.FormState(this.Controls, false);

您最终应该得到类似

 private void btnSave_Click(object sender, EventArgs e)
 {
      FormUtility.FormState(this.Controls, false);

      //Do your work
      if (!SaveSuccessful())
           //Renable if your validation failed
           FormUtility.FormState(this.Controls, true);
 }

EDIT 的结果:我认为 @tcarvin 的建议是您不需要在每个控件上调用刷新,而只需使控件,然后刷新容器,这将导致所有无效的控件立即重绘。我还没有测试过这个,但是对类似的东西做了一个小改变......

    public static void FormState(Form form, bool enabled)
    {
        foreach (Control c in form.Controls)
        {
            c.Enabled = enabled;
            c.Invalidate();
        }

        form.Refresh();
    }

然后使用

FormUtility.FormState(this, true);

I found that I needed to do this quite often when building a windows mobile application so made a simple utility class.

public static class FormUtility
{
    /// <summary>
    /// Lock the form whilst processing
    /// </summary>
    /// <param name="controlCollection"></param>
    /// <param name="enabled"></param>
    public static void FormState(Control.ControlCollection controlCollection, bool enabled)
    {
        foreach (Control c in controlCollection)
        {
            c.Enabled = enabled;
            c.Invalidate();
            c.Refresh();
        }
    }
 }

All I need to do was then call one line to lock the form down.

FormUtility.FormState(this.Controls, false);

You should end up with something like

 private void btnSave_Click(object sender, EventArgs e)
 {
      FormUtility.FormState(this.Controls, false);

      //Do your work
      if (!SaveSuccessful())
           //Renable if your validation failed
           FormUtility.FormState(this.Controls, true);
 }

EDIT : I think what @tcarvin is suggesting is that you do not need to call refresh on every control but simply invalidate the controls and then refresh the container which will cause all the invalidated controls to redraw at once. I haven't tested this but a small change to something like...

    public static void FormState(Form form, bool enabled)
    {
        foreach (Control c in form.Controls)
        {
            c.Enabled = enabled;
            c.Invalidate();
        }

        form.Refresh();
    }

Then use

FormUtility.FormState(this, true);
寄人书 2024-12-18 17:06:47

对于名为 button1 的按钮,这是最简单的方法:

void button1_Clicked(object sender, EventArgs e) {
  button1.Enabled = false;
  try {
    // put your code here
  } finally {
    button1.Enabled = true;
  }
}

This is the easiest way, for a button called button1:

void button1_Clicked(object sender, EventArgs e) {
  button1.Enabled = false;
  try {
    // put your code here
  } finally {
    button1.Enabled = true;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文