Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I control the cursor over a DataGrid?

One way you can do this is to derive a class from DataGrid and override its WndProc method to handle the WM_SETCURSOR method. In your override, you can do hit testing to decide when you want to set the cursor, or when you want to call the base class and let it set the cursor. Here are sample projects (VB and C#) showing the technique.

Public Class MyDataGrid
  Inherits DataGrid

  Private Const WM_SETCURSOR As Integer = 32

  Protected Overrides Sub WndProc(ByRef m As Message)
    If m.Msg <> WM_SETCURSOR Then
      MyBase.WndProc(m)
    Else
      ' see if the cursor is in column 1 and rows 2, 3, or 4
      Dim pt As Point = PointToClient(Control.MousePosition)
      Dim hti As DataGrid.HitTestInfo = HitTest(pt.X, pt.Y)
      If hti.Column = 1 AndAlso hti.Row > 1 AndAlso hti.Row < 5 Then
        Cursor.Current = Cursors.Hand
      Else ' otherwise call the base class method
        MyBase.WndProc(m)
      End If
    End If
  End Sub
End Class

Contributed from George Shepherd's Windows Forms FAQ



Page view counter