C#/.NET:TextBox不是';专注';在启动一个过程之后
单击“btnSearch”按钮打开记事本后,我遇到问题 这个想法是,一旦我点击按钮“btnSearch”,文本框“txtSearch”应该是“聚焦”的,即使在主窗口外启动/打开了一个进程 这是我的密码:C#/.NET:TextBox不是';专注';在启动一个过程之后,c#,.net,textbox,focus,lost-focus,C#,.net,Textbox,Focus,Lost Focus,单击“btnSearch”按钮打开记事本后,我遇到问题 这个想法是,一旦我点击按钮“btnSearch”,文本框“txtSearch”应该是“聚焦”的,即使在主窗口外启动/打开了一个进程 这是我的密码: private void btnSearch_Click(object sender, RoutedEventArgs e) { System.Diagnostics.Process.Start("notepad"); txtSearch.Focu
private void btnSearch_Click(object sender, RoutedEventArgs e)
{
System.Diagnostics.Process.Start("notepad");
txtSearch.Focus(); // not working
}
有什么建议吗?在您的页面中\u加载事件重试
Control c= GetPostBackControl(this.Page);
if(c != null)
{
if (c.Id == "btnSearch")
{
SetFocus(txtSearch);
}
}
然后将其添加到您的页面或基本页面或其他任何内容
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;
}
在您的页面中,请尝试加载事件
Control c= GetPostBackControl(this.Page);
if(c != null)
{
if (c.Id == "btnSearch")
{
SetFocus(txtSearch);
}
}
然后将其添加到您的页面或基本页面或其他任何内容
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;
}
你试过了吗
txtSearch.Select ()
txtSearch.Focus()
?您的文本框在GroupBox中吗?您试过了吗
txtSearch.Select ()
txtSearch.Focus()
?您的文本框是否在GroupBox中?应用程序无法从其他应用程序“窃取”焦点(因为Windows XP),它们可以实现的最接近的功能是闪烁任务栏,这可以通过p/Invoke实现:
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool FlashWindow(IntPtr handle, bool invert);
然后将表单的句柄传递给它
应用程序无法从其他应用程序“窃取”焦点(因为Windows XP),它们可以实现的最接近的功能是闪烁任务栏,这可以通过p/Invoke实现:
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool FlashWindow(IntPtr handle, bool invert);
然后将表单的
句柄传递给它
下面是您需要的代码。这可以通过互操作服务来实现
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();
下面是您需要的代码。这可以通过互操作服务来实现
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();
查看
选项卡index
属性。启动应用程序时,在需要聚焦的控件上使用值0。查看TabIndex
属性。启动应用程序时,在需要聚焦的控件上使用值0。txt.Search.Select也不起作用。我只想“聚焦”文本框(该条应闪烁)文本框不在groupboxtxt.Search.Select中也不起作用。我只想“聚焦”文本框(该栏应闪烁)文本框不在GroupBox内这是WPF还是WinForms?我同时使用了WPF和WinForms。这是个问题吗?我敢打赌你正在失去对正在打开的应用程序的关注。尝试在启动记事本后将其最小化。这可能会将焦点返回到应用程序,或者尝试将焦点切换到应用程序instead@Cen:是的@Veer:有没有办法将焦点切换到主窗口?这是WPF还是WinForms?我同时使用了WPF和WinForms。这是个问题吗?我敢打赌你正在失去对正在打开的应用程序的关注。尝试在启动记事本后将其最小化。这可能会将焦点返回到应用程序,或者尝试将焦点切换到应用程序instead@Cen:是的@Veer:有没有办法将焦点切换到主窗口?控件是在System.Windows.Forms.Control还是System.Windows.Controls.Control下?这个答案似乎是指ASP.Net,而不是WinForms(或WPF)是的,对不起,这是一个ASP.Net示例。他没有准确指定WinForms,如果相关,可以在WinForm上使用样本和simlar方法;我不知道为什么这被否决了。shrugwhat是这里的“\uuuu EVENTTARGET”。我在一个按钮上调用它,我将如何做?控件是在System.Windows.Forms.Control下还是在System.Windows.Controls.Control下?这个答案似乎是指ASP.Net,而不是WinForms(或WPF)是的,对不起,这是一个ASP.Net示例。他没有准确指定WinForms,如果相关,可以在WinForm上使用样本和simlar方法;我不知道为什么这被否决了。shrugwhat是这里的“\uuuu EVENTTARGET”。我按按钮打电话,我该怎么做?