我有一个具有以下构造函数的应用程序:
public BankApp()
{
InitializeComponente();
Cursor.Current = Cursors.WaitCursor;
// some coding and setting up
Cursor.Current = Cursors.Default;
}但是,由于某些原因,光标图标没有设置为WaitCursor,而如果我执行任何其他操作,它将被正确地替换。
我需要这样做,因为在中,我想防止用户在调用并完成构造函数中的所有例程之前执行任何操作。
任何帮助都是非常感谢的!
发布于 2019-06-04 00:01:36
请注意,Cursor是表单的属性。当鼠标移动到此窗体上时,将显示选定的光标。但由于窗体在构造函数运行时仍不可见,因此无法显示等待光标。
考虑将长时间运行的内容转移到Shown事件处理程序:
private void Form1_Shown(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
try {
Thread.Sleep(2000); // Do long running stuff here
} finally {
Cursor.Current = Cursors.Default;
}
}相反,您也可以在打开这个新表单的表单中设置等待光标。
Cursor.Current = Cursors.WaitCursor;
try {
var frm = new BankApp(); // Constructor is running here.
frm.Show();
} finally {
Cursor.Current = Cursors.Default;
}如果窗体不是主窗体,则可以使用以下代码为所有打开的窗体设置等待光标。这也应该在构造函数中起作用:
Application.UseWaitCursor = true;
try {
Thread.Sleep(2000); // Do long running stuff here
} finally {
Application.UseWaitCursor = false;
}try-finally确保如果出现错误,默认光标也会被重置。
https://stackoverflow.com/questions/56430643
复制相似问题