2016-07-28 80 views
2

我正在写一个简单的WindowsForm程序,其中包含单选按钮,按钮和习惯实现的InputBox。C#停止程序的流程并重置它而不重新启动

相关程序逻辑:我点击groupbox - > enable按钮中的一个radiobutton - >点击按钮启动自定义输入框,询问1值和是/ Cancel按钮。

| 
V 

如果是按钮点击 - >用逻辑进行流动

如果取消按钮点击 - >出现弹出窗口(“你的输入被取消了,你想退出应用程序”)是/否按钮

| 
    V 

如果是按钮点击 - >退出程序

如果没有任何按键点击 - >应用相同的逻辑,如果复位按钮被按下,这将重置整个亲革兰氏阴性,而不需要重启< ---------这就是我想实现(下面提供了所有相关的方法)

问题是什么?

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

但是,情况并非如此。在调试模式下,点击No按钮后,它会像我想要的一样遍历整个Reset按钮的逻辑,但是之后,它进入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(); 
     } 
    } 
    } 

回答

1

尝试改变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; 
} 

这样,复位后,该funtion inputBoxProcedure将返回false。当函数返回false时,函数buttonGetWorkHours_Click将返回,从而防止进一步的执行。

我希望这会有所帮助。

+0

它的工作!谢谢一堆! –

+0

很高兴帮助!我在自己面前遇到过同样的问题:D – MasterXD

相关问题