12

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?

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
Huy Duong Tu
  • 7,798
  • 5
  • 25
  • 46
  • Does this happen with other defaults too? For example, how does `foobar_FOOBAR.csv` show up? –  Jun 18 '13 at 08:17
  • 3
    I think this is a bug with the system.. https://connect.microsoft.com/VisualStudio/feedback/details/525070/openfiledialog-show-part-of-file-name-in-win7# – Bolu Jun 18 '13 at 08:21
  • That may be the bug, but i did not find any issue with my application. I have tried the name as answer_XXXXXX123456.csv, it has not given any issue. But yes if you provide longer name then to see the file name you have to scroll to the left side to see the complete filename. :) – Hitesh Jun 18 '13 at 08:34
  • 1
    @HiteshMistry Perhaps you're using Windows 8 or forcing classic XP style, as described in the workarounds. – user247702 Jun 18 '13 at 08:36
  • No, its windows 7.. And i am using default windows style. No modification is done from my side. :) – Hitesh Jun 18 '13 at 10:27

4 Answers4

10

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

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
Pasan Eeriyagama
  • 277
  • 1
  • 4
  • 15
6

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.

King King
  • 61,710
  • 16
  • 105
  • 130
4

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) {
    ...
}
Binni
  • 41
  • 2
0

I can't belive there isn't a fix for this; Initially, it seems it fell into cracks between 2 Microsoft groups and the workaround of using the old ugly dialog box was accepted. Maybe by that time support was switching from WinForms, but WinForms works better for me. My workaround is complicated, but I might keep it.

I did not like using Sendkeys since the keys are queued and might be picked up by a different window. Using timing that assumes the dialog box is ready was bothersome. This solution attempts to address those concerns and exhibits some robustness when running on heavily loaded PCs. It might be done more elegantly.

Some imports (is there a better way?):

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

The code uses Win32 api calls to validate a handle to the actual edit control containing the file name and replaces the text in it for a case where scrolling was almost correct to a fraction of character (m was truncated to n). Retries are used for a short while to attempt the fix. My other attempts from online suggestions and my own are commented out:

            //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)
                {
Codemeister
  • 107
  • 1
  • 6