-1

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;
      }
  }
Umid Kurbanov
  • 174
  • 1
  • 5

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
  • 8,712
  • 3
  • 29
  • 51
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
  • 2,135
  • 2
  • 11
  • 25