首页 > 解决方案 > 如何在事件处理程序 C# 之前阻止代码

问题描述

我正在寻找一种方法来阻止我的代码,直到运行事件处理程序。

我的代码如下:

private void extendBt_Click(object sender, EventArgs e)
{
    DisplayPrompt("Select a Construction line (L) or Two point (P) to extend");

    // I want this code is blocked here, then user must press on P or L to continue

    this.KeyPress += new KeyPressEventHandler(keypressed); 

    if (key == "p")
    {
        // Method "P";
    }
    if (key == "l")
    {
        // Method "L";
    }
    else // If L or P aren't pressed => How to return to the select event on keyboard  ?
}

标签: c#winformseventsdelegatesasync-await

解决方案


这似乎是一个基于 Windows 窗体的应用程序,基于KeyPressEventHandler.

从用户界面设计 POV 来看,表单应该有一个RadioButtonCombobox多个用户可以做出选择。在此之前,禁用扩展按钮。

如果您想坚持这种方式,您将需要创建另一个表单并将选择控件放在那里。在按钮单击时,显示该 CustomForm 使用ShowDialog,并且当用户选择时,基于此返回DialogResult主表单。您可以为 P/L 选择是/否,然后进行相应的处理。

那时你根本不需要KeyPressEventHandler


推荐阅读