2016-07-28 38 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

相關問題