关闭第二个表单后,在主窗体上关注文本框 [英] Focus textbox on main form after closing a second form

查看:150
本文介绍了关闭第二个表单后,在主窗体上关注文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在关闭通过调用 ShowDialog() TextBox 元素

我试图在关闭和关闭事件中调用 MainForm.TextBox.Focus()但是这不会集中文本框。

我正在使用 System.Windows.Forms 在紧凑的框架中。



谢谢。

解决方案

窗体,使一个按钮(或另一个控制)返回一个 DialogResult 进入属性。当你想要关闭第二个窗体(即按下按钮后),使其返回一个特定的 DialogResult 。在你的主表单中,你可以这样做:
$ b $ pre $ $ $ $ $ c $ if(secondform.ShowDialog()== DialogResult.OK)
{
textBox.Focus();


调用 ShowDialog )会阻塞,直到它被关闭,所以你可以简单地做到:
$ b $ pre $第二个form.ShowDialog();
textbox.Focus()

然而,第一个例子是当你只想做在按下某个按钮或在第二个窗体上执行操作后,文本框会有焦点。


How do I focus a TextBox element on the main form after closing a second form which is opened by calling ShowDialog()?

I have tried to call MainForm.TextBox.Focus() in the closing and closed event of the second form, but this won't focus the textbox.

I am using System.Windows.Forms on the compact framework.

Thanks.

解决方案

From your second form, make a button (or another control) return a DialogResult by going into Properties. When you want the second form to close (ie after you press a button) make it return a specific DialogResult. In your main form you can do this:

if(secondform.ShowDialog() == DialogResult.OK)
{
    textBox.Focus();
    ...
}

Calling ShowDialog() will block until it is closed so you could simply do:

secondform.ShowDialog();
textbox.Focus()

However the first example is for when you only want to make the textbox have focus after you press a certain button or do an action on the second form.

这篇关于关闭第二个表单后,在主窗体上关注文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆