我决定创建一个登录表单并首次使用Form.AcceptButton
属性。我的Login
函数运行时,我遇到锁定控件的问题。
当我按Enter键进入登录状态时,表单会在光标变为WaitCursor
并且控件被锁定之前冻结。
当我点击登录按钮时,表单冻结 后,光标变为WaitCursor
并且控件被锁定(首选行为)
这是我的代码(简化):
private void btnLogin_Click(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
// foreach loop that sets Enabled = false on all controls on form
LockControls();
// this function tries to login to SOAP web service, sometimes it takes few seconds
Login();
this.Cursor = Cursors.Default;
// foreach loop that sets Enabled = true on all controls on form
UnlockControls();
}
答案 0 :(得分:1)
您使用的是什么版本的.Net Framework和Windows。我测试了以下代码,这两个案例与您喜欢的完全相同:
private void btnLogin_Click(object sender, EventArgs e)
{
Cursor = Cursors.WaitCursor;
foreach (var c in this.Controls.OfType<Control>())
c.Enabled = false;
Thread.Sleep(5 * 1000);
Cursor = Cursors.Default;
foreach (var c in this.Controls.OfType<Control>())
c.Enabled = true;
}
答案 1 :(得分:1)
在我的Win7,VS2012上,这也有效:
private void button1_Click(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
LockControls(true);
Login();
this.Cursor = Cursors.Default;
LockControls(false);
}
private void LockControls(bool mylock)
{
this.Enabled = !mylock;
}
private void Login()
{
Thread.Sleep(5 * 1000);
}
单击按钮或按下回车键时,控件被禁用时会出现等待光标。