在c#中的自定义对话框上读取事件时出错

时间:2014-06-09 08:30:58

标签: c# winforms

我制作了一个自定义对话框,其中,用户可以选择多个图像路径。

enter image description here

Change”,“Done”按钮和“ldpifilepath”等标签最初是不可见的。

很快,当点击“LDPI”之类的按钮时,会打开OpenFileDialog来选择图像。选择图像后,相应的“Change”按钮会再次更改图像,同时也会显示相应的FilePath标签。这就是功能。

DialogResultCancel按钮的Done属性已设置为“Cancel”和“OK”。

//filename is null when no image is chosen, otherwise, when the button is clicked,
//we obtain an image preview and Image Details
private void ldpiUpdate ( string filename )
        {
            if ( null == filename )
            {

                OpenFileDialog ofd = new OpenFileDialog ();

                ofd.Filter = "Image Files|*.BMP;*.GIF;*.JPG;*.JPEG;*.PNG|All files (*.*)|*.*";

                if ( ofd.ShowDialog () == DialogResult.OK )
                {
                    ldpiImagePath = ofd.FileName;
                }

                else return;
            }

            ldpiFilePath.Text = ldpiImagePath;
            ldpiFilePath.Visible = true;
            ldpi.Enabled = true;
            fileName.Text = System.IO.Path.GetFileName (ldpiImagePath);
            fileName.Enabled = true;
            fileName.Visible = true;
            var img = Image.FromFile (ldpiImagePath);
            imageRes.Text = img.Height + "x" + img.Width;
            imageRes.Enabled = true;
            imageRes.Visible = true;
            Size size = new Size (imagePreview.Height, imagePreview.Width);
            if ( size.Height < img.Size.Height || size.Width < img.Size.Width )
                img = ( Image )( new Bitmap (img, size) );
            imagePreview.BackgroundImage = img;
            ldpiChange.Enabled = true;
            ldpiChange.Visible = true;
            done.Enabled = true;
            img = null;

        }

现在,我正尝试以下列方式在我的主项目中使用此表单。

        BrowseForAssets a = new BrowseForAssets (); //this is the name of the custom form
        DialogResult k = a.ShowDialog ();

        if ( k== DialogResult.OK )
        {

            MessageBox.Show ("OK");

        }

        if ( k == DialogResult.Cancel )
        {
            MessageBox.Show ("Cancel");

        }

问题:

当我点击LDPI并打开openfiledialog(ofd)框时。我选择了一张图片并点击了“OK”,我得到MessageBox.Show ("Cancel")输出,主BrowseForAssets框也关闭,而我没有点击“Cancel”或“Done”按钮。

我也不明白k如何设置为DialogResult.Cancel。我真的很感激一些帮助。

0 个答案:

没有答案