-2

I wanna change all buttons's cursor type to "Hand" in my form. But the code is not working. Can anyone help pls? ) This is code:

 foreach (Control control in this.Controls)
 {
      if (control is Button)
      {
           control.Cursor = Cursors.Hand;
      }
  }

2 Answers2

1

You are correctly checking if the control is of type Button. But you need to cast it to become a Button control (not a generic control) before changing the Cursor.

if (control is Button)
{
    (control as Button).Cursor = Cursors.Hand;
}
jason.kaisersmith
  • 7,807
  • 3
  • 26
  • 45
1

You can use pattern-matching grammar. Try this:

 foreach (Control control in this.Controls)
 {
      if (control is Button b)
      {
           b.Cursor = Cursors.Hand;
      }
  }
MyBug18
  • 1,943
  • 2
  • 8
  • 25