C#/.NET:TextBox 未“聚焦”;进程启动后

发布于 2024-08-27 07:49:02 字数 339 浏览 5 评论 0原文

单击“btnSearch”按钮后打开记事本后遇到问题。

这个想法是,一旦我单击按钮“btnSearch”,即使在主窗口之外启动/打开进程后,文本框“txtSearch”也应该“聚焦”。

这是我的代码:

    private void btnSearch_Click(object sender, RoutedEventArgs e)
    {
        System.Diagnostics.Process.Start("notepad");
        txtSearch.Focus(); // not working
    }

有什么建议吗?

I am having a problem after opening the notepad once I click the button "btnSearch".

The idea is that once I clicked the button 'btnSearch', the textbox 'txtSearch' should be 'focused' even after a process was initiated/opened outside the main window.

Here's my code:

    private void btnSearch_Click(object sender, RoutedEventArgs e)
    {
        System.Diagnostics.Process.Start("notepad");
        txtSearch.Focus(); // not working
    }

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

月下凄凉 2024-09-03 07:49:02

在您的 Page_Load 事件中尝试

Control c= GetPostBackControl(this.Page);

if(c != null)
{
   if (c.Id == "btnSearch")
   {
       SetFocus(txtSearch);
   }

}

然后将其添加到您的页面或 BasePage 或其他内容

public static Control GetPostBackControl(Page page)
{
     Control control = null;
     string ctrlname = page.Request.Params.Get("__EVENTTARGET");
     if (ctrlname != null && ctrlname != String.Empty)
     {
          control = page.FindControl(ctrlname);

     }
     else
     {
          foreach (string ctl in page.Request.Form)
          {
               Control c = page.FindControl(ctl);
               if(c is System.Web.UI.WebControls.Button)
               {
                   control = c;
                   break;
               }
          }

     }
     return control;
}

In your Page_Load event try

Control c= GetPostBackControl(this.Page);

if(c != null)
{
   if (c.Id == "btnSearch")
   {
       SetFocus(txtSearch);
   }

}

Then add this to your page or BasePage or whatever

public static Control GetPostBackControl(Page page)
{
     Control control = null;
     string ctrlname = page.Request.Params.Get("__EVENTTARGET");
     if (ctrlname != null && ctrlname != String.Empty)
     {
          control = page.FindControl(ctrlname);

     }
     else
     {
          foreach (string ctl in page.Request.Form)
          {
               Control c = page.FindControl(ctl);
               if(c is System.Web.UI.WebControls.Button)
               {
                   control = c;
                   break;
               }
          }

     }
     return control;
}
水水月牙 2024-09-03 07:49:02

以下是您需要的代码。这可以通过互操作服务来完成

    private void setwind()
    {

        System.Diagnostics.Process.Start("notepad");

        System.Threading.Thread.Sleep(2000);  //  To give time for the notepad to open

        if (GetForegroundWindow() != this.Handle)
        {
            SetForegroundWindow(this.Handle);
        }
    }


    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool SetForegroundWindow(IntPtr hWnd);

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

The below is the code you would need. This could be done through interop services

    private void setwind()
    {

        System.Diagnostics.Process.Start("notepad");

        System.Threading.Thread.Sleep(2000);  //  To give time for the notepad to open

        if (GetForegroundWindow() != this.Handle)
        {
            SetForegroundWindow(this.Handle);
        }
    }


    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool SetForegroundWindow(IntPtr hWnd);

    [DllImport("user32.dll")]
    static extern IntPtr GetForegroundWindow();
违心° 2024-09-03 07:49:02

你尝试过吗

txtSearch.Select ()
txtSearch.Focus()


您的 TextBox 是否位于 GroupBox 中?

Have you tried

txtSearch.Select ()
txtSearch.Focus()

?
Is your TextBox within a GroupBox?

呆头 2024-09-03 07:49:02

应用程序无法从其他应用程序“窃取”焦点(自 Windows XP 起),它们所能实现的最接近的功能是闪烁任务栏,这可以通过 P/Invoke 实现:

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool FlashWindow(IntPtr handle, bool invert);

然后将表单的 Handle 传递给它

Applications cannot "steal" focus from other applications (since Windows XP), the closest they can achieve is flashing the taskbar, which is possible via P/Invoke:

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool FlashWindow(IntPtr handle, bool invert);

Then pass it the form's Handle

昔梦 2024-09-03 07:49:02

查看 TabIndex 属性。在启动应用程序时需要聚焦的控件上使用值 0。

Look at the TabIndex property. Use a value of 0 on the control you need focused when you start the application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文