C#停止程序的流程并在不重新启动的情况下将其重置

标签 c# winforms button radio-button

我正在编写一个简单的 WindowsForm 程序,其中包含单选按钮、按钮和一个习惯实现的 InputBox。

相关程序逻辑:我单击组框中的一个单选按钮 -> 启用按钮 -> 单击按钮启动自定义输入框,询问 1 个值和是/取消按钮。

|
V

如果单击"is"按钮 -> 继续逻辑流程

如果单击“取消”按钮 -> 出现带有是/否按钮的弹出窗口(“您的输入已取消,是否要退出应用程序?”)

    |
    V

如果单击"is"按钮 -> 退出程序

如果没有点击按钮 -> 应用与点击重置按钮相同的逻辑,这将重置整个程序而不重新启动它 <--------这就是我想要实现的(下面提供了所有相关方法)

问题是什么?

当我简单地点击重置按钮时,它会应用所有需要的操作并停止,等待我的进一步输入。当我在上面提到的弹出窗口中单击“否”按钮时,这就是我试图实现的确切结果。

然而,事实并非如此。在 Debug模式下,在我单击“否”按钮后,它会像我想要的那样经历“重置”按钮的整个逻辑,但是之后,它进入 IF 语句 (在我的标记中buttonGetWorkHours_Click 方法)。我不想要那样,我希望它在应用重置按钮的逻辑并等待我的输入(单选按钮/按钮点击)后停止流程。

我尝试了什么

我在 SO 中搜索了多个线程并尝试实现几个建议。这些建议的结果在 inputBoxProcedure 方法中被注释掉了。另外,我一直在寻找 similar posts ,这会给我正确的想法。但是they state不重新加载是不可能的。基于此thread ,我想过实现额外的变量来检查重置逻辑是否正在运行,但似乎不必要地复杂。

终极问题:

我看到了一个测试可执行文件,所以我知道它是可能的,不像帖子和线程所说的那样。能否请您指出正确的方向,说明如何继续进行?

相关代码片段方法

为了节省大家的时间,我将只包含与我的问题相关的方法。

private void buttonGetWorkHours_Click(object sender, EventArgs e)
  {
     if (radioButtonJobStatusFull.Checked) 
     {
        inputBoxProcedure("Enter the total hours worked by the full time employee for the week", "40");
     }
     else if (radioButtonJobStatusPart.Checked)
     {
        inputBoxProcedure("Enter the total hours worked by the part time employee for the week", "30");
     }

     //if, else if, else, unrelated to the lines above
     if()    //<----------------the logic goes here after going through the whole Reset button logic
     {}
     else if()
     {}
     else()
     {}
  }

private void buttonReset_Click(object sender, EventArgs e)
  {
      //clear all radiobutton selections
      //set all strings to empty
      //disable several buttons and groupboxes
      //hide several labels
  }

private void inputBoxProcedure(string text, string defaulttext)
  {
     InputBoxResult result = InputBox.Show(text, "Hours Entry", defaulttext, new InputBoxValidatingHandler(inputBox_Validating));
     if (result.OK)
     {
        labelHoursWorked.Text = result.Text.Trim();
     }
     else
     {
        if (MessageBox.Show("Input was cancelled. Do you wish to quit the application?", "Input Cancelled", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
        {
           Close();
        }
        else
        {
           buttonReset_Click(this, new EventArgs());
           //Form fr = new Form();
           //fr.Show();
           //this.Close();
           //Application.Restart();
        }
     }
  }

最佳答案

尝试将 inputBoxProcedure 更改为:

private bool inputBoxProcedure(string text, string defaulttext)
{
    InputBoxResult result = InputBox.Show(text, "Hours Entry", defaulttext, new InputBoxValidatingHandler(inputBox_Validating));
    if (result.OK)
    {
        labelHoursWorked.Text = result.Text.Trim();
    }
    else
    {
        if (MessageBox.Show("Input was cancelled. Do you wish to quit the application?", "Input Cancelled", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
        {
            Close();
        }
        else
        {
            buttonReset_Click(this, new EventArgs());
            //Form fr = new Form();
            //fr.Show();
            //this.Close();
            //Application.Restart();

            return false; // Added
        }
    }

    return true; // Added
}

请注意,返回类型 void 变为 bool 并且添加了两行 return

buttonGetWorkHours_Click 中更改:

if (radioButtonJobStatusFull.Checked)
{
    inputBoxProcedure("Enter the total hours worked by the full time employee for the week", "40");
}
else if (radioButtonJobStatusPart.Checked)
{
    inputBoxProcedure("Enter the total hours worked by the part time employee for the week", "30");
}

进入:

if (radioButtonJobStatusFull.Checked)
{
    if (!inputBoxProcedure("Enter the total hours worked by the full time employee for the week", "40"))
        return;
}
else if (radioButtonJobStatusPart.Checked)
{
    if (!inputBoxProcedure("Enter the total hours worked by the part time employee for the week", "30"))
        return;
}

这样,在重置后,函数inputBoxProcedure 将返回false。当函数返回 false 时,函数 buttonGetWorkHours_Click 将返回,从而阻止进一步执行。

希望对您有所帮助。

关于C#停止程序的流程并在不重新启动的情况下将其重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38641367/

相关文章:

c# - 如何在没有编译时常量的情况下设置可选参数

c# - 使用 CheckedListBox 设置 DataGridView 列的可见性

c# - Infix.ParseOrUndefined 序列顺序

c# - 有没有简单的方法(包装器/字典)从 Keys enum 转换为 SenKeys 字符串?

c# - 使用带有进度报告的 C# 提取 ZipFile

vb.net - 将图标从主窗体继承到vb.net中的子窗体

javascript - 使用 javascript 禁用 HTML 中的按钮

visual-studio - 点击一个按钮,该按钮将无法点击,直到重置

button - Godot:检测按下的鼠标是否在按钮上方

c# - 此代码应返回 Task 还是 Task<object>?