首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何编写OpenFileDialog框中取消按钮的代码

如何编写OpenFileDialog框中取消按钮的代码
EN

Stack Overflow用户
提问于 2014-10-21 01:14:47
回答 2查看 13.9K关注 0票数 2

每当我取消OpenFileDialog盒子的时候。它给出了一个错误路径为空。我的OpenFileDialog box

有没有什么方法可以为OpenFileDialog的取消按钮和关闭按钮编写代码

我的代码:

代码语言:javascript
复制
       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;
        }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-10-21 01:17:46

为什么不呢:

else语句避免了重复的逻辑,并且返回一个空字符串,调用方法可以检查该字符串

代码语言:javascript
复制
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;
}
票数 3
EN

Stack Overflow用户

发布于 2014-10-21 01:18:07

代码语言:javascript
复制
if (ofd.ShowDialog() != DialogResult.OK)
    return "";

它不工作,因为你应该返回字符串!如果你愿意,你可以返回null,也可以检查null!

在你的方法中:

代码语言:javascript
复制
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
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26471230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档