c# - Closing browse dialog causes form to close

岁酱吖の 提交于 2019-12-22 15:39:04

问题


I have a form which is displayed through: ShowDialog(). The form doesn't have CancelButton specified.

When I open a BrowseDialog from the form and then close the BrowseDialog, the form is also closed. How can I prevent this from happening?

When the "browse"-button is clicked:

browseDialog.SelectedPath = projectLocation.Text;
browseDialog.ShowDialog();

if (browseDialog.SelectedPath != "")
{
      projectLocation.Text = browseDialog.SelectedPath;
}

When the "cancel"-button of the form is clicked:

Close();

回答1:


I would guess that the button you use to show the BrowseDialog has its DialogResult set to something other than None.

If this is not the case, please post some code.




回答2:


in your onclosing event from your browser dialog, do a check on the sender arg to see which dialog is requesting the close and if it's not the browser dlg, set e.Cancel = true




回答3:


I stumbled upon this when I had a similar issue, ensure your parent form is not setting ITS dialog result, because once the event to call the modal dialog is done, the parent form will close if it's set to something other than none.



来源:https://stackoverflow.com/questions/1998817/c-sharp-closing-browse-dialog-causes-form-to-close

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!