16

I use the following to display an Open File dialog:

OpenFileDialog fdlg = new OpenFileDialog();
fdlg.FileName = Properties.Settings.Default.Last_competition_file;
fdlg.Filter = "FS database files (*.fsdb)|*.fsdb|All files (*.*)|*.*";
fdlg.FilterIndex = 0;
if (fdlg.ShowDialog(this) == DialogResult.Cancel) return false;

(Properties.Settings.Default.Last_competition_file contains the whole path to the last file)

Problem: For a file name "c:\data\nationals_2014.fsdb", the File name field only shows "ionals_2014.fsdb".

When clicking into the File name field, and moving the cursor to the left, the remainder of the file name & path re-appears. But I'm looking for a way to make the whole file name visible from the beginning.

Note that this is not a length issue. I also tried setting path and file name separately (through OpenFileDialog.InitialDirectory), but even then only the tail end of the (now much shorter) file name was displayed.

Any ideas how to get the Open File dialog to show the full pre-populated file name from the beginning?

JoergEwald
  • 379
  • 3
  • 10

4 Answers4

6

Caveat: This is a Kludge, not a real answer.

  OpenFileDialog fdlg = new OpenFileDialog();
  fdlg.FileName = Properties.Settings.Default.Last_competition_file;
  fdlg.Filter = "FS database files (*.fsdb)|*.fsdb|All files (*.*)|*.*";
  fdlg.FilterIndex = 0;
  fdlg.ShowHelp = true;
  fdlg.HelpRequest +=  new System.EventHandler(HelpRequested); ;
  if (fdlg.ShowDialog(this) == DialogResult.Cancel) return false;

private void HelpRequested(object sender, EventArgs e)
{
    MessageBox.Show(".. is no Help", "There..");
}

The style of the Dialog reverts to an older incarnation.

Shrug. Some workarounds make me wonder about many things..

TaW
  • 53,122
  • 8
  • 69
  • 111
4

I got the same thing on windows 10 with an open file dialog set up like this:

var dialog = new OpenFileDialog{
  Filter = "excel files (*.xlsx)|*.xlsx",
  InitialDirectory = @"c:\temp",
  FileName = @"MyFileNameExceeds14Characters.xlsx"
};
dialog.ShowDialog();

Work-arounds:

  1. Set AutoUpgradeEnabled = false to revert to an older dialog style. But then you're stuck with the older UI.
  2. Make sure the file name is under 14 characters long. If you don't have direct control of the file name, then run it through Path.GetFileNameWithoutExtension() to slim it down as much as possible.
  3. Use a SaveFileDialog instead, which does not have this issue.
jcox
  • 786
  • 8
  • 11
-1

Inserting code:

SendKeys.Send("{HOME}");

before the line :

if (fdlg.ShowDialog(this) == DialogResult.Cancel) return false;

does the job.

toha
  • 5,095
  • 4
  • 40
  • 52
Petardo
  • 11
  • 1
-2

Found a good answer on another thread: c# Sending keyboard commands to another window / process This does a good job of fixing the file name display.

I use a timer anyway in order to make sure that dialog is centered on active screen. Once the dialog is displayed:

IntPtr handle = FindWindowByCaption(IntPtr.Zero, dialogTitle));
SetForegroundWindow(handle);
SendKeys.SendWait("{HOME}");
SendKeys.Flush();
Community
  • 1
  • 1
Mick Bruno
  • 1,373
  • 15
  • 13