2017-07-05 86 views
0

我想重新加載當前窗體(而不是主窗體),只要這兩個單選按鈕都未選中。我這樣做,但它不會工作。如何重新加載/重新打開相同的窗體C#

StreamWriter sw; 
using (sw = File.CreateText(path)) 
{ 
    if (OnewayRadio.Checked == true) 
    { 
     sw.WriteLine("One Way Ticket"); 
    } 
    else if (RoundRadio.Checked == true) 
    { 
     sw.WriteLine("Round Trip"); 
    } 
    else 
    { 
     MessageBox.Show("You have not selected your type of trip!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
     sw.Close(); 
     File.Delete(path); 
     sw = File.CreateText(path); 
    } 
    sw.WriteLine("Name: " + name.Text); 
    sw.WriteLine("Number: " + number.Text); 
} 
+0

你說的目前的形式是什麼意思?它是您的應用程序中的主要形式還是您從另一種形式打開它? –

+1

無論你在做什麼,都不要關閉StreamWriter,刪除文件,然後嘗試寫入它。這將是一個問題。 –

+1

此外,您發佈的代碼段中沒有任何內容與關閉或打開表單有關。 –

回答

0

通常的做法是當你在表單代碼完成執行,將設置一個公共DialogResult

來電者隨後可以讀取DialogResult並採取任何必要使用ShowDialog

來打開窗體行動。

對於您的情況,您可以將DialogResult設置爲Retry以用於此特定實例。然後,首戰可以運行一個循環,並繼續再次展現形式,而ShowDialog() == DialogResult.Retry

Form2 testDialog = new Form2(); 

// Show testDialog as a modal dialog and determine if DialogResult = OK. 
while (testDialog.ShowDialog() == DialogResult. Retry) 
{ 
    testDialog.Dispose(); 
    testDialog = null; 
    testDialog = new Form2(); 
} 
-3

使用否則,如果不是的人。

按照代碼:

StreamWriter sw; 
    using (sw = File.CreateText(path)) 
    { 
     if (OnewayRadio.Checked == true) 
     { 
      sw.WriteLine("One Way Ticket"); 
     } 
     else if (RoundRadio.Checked == true) 
     { 
      sw.WriteLine("Round Trip"); 
     } 
     else if (!OnewayRadio.Checked && !RoundRadio.Checked) 
     { 
      MessageBox.Show(@"You have not selected your type of trip!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
      sw.Close(); 
      File.Delete(path); 
      sw = File.CreateText(path); 
     } 
     sw.WriteLine("Name: " + name.Text); 
     sw.WriteLine("Number: " + number.Text); 
+0

對不起,先生,它並沒有在程序上做任何改變。 –

+0

你能解釋你的問題嗎?你究竟想要什麼? –