2013-05-11 4 views
-1

내 프로그램에 SaveFileDialog가 있습니다. 문제는 대화 상자에서 "취소"를 클릭하면 다른 SaveFileDialog가 열리는 것입니다. 그러나 두 번째 SaveFileDialog에서 취소를 클릭하면 세 번째 부분에서 이 표시되지 않으므로이 나타나므로 루프 또는 이와 비슷한 것이 아닙니다. 내 SaveFileDialog가 이상한 방식으로 작동하도록하는 것을 볼 수 없습니다. 분명히 사용자가 첫 번째 SaveFileDialog에서 취소를 클릭하면 폼에 반환되도록이 문제를 해결해야합니다.취소하면 내 SaveFileDialog가 다시 표시되는 이유는 무엇입니까?

private void SaveFile() 
    { 
     if (filepath == null) 
     { 
      SaveFileAs(); 
      } 

     else 
     { 
      StreamWriter sw = new StreamWriter(filepath); 
      try 
      { 
       sw.WriteLine(richTextBoxPrintCtrl1.Rtf); 
       richTextBoxPrintCtrl1.Modified = false; 
       sw.Close(); 
       lastsave.Text = "Last Saved: " + DateTime.Now.ToString(); 

      } 
      catch (Exception exc) 
      { 
       MessageBox.Show("Failed to save file. \n \n" + exc.Message); 
      } 
      finally 
      { 
       if (sw != null) sw.Close(); 
      } 

그리고 SaveFileAs

private void SaveFileAs() 
    { 
     SaveFileDialog sfdSaveFile = new SaveFileDialog();//Creates a new instance of the SaveFileDialog 
     sfdSaveFile.Title = "Save File";//The title of the SaveFileDialog window 
     sfdSaveFile.FileName = "Untitled";//The default filename in the SaveFileDialog window 
     sfdSaveFile.Filter = "Rich Text Files (*.rtf)|*.rtf|Text Document (*.txt)|*.txt";//The supported file extensions when saving 
     if (sfdSaveFile.ShowDialog() == DialogResult.OK)//If the condition is correct, run the lines of code 
      try//try to run the code 
      { 
       filepath = sfdSaveFile.FileName;//get the filepath of the file once it is saved 
       SaveFile();//Calls the SaveFile object 
       this.Text = string.Format("{0} - Basic Word Processor", Path.GetFileName(sfdSaveFile.FileName));//Set the form name 
       lastsave.Text = "Last Saved: " + DateTime.Now.ToString();//Writes the text to the lastsave.Text label, followed by the current date and time 
       richTextBoxPrintCtrl1.Modified = false; 
       return; 
      } 
      catch (Exception exc)//Catches any errors 
      { 
       MessageBox.Show("An error occured whilst saving. " + exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      } 
       else if (sfdSaveFile.ShowDialog() == DialogResult.Cancel) 
       { 
        return; 
       } 

     else if (sfdSaveFile.ShowDialog() == DialogResult.Cancel)//If the condition is true, run the line of code 
     { 
      return; 
     } 

사람이 나를이 발생하는 이유를 확인하는 데 도움 수 있다면

를 내가 정말 감사하겠습니다 .. 다음과 같이

내 프로그램에 저장하는 코드는

--EDIT--

나는 g 사용자가 않는 경우 언급하는 것을 잊었다 o SaveFileDialog는 다른 SaveFileDialog를 열지 않습니다. 문제를 일으키는 SaveFileDialog를 취소하는 것과 관련이 있습니다.

답변

7

sfdSaveFile.ShowDialog()은 파일 대화 상자를 엽니 다. 처음으로 DialogResult.OK이 아닌 경우 else 절로 이동하여 다시 호출됩니다. ShowDialog의 결과를 저장하고 그 내용을 확인하고 매번 호출하지 마십시오./다른 경우 이런 종류의 이렇게 사용하기 위해서는

: 내가 바보 같은 소리 경우

DialogResult dialogResult = sfdSaveFile.ShowDialog(); 
if (dialogResult == DialogResult.OK) 
{ 
} 
else if (dialogResult == DialogResult.Cancel) 
{ 
} 
+0

미안 ...하지만 내가 그걸 어떻게 할 것인가? – Toby

+1

관련 코드를 포함하도록 수정되었습니다. –

+0

그래, 끝났어. 당신의 도움을 주셔서 감사합니다. :) – Toby

관련 문제