我在OpenFileDialog中设置默认文件名为answer_XXXXXX.csv。但是显示是这样的。默认名称“answer_XXXXXX.csv”未完整显示。
然后我点击文件名组合框。它显示准确。
我该如何解决?
对此有一个小的解决方法。打电话之前先下线
ShowDialog()
.
openfiledialog.ShowHelp = true;
例子:
OpenFileDialog openfiledialog = new OpenFileDialog();
openfiledialog.ShowHelp = true;
openfiledialog.FileName = "answer_XXXXXXX.csv";
openfiledialog.ShowDialog();
更多:
这是另一个解决方法,您可以使用更复杂的 Win32 api 函数来访问文件名组合框并做任何您想做的事情,但是这个解决方法使用
SendKeys
,我现在没有时间深入研究 Win32 API 函数:
public Form1()
{
InitializeComponent();
t.Interval = 100;
t.Tick += (s, e) =>
{
SendKeys.Send("{HOME}+{END}");
t.Stop();
};
}
Timer t = new Timer();
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.FileName = "I love .NET so much";
t.Start();
open.ShowDialog();
}
我无法解释这个错误,但有一些解决方法,上面的就是其中之一。
King King 的回答似乎是最好的解决方案,我使用的基本相同但可能更简单一些(显然我没有得到投票或直接在他的帖子上发表评论的声誉):
OpenFileDialog oFileD = new OpenFileDialog();
oFileD.InitialDirectory = initialDir;
oFileD.FileName = fileName;
if (oFileD.FileName != "")
{
Timer t = new Timer();
t.Interval = 100;
t.Tick += (s, e) =>
{
SendKeys.Send("{HOME}+{END}");
t.Stop();
};
t.Start();
}
if (oFileD.ShowDialog() == DialogResult.OK) {
...
}
我不敢相信没有解决办法;最初,它似乎陷入了 2 个 Microsoft 组之间的裂缝,并且接受了使用旧的丑陋对话框的解决方法。也许到那时支持已从 WinForms 切换,但 WinForms 对我来说效果更好。我的解决方法很复杂,但我可能会保留它。
我不喜欢使用 Sendkeys,因为键是排队的,可能会被不同的窗口拾取。使用假定对话框已准备就绪的计时很麻烦。该解决方案试图解决这些问题,并在高负载 PC 上运行时表现出一定的稳健性。它可能会做得更优雅。
一些导入(有没有更好的方法?):
public static class Util1
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int GetFocus();
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, int wMsg, int wParam, int lParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GetClassName(int hWnd, StringBuilder lpClassName, int nMaxCount);
}
public static class Util1S
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, int wMsg, int wParam, string lpData);
}
public static class Util1SB
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, int wMsg, int wParam, System.Text.StringBuilder lpData);
}
代码使用 Win32 api 调用来验证包含文件名的实际编辑控件的句柄,并在滚动几乎正确到字符的一小部分(m 被截断为 n)的情况下替换其中的文本。短时间内使用重试来尝试修复。我和我自己的网上建议的其他尝试被注释掉了:
//SaveFileDialog ofd;
//using (ofd = new SaveFileDialog()
OpenFileDialog ofd;
using (ofd = new OpenFileDialog()
{
//AutoUpgradeEnabled = false,
FileName = prevfile,
InitialDirectory = prevdir,
AddExtension = true,
CheckFileExists = true,
CheckPathExists = true,
Title = title,
DefaultExt = defext,
Filter = filter
})
{
string other = Directory.GetCurrentDirectory();
if (!string.IsNullOrWhiteSpace(prevfile)) // workaround for previous file name not left-justified in edit field
{
//ofd.Site.GetService().OnGotFocus()
//SendKeys.Send("{HOME}");
var t = new System.Windows.Forms.Timer();
int cnt = 0; // retry counter
t.Interval = 100;
t.Tick += (s, e) =>
{
++cnt;
int hwnd = Util1.GetFocus();
G.Log("hwnd " + hwnd);
if (hwnd != 0)
{
var buf = new System.Text.StringBuilder(256);
Util1.GetClassName(hwnd, buf, 128);
G.Log("class " + buf);
if (buf.ToString().Equals("Edit"))
{
var buf2 = new System.Text.StringBuilder(1000);
Util1SB.SendMessage(hwnd, 13, 500, buf2); // hwnd WM_GETTEXT limit dest
G.Log("text " + buf2);
if (!string.IsNullOrWhiteSpace(buf2.ToString()) && buf2.ToString().Equals(prevfile))
{
Util1S.SendMessage(hwnd, 12, 0, ""); // hwnd WM_SETTEXT 0 "str"
Util1SB.SendMessage(hwnd, 12, 0, buf2); // hwnd WM_SETTEXT 0 "str"
//Util1.SendMessage(hwnd, 0x00B6, 1000, 0); // hwnd EM_LINESCROLL 1000 0 : scroll right a whole lot
//Util1.SendMessage(hwnd, 0x00B6, -1000, 0); // hwnd EM_LINESCROLL -1000 0 : scroll left a whole lot
//Util1.SendMessage(hwnd, 0x00B1, 0, 1); // hwnd EM_SETSEL 0 1 : select first char
Util1.SendMessage(hwnd, 0x00B1, 0, -1); // hwnd EM_SETSEL 0 -1 : select all text
cnt = 1000;
}
}
}
//SendKeys.Send("{HOME}+{END}");
if (cnt > 5)
t.Stop();
};
t.Start();
}
if (ofd.ShowDialog() == DialogResult.OK)
{