简体   繁体   中英

How can I get OpenFileDialog to focus on the “files box”, and not the “name box”, when opened?

When the dialog box opens the focus is on the name box. I have to tab like 10 times to get to the files, and arrow down to the one I want.

I am using keyboard not mouse.

private void LoadFileNow()
{
   OpenFileDialog Open = new OpenFileDialog();
   Open.Filter = "GCode Files|*.ngc";
   Open.Title = "Select a GCode File";
   // Show the Dialog.
   // If the user clicked OK in the dialog and
   // a .ngc file was selected, open it.
   if (Open.ShowDialog() == DialogResult.OK)
   {
      // Assign the GCode FileName to Var.
      GV.GCodeFile = Open.FileName;
      string f = "";
      f = String.Format("{0}{1}", "GCode File: ", Open.FileName);        //Change Ver. 4.0.1.1
      label6.Text = f;
      TextBox.LoadFile(Open.FileName,  RichTextBoxStreamType.PlainText);
   }
   Thread.Sleep(500); //Wait a moment while file loads.
}

Ok so if I get this right you want to focus the file selection window when your dialog box opens? Oh you are gonna love this one haha. Might be other ways but this works :-p. Only tested in Windows 7. You might have to change the control search.

Lets p/invoke some stuff

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lclassName, string windowTitle);

[DllImport("user32.dll")]
static extern IntPtr SetFocus(IntPtr hWnd);

[DllImport("user32.dll")]
internal static extern IntPtr SetForegroundWindow(IntPtr hWnd);

method that will let us get child controls of parent

private static List<IntPtr> GetAllChildrenWindowHandles(IntPtr hParent)
{
    List<IntPtr> result = new List<IntPtr>();
    IntPtr prevChild = IntPtr.Zero;
    IntPtr currChild = IntPtr.Zero;
    while (true)
    {
        currChild = FindWindowEx(hParent, prevChild, null, null);
        if (currChild == IntPtr.Zero)
        {
            break;
        }
        result.Add(currChild);
        prevChild = currChild;
    }
    return result;
}

here is the method we will run in a thread right before you open your dialog

private void FocusFileDialog()
{
    bool windowFound = false;
    while (!windowFound)
    {
        IntPtr od = FindWindow(null, "Select a GCode File");

        //found main dialog
        if (od != IntPtr.Zero)
        {
            IntPtr od1 = FindWindowEx(od, IntPtr.Zero, "DUIViewWndClassName", null);

            if (od1 != IntPtr.Zero)
            {
                IntPtr od2 = FindWindowEx(od1, IntPtr.Zero, "DirectUIHWND", null);

                if (od2 != IntPtr.Zero)
                {
                    List<IntPtr> results = GetAllChildrenWindowHandles(od2);

                    results.ForEach(hwd =>
                    {
                        IntPtr od3 = FindWindowEx(hwd, IntPtr.Zero, "SHELLDLL_DefView", null);

                        if (od3 != IntPtr.Zero)
                        {
                            IntPtr found = FindWindowEx(od3, IntPtr.Zero, "DirectUIHWND", null);

                            if (found != IntPtr.Zero)
                            {
                                SetForegroundWindow(found);
                                SetFocus(found);
                                windowFound = true;
                            }
                        }
                    });
                }
            }
        }
    }
}

Here is how you will open your dialog

new Thread(FocusFileDialog).Start();
using (OpenFileDialog Open = new OpenFileDialog())
{
    //your stuff here
}

Attached is from Spy++ might help you understand. What is selected is what we need the pointer for.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM