How to code for cancel button in OpenFileDialog box

Stacy Kebler picture Stacy Kebler · Oct 20, 2014 · Viewed 13.6k times · Source

Whenever I am canceling the OpenFileDialog box. It's giving an error path is empty. My OpenFileDialog box

Is there any way I can code for the cancel button and close button of OpenFileDialog

My codes:

       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 the 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;
        }

Answer

Christian Sauer picture Christian Sauer · Oct 20, 2014

Why not:

The else statement avoids duplicated logic and you return an empty string, which the 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 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;
}