FROM#/. NET: TextBox is not "focused" after starting the process

I had a problem after opening notepad after clicking the "btnSearch" button.

The idea is that after I clicked the “btnSearch” button, the text field “txtSearch” should be “focused” even after the process was initiated / opened outside the main window.

Here is my code:

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

Any suggestions?

0
source share
5 answers

Below is the code you need. This can be done using gateway 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(); 
0
source

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 something else

 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; } 
+3
source

You tried

 txtSearch.Select () txtSearch.Focus() 

?
Is your textbox inside a groupbox?

0
source

Applications cannot “steal” focus from other applications (with Windows XP), the closest to them may be the flashing taskbar, which is possible through P / Invoke:

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

Then give it the shape of Handle

0
source

Take a look at the TabIndex property. Use a value of 0 for the control you need to focus on when launching the application.

0
source

Source: https://habr.com/ru/post/1312491/


All Articles