Default name with OpenFileDialog C#?

时光总嘲笑我的痴心妄想 提交于 2019-12-10 01:59:11

问题


I set the default file name is answer_XXXXXX.csv in OpenFileDialog. But it displays like this. The default name "answer_XXXXXX.csv" isn't displayed full.

Then I click on File name combo box. It displays exactly.

How can I fix it?


回答1:


There is a small workaround for this. Have below line before calling ShowDialog().

openfiledialog.ShowHelp = true;

Example:

OpenFileDialog openfiledialog = new OpenFileDialog();
openfiledialog.ShowHelp = true;
openfiledialog.FileName = "answer_XXXXXXX.csv";
openfiledialog.ShowDialog();

For more:

.NET 4.5 WPF RibbonWindow broken in VS2012




回答2:


Here is another work around, you can use more complex Win32 api functions to access the filename combobox and do whatever you want but this work around uses SendKeys, I have no time to dig into Win32 API functions at this time:

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();
}

I can't explain this bug but there are some work arounds and the above one is one of them.




回答3:


King King's answer seems to be the best solution, I've used basically the same but perhaps a bit simpler (apparently I haven't got the reputation to up-vote or comment directly on his post):

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) {
    ...
}


来源:https://stackoverflow.com/questions/17163784/default-name-with-openfiledialog-c

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