0

OpenFileDialog 상자를 취소 할 때가 언제입니까? 그것의 오류 경로를주는 것은 비어 있습니다. 내 OpenFileDialog를 상자 https://imageshack.com/i/iqQYnD8pjOpenFileDialog 상자에서 취소 단추를 코딩하는 방법

내가 OpenFileDialog를

의 취소 버튼을 닫기 버튼에 대한 내 코드

코딩 할 수있는 방법이있다 :

private void button4_Click(object sender, EventArgs e) 
    { 
     string s = image_print() + Print_image(); 
     PrintFactory.sendTextToLPT1(s);/sending to serial port 
    } 

    private string image_print() 
    { 
     OpenFileDialog ofd = new OpenFileDialog(); 
     string path = ""; 
     string full_path = ""; 
     string filename_noext = ""; 
     ofd.InitialDirectory = @"C:\ZTOOLS\FONTS"; 
     ofd.Filter = "GRF files (*.grf)|*.grf"; 
     ofd.FilterIndex = 2; 
     ofd.RestoreDirectory = true; 

     if (ofd.ShowDialog() == DialogResult.OK) 
     { 
      filename_noext = System.IO.Path.GetFileName(ofd.FileName); 
      path = Path.GetFullPath(ofd.FileName); 
      img_path.Text = filename_noext; 
      //MessageBox.Show(filename_noext, "Filename"); 
      // MessageBox.Show(full_path, "path"); 
      //move file from location to debug 
      string replacepath = @"E:\Debug"; 
      string fileName = System.IO.Path.GetFileName(path); 
      string newpath = System.IO.Path.Combine(replacepath, fileName); 
      if (!System.IO.File.Exists(filename_noext)) 
       System.IO.File.Copy(path, newpath); 

     } 

     //tried using below codes but not taking return statement. saying "An object of a type convertible to 'string' is required" 

     if (ofd.ShowDialog() != DialogResult.OK) 
      return;//---->> here its not taking return 

      //when ever i press the cancel or close button it is going to below line. How to stop this 
     StreamReader test2 = new StreamReader(img_path.Text); 
     string s = test2.ReadToEnd(); 
     return s; 
    } 


    private string Print_image() 
    { 
     //some codes that returns string value in s 
     return s; 
    } 

답변

1

왜 :

else 문을 피합니다은 duzplicated 논리를 사용하면 호출 할 수있는 빈 문자열을 반환합니다.

if (ofd.ShowDialog() == DialogResult.OK) 
    { 
     filename_noext = System.IO.Path.GetFileName(ofd.FileName); 
     path = Path.GetFullPath(ofd.FileName); 
     img_path.Text = filename_noext; 
     //MessageBox.Show(filename_noext, "Filename"); 
     // MessageBox.Show(full_path, "path"); 
     //move file from location to debug 
     string replacepath = @"E:\Debug"; 
     string fileName = System.IO.Path.GetFileName(path); 
     string newpath = System.IO.Path.Combine(replacepath, fileName); 
     if (!System.IO.File.Exists(filename_noext)) 
      System.IO.File.Copy(path, newpath); 

    } 
else 
{ 
return String.Empty; 
} 
+0

, 내가 그것을 선으로가는 취소를 누르면 '에서는 StreamReader TEST2 = 새에서는 StreamReader (img_path.Text) ; ' 내가 어떤 파일을 선택하지 않았다면 그것을 막는 방법 –

+0

@Shell 어떤 도움 /// –

1
if (ofd.ShowDialog() != DialogResult.OK) 
    return ""; 

문자열을 반환해야하기 때문에 작동하지 않습니다! 원하는 경우 null을 반환하고 null도 확인할 수 있습니다.

그리고 당신의 방법에

:

이 코드는 어떤 수단 무슨 일이 일어나고 ... 완벽하지만 지금 owrking됩니다
private void button4_Click(object sender, EventArgs e) 
{ 
    if(image_print() == "") 
    { 
     return; 
     //You can write a message here to tell the user something if you want. 
    } 

    string s = image_print() + Print_image(); 
    PrintFactory.sendTextToLPT1(s);/sending to serial port 
} 
+0

내가 위의 코드를주었습니다 .. 이제 OpenFileDialog는 두 번 올 것입니다. 내가 취소 할 때 OpenFileDialog가 다시 열립니다. –

+0

@StacyKebler는 디버그를 사용하여 이유를 확인합니다 ... – mybirthname

+0

왜냐하면 .. 버튼 4_Click에서'image_print()'가 있고'.string s = image_print()'도 있습니다. 그래서 뭐 할까 –