Confusion on Protected Overrides

R

Ron Dahl

I'm very confused on how the Protected Overrides works.
I created a new project with a new form1 and a new datagrid called
myDataGrid.
I created a simple DataTable and put 5 rows and 5 columns of data into it.
I bound the DataTable to the DataGrid.

The example at the end of this post is exactly as copied from the vb.net
help screen:

I put the new myDataGrid Class following the Form1 Class.
I can make the code work with a Button Click event, but not the MouseDown
event.

When I click on a row it is not selected.

I don't understand how to connect the MouseDown mouse event to my form1
class to make the connection.

How do I properly create an instance of the myDataDrid class?
Do I need eventhandler code of some sort in Form1?
Do I need a delegate or something to make this example work?

Thanks in advance for any help or pointing me in the right direction.

Ron Dahl



Public Class MyDataGrid
Inherits DataGrid

' Override the OnMouseDown event to select the whole row
' when the user clicks anywhere on a row.

Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
' Get the HitTestInfo to return the row and pass
' that value to the IsSelected property of the DataGrid.
Dim hit As DataGrid.HitTestInfo = Me.HitTest(e.X, e.Y)
If hit.Row < 0 Then
Return
End If
If IsSelected(hit.Row) Then
UnSelect(hit.Row)
Else
[Select](hit.Row)
End If
End Sub
End Class
 
C

Chris

Ron said:
I'm very confused on how the Protected Overrides works.
I created a new project with a new form1 and a new datagrid called
myDataGrid.
I created a simple DataTable and put 5 rows and 5 columns of data into it.
I bound the DataTable to the DataGrid.

The example at the end of this post is exactly as copied from the vb.net
help screen:

I put the new myDataGrid Class following the Form1 Class.
I can make the code work with a Button Click event, but not the MouseDown
event.

When I click on a row it is not selected.

I don't understand how to connect the MouseDown mouse event to my form1
class to make the connection.

How do I properly create an instance of the myDataDrid class?
Do I need eventhandler code of some sort in Form1?
Do I need a delegate or something to make this example work?

Thanks in advance for any help or pointing me in the right direction.

Ron Dahl



Public Class MyDataGrid
Inherits DataGrid

' Override the OnMouseDown event to select the whole row
' when the user clicks anywhere on a row.

Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
' Get the HitTestInfo to return the row and pass
' that value to the IsSelected property of the DataGrid.
Dim hit As DataGrid.HitTestInfo = Me.HitTest(e.X, e.Y)
If hit.Row < 0 Then
Return
End If
If IsSelected(hit.Row) Then
UnSelect(hit.Row)
Else
[Select](hit.Row)
End If
End Sub
End Class

You need to define the datagrid in the Form using the WithEvents keyword.

If that doesn't fix it, please show us your form1 code.

Chris
 
P

Phill W.

Ron Dahl said:
I'm very confused on how the Protected Overrides works.

Does your the program actually get into your overriding routine?
Put a break point in the code where you do the HitTest() call.

If /not/, then the Form /isn't/ using your new grid class.
Check the Designer-Generated code and make sure the grid on the
form is actually defined as /your/ grid class and not just a DataGrid.

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top