C# 在c中读取自定义对话框上的事件时出错
我制作了一个自定义对话框,用户可以在其中选择多个图像路径 更改、完成按钮和标签(如ldifilePath)最初是不可见的 只要单击像LDPI这样的按钮之一,就会打开一个OpenFileDialog来选择图像。选择图像后,相应的更改按钮会再次显示以更改图像,并且相应的文件路径标签也会显示。这就是函数 取消和完成按钮的DialogResult属性已设置为取消和确定 问题是: 当我点击LDPI时,openfiledialogofd框打开。我选择一个图像,然后在ofd上单击OK,我会得到消息框。显示取消输出,并且主BrowseForAssets框也会关闭,而我没有单击Cancel或Done按钮C# 在c中读取自定义对话框上的事件时出错,c#,winforms,C#,Winforms,我制作了一个自定义对话框,用户可以在其中选择多个图像路径 更改、完成按钮和标签(如ldifilePath)最初是不可见的 只要单击像LDPI这样的按钮之一,就会打开一个OpenFileDialog来选择图像。选择图像后,相应的更改按钮会再次显示以更改图像,并且相应的文件路径标签也会显示。这就是函数 取消和完成按钮的DialogResult属性已设置为取消和确定 问题是: 当我点击LDPI时,openfiledialogofd框打开。我选择一个图像,然后在ofd上单击OK,我会得到消息框。显示取消
我也不明白k是如何设置为DialogResult.Cancel的。非常感谢您的帮助。检查按钮的DialogResult属性。可能它被设置为Cancel,所以事件被触发,代码被执行,但随后对话框关闭,结果是Cancel。除了Cancel和OK之外的每个按钮都应该将其属性DialogResult设置为None。任何其他属性都会导致资产的浏览退出ShowDialogy。非常感谢。这就是错误所在。我想我把LDPI的对话结果设置成了错误的取消。这种情况在我身上发生了很多次,以至于我失去了计数。罪魁祸首总是相同的复制/粘贴。
//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");
}