C# 如何让OpenFileDialog专注于;文件框“;,而不是",;名称框“;,什么时候开门?

C# 如何让OpenFileDialog专注于;文件框“;,而不是",;名称框“;,什么时候开门?,c#,winforms,C#,Winforms,对话框打开时,焦点在“名称”框上。我必须用tab键打10次才能找到文件,然后用箭头指向我想要的文件 我用的是键盘而不是鼠标 private void LoadFileNow() { OpenFileDialog Open = new OpenFileDialog(); Open.Filter = "GCode Files|*.ngc"; Open.Title = "Select a GCode File"; // Show the Dialog. // If the

对话框打开时,焦点在“名称”框上。我必须用tab键打10次才能找到文件,然后用箭头指向我想要的文件

我用的是键盘而不是鼠标

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.
}

好的,如果我做对了,你想在对话框打开时聚焦文件选择窗口吗?哦,你会喜欢这个的,哈哈。可能是其他方式,但这是有效的:-p。仅在Windows7中测试。您可能必须更改控件搜索

让p/调用一些东西

[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);
方法,该方法将允许我们获取父控件的子控件

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;
}
附件来自Spy++可能有助于您理解。所选内容就是我们需要指针的目的


上次我试过(但已经有一段时间没有试过了),一个
Shift+Tab
组合将我带到了文件列表。这是一个仅供您个人使用的程序吗?因为如果不是的话,打开一个标准的打开文件对话框,它看起来就像Windows中几乎每个程序中的每个对话框,但行为却不同,这会给用户带来非常糟糕的体验。布赖恩,这太粗鲁了。我有一个需要100%通过键盘运行的系统。哈米迪先生,这只不过是倒抽。。。仍然需要点击几下。是的,这是一个标准的打开文件对话框。按住Shift+Tab键2次,然后按向下箭头和向上箭头键。只是选择第一个文件。@ JEFServ——当你认为你的问题显示很少的研究或努力时,这并不粗鲁。例如,试试谷歌的“OpenFileDialog设置焦点”。
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;
                            }
                        }
                    });
                }
            }
        }
    }
}
new Thread(FocusFileDialog).Start();
using (OpenFileDialog Open = new OpenFileDialog())
{
    //your stuff here
}