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