c# - How to code for cancel button in OpenFileDialog box -
c# - How to code for cancel button in OpenFileDialog box -
when ever canceling openfiledialog box. giving error path empty. openfiledialog box https://imageshack.com/i/iqqynd8pj
is there way can code cancel button , close button of openfiledialog
my codes:
private void button4_click(object sender, eventargs e) { string s = image_print() + print_image(); printfactory.sendtexttolpt1(s); / sending 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 location 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 not taking homecoming statement. saying "an object of type convertible 'string' required" if (ofd.showdialog() != dialogresult.ok) return;//---->> here not taking homecoming //when ever press cancel or close button going below line. how stop streamreader test2 = new streamreader(img_path.text); string s = test2.readtoend(); homecoming s; } private string print_image() { //some codes returns string value in s homecoming s; }
why not:
the else statement avoids duzplicated logic , homecoming empty string, calling method can check
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 location 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 { homecoming string.empty; } c# winforms visual-studio-2010 openfiledialog dialogresult
Comments
Post a Comment