我想创建一个保存文件对话框,当您按下自定义功能区按钮时打开该对话框并将文件名和初始目录属性设置为特定值。现在我在一个表单中创建该对话框。在设计器中,它显示在底部 Pane 中,因为它不是直接绘制在表单上的项目,或者我在这个问题中被告知:Visual Studio Designer Bottom Pane
我的问题是:我应该删除表单并自行创建 saveFileDialog 吗?或者,如果我保留表单,当我调用 form.ShowDialog() 时,它会显示我在设计器中添加的 saveFileDialog 吗?还是我需要调用 saveFileDialog.ShowDialog()? (我不能在课外做)。我不确定实现自定义 saveFileDialog 的正确方法是什么。我曾经被告知要创建一个表单,现在看来我不应该使用表单。
最佳答案
我认为您绝对不需要通过表单使用保存对话框;
在你的功能区代码中添加:
置顶
using System.Windows.Forms;
然后这个函数
/// <summary>
/// Return selected file from winform save dialog
/// </summary>
/// <param name="fn">The filename</param>
/// <param name="id">The initial directory</param>
/// <returns>Specified filename</returns>
private string SaveFileTo(string id, string fn)
{
SaveFileDialog fd = new SaveFileDialog();
//fd.OverwritePrompt = false;
fd.AddExtension = true;
fd.ValidateNames = true;
fd.FileName = fn;
fd.InitialDirectory = id;
//fd.Filter = "PDF files|*.pdf|Other ext like music|*.mp3;*.wma|All File|*.*";
fd.Filter = "PDF files|*.pdf";
if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
return fd.FileName;
return "";
}
并且,在你的功能区按钮的方法中
string s = SaveFileTo(@"C:\Users\Public\Documents", "MyAtt");
//Do something with s
MessageBox.Show(s);
正如您在函数中看到的,有两行被注释,您可以取消注释或修改它们,就像您希望您的应用正常工作一样
关于c# - Outlook VSTO 插件中的保存文件对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30284130/