我一直在手动设置每个按钮的事件,但我如何概括这一点?
我想我可以覆盖 ButtonBase,但我该怎么做呢?我是一个相对较新的 C# 程序员,我需要这个,因为我正在模拟一个真实的设备,所以我需要改变光标,以便用户知道他们可以点击哪里。
最佳答案
如果所有按钮都在表单上(没有嵌套容器),那么您可以在 Form_Load()
foreach(Button b in this.Controls.OfType<Button>())
{
b.MouseEnter += (s, e) => b.Cursor = Cursors.Hand;
b.MouseLeave += (s, e) => b.Cursor = Cursors.Arrow;
}
如果你不想触摸表单上的每个按钮,你可以做一个简单的集合并迭代它们
Button[] buttons = new[] {button1, button2, button3};
foreach (Button b in buttons)
{
b.MouseEnter += (s, e) => b.Cursor = Cursors.Hand;
b.MouseLeave += (s, e) => b.Cursor = Cursors.Arrow;
}
关于c# - 如何在鼠标移动时为 C# 中的所有按钮设置手形光标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7155569/