0

「btnSearch」ボタンをクリックしてメモ帳を開いた後、問題が発生しました。

アイデアは、ボタン「btnSearch」をクリックすると、プロセスがメインウィンドウの外で開始/開かれた後でも、テキストボックス「txtSearch」が「フォーカス」されるはずであるということです。

これが私のコードです:

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

助言がありますか?

4

5 に答える 5

4

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;
}
于 2010-04-01T07:46:45.303 に答える
0

TabIndexプロパティを見てください。アプリケーションの起動時にフォーカスする必要のあるコントロールに値0を使用します。

于 2012-08-08T17:07:44.887 に答える
0

アプリケーションは他のアプリケーションからフォーカスを「盗む」ことはできません(Windows XP以降)。アプリケーションが達成できる最も近い方法は、タスクバーのフラッシュです。これは、P/Invokeを介して可能です。

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

次に、フォームに渡しますHandle

于 2010-04-01T15:14:55.677 に答える
0

やってみました

txtSearch.Select ()
txtSearch.Focus()


TextBoxはGroupBox内にありますか?

于 2010-04-01T10:41:14.160 に答える
0

以下はあなたが必要とするであろうコードです。これは、相互運用サービスを通じて行うことができます

    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();
于 2010-06-11T13:50:35.833 に答える