关闭表单后启用按钮 [英] Enable a button after closing a form

查看:21
本文介绍了关闭表单后启用按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在关闭第二个表单时启用主表单中的按钮.我读过一些关于调用的东西,但不太了解.

I want to Enable a button from the main form whenever a second form has been closed. I've read something about invokes, but didn't understood much.

我怎样才能做到这一点?

How could I achieve this?

推荐答案

当您关闭第二个表单时,它的 FormClosed 事件会自动引发.在引发事件之前,您可以将事件处理程序注册到事件.这使您能够编写在事件发生时自动执行的代码.

When you close the second form, its FormClosed event is automatically raised. Before an event is raised, you can register an event handler to events. This enables you to write code, which is automatically executed, when the event occurrs.

在 .NET 中注册事件处理程序是通过向事件添加委托实例来实现的.委托是一种描述方法签名的类型.如果您使用 gcnew 实例化一个委托,您会将它与代码中的一个函数相关联.您可以自己调用委托(此处不需要),也可以将其传递给其他一些代码,然后该代码可以调用它.后者用于事件.

Registering an event handler in .NET is realised by adding a delegate instance to the event. A delegate is a type that describes a method signature. If you instantiate a delegate with gcnew you associate it with a function in your code. You can call the delegate by yourself (which is not needed here) or you can pass it to some other code, which then can invoke it. The latter one is used for events.

对于您的情况,这意味着:

For your case that means:

  1. 查看 FormClosed 事件的委托类型.它是 FormClosedEventHandler,它被定义为 delegate void FormClosedEventHandler(Object^ sender, FormClosedEventArgs^ e)
  2. 这意味着您必须实现一个不返回任何内容(void)并接受两个参数的方法:一个 System::Object 和一个 System::Windows::Forms::FormClosedEventArgs
  3. 实例化一个 FormClosedEventHandler 委托并将其与您的方法相关联
  4. 注册到第二个表单上的 FormClosed 事件并启用事件处理程序中的按钮.
  1. Look at the FormClosed event's delegate type. It is the FormClosedEventHandler which is defined as delegate void FormClosedEventHandler(Object^ sender, FormClosedEventArgs^ e)
  2. This means you must implement a method returning nothing (void) and accepting two arguments: a System::Object and a System::Windows::Forms::FormClosedEventArgs
  3. Instantiate a FormClosedEventHandler delegate and associate it with your method
  4. Register to the FormClosed event on the second form and enable the button in the event handler.

示例:

ref class MainForm
{
    ...

    // event handler function (compatible to the FormClosedEventHandler delegate)
    void OnSecondFormClosed(Object^ sender, FormClosedEventArgs^ e)
    {
        myButton->Enabled = true;
    }

    void DoSomethingWithSecondForm(Form^ secondForm)
    {
        // get a disabled Button
        myButton->Enabled = false;
        // create an event handler by instantiating a delegate
        FormClosedEventHandler^ handler = gcnew FormClosedEventHandler(this, &MainForm::OnSecondFormClosed);
        // register event handler
        secondForm->FormClosed += handler;
    }

    ...
}

(我没有编译代码,但它是一般的工作方式)

(I did not compile the code, but this is how it works in general)

当两个涉及的表单都是从同一个线程中创建时,不需要做一些额外的调用.否则,您必须将控件的更改放入创建控件的同一线程中.您可以通过将委托传递给 Control::InvokeControl::BeginInvoke 来实现这一点.

When both involved forms are created from within the same thread, there is no need to do some additional Invoke. Otherwise you must put changes to controls into the same thread that created the control. You can achieve this by passing a delegate to Control::Invoke or Control::BeginInvoke.

// event handler function (compatible to the FormClosedEventHandler delegate)
void OnSecondFormClosed(Object^ sender, FormClosedEventArgs^ e)
{
    if (myButton->InvokeRequired)
    {
        // create a delegate to call the same event handler again
        FormClosedEventHandler^ handler = gcnew FormClosedEventHandler(this, &MainForm::OnSecondFormClosed);
        // BeginInvoke causes the delegate to be called asynchronously from the UI thread
        myButton->BeginInvoke(handler, sender, e);
        // nothing to be done here, the actual work happens when the delegate is actually called
        return;
    }

    myButton->Enabled = true;
}

这篇关于关闭表单后启用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆