Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default name with OpenFileDialog C#?

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?

like image 932
Huy Duong Tu Avatar asked Jun 18 '13 08:06

Huy Duong Tu


People also ask

What is the OpenFileDialog in C#?

The OpenFileDialog component allows users to browse the folders of their computer or any computer on the network and select one or more files to open. The dialog box returns the path and name of the file the user selected in the dialog box. The FileName property can be set prior to showing the dialog box.

What is OpenFileDialog?

OpenFileDialog component opens the Windows dialog box for browsing and selecting files. To open and read the selected files, you can use the OpenFileDialog. OpenFile method, or create an instance of the System.

What is initial directory?

The InitialDirectory property is typically set using one of the following sources: A path that was previously used in the program, perhaps retained from the last directory or file operation. A path read from a persistent source, such as an application setting, a Registry or a string resource in the application.

Which property of OpenFileDialog is set to add extension to file name if the user does not supply any extension?

From FileDialog. AddExtension Property: "Gets or sets a value indicating whether the dialog box automatically adds an extension to a file name if the user omits the extension."


2 Answers

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) {
    ...
}
like image 172
Binni Avatar answered Oct 15 '22 08:10

Binni


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.

like image 21
King King Avatar answered Oct 15 '22 08:10

King King