摘要:System.Windows.Forms.Form继承于System.Windows.Forms.ContainerControl也就继承了public virtual Control ActiveControl {get; set;}你经常使用ActiveControl么?
假设在当前Form上有2个TextBox接受用户输入的密码。2次输入的密码必须一致,否则我们弹出一个MessageBox告诉用户密码必须重新输入。我们希望当用户点击OK按钮后,MessageBox消失而光标位置回到第一个TextBox上。如何做到呢?
最常用的代码使用Control.Focus:
if (this.textBox1.Text != this.textBox2.Text)
{
MessageBox.Show(this, "Make sure two passwords are the same", "Demo", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.textBox1.Focus();
}
但是我们还可以使用ContainerControl.ActiveControl:
this.ActiveControl = this.textBox1;...[
阅读全文]