Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / April 2005

Tip: Looking for answers? Try searching our database.

Inherited form - Protected sub / Derived form - Private Shadows subs

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dbuchanan - 27 Apr 2005 21:48 GMT
Is the following behavior normal?

Both the 'Protected sub' in the inherited form and the 'Private Shadows
sub' in the derived form fires.

My interpretation of MSDN help on the topic "Shadows" does not seem to
indicate that this is the designed behavior. (the topic is rather
cryptic to me.

Here is my code;

=== In the Inherited form is this code; ===
   Protected Sub DataGrid1_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseUp
       MessageBox.Show("DataGrid1_MouseUp - frmForm1")
       'Highlight the entire row
       'Get the X and Y of the DataGrid from the mouse event
       Dim pt As New Point(e.X, e.Y)
       Dim hti As DataGrid.HitTestInfo = Me.DataGrid1.HitTest(pt)

       If hti.Type = DataGrid.HitTestType.Cell Then
           'Me.DataGrid1.CurrentCell = New DataGridCell(hti.Row,
hti.Column)
           Me.DataGrid1.Select(hti.Row)
       End If

   End Sub

=== In the derived form is this code; ===

   Private Shadows Sub DataGrid1_MouseUp(ByVal sender As Object, ByVal
e As System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseUp
       MessageBox.Show("DataGrid1_MouseUp - frmJobCustomer")
       Call PopulateStatusBarData()
       Call RowSelectedToEdit()
   End Sub

=========================================
Both subs fire. First the code in the inherited form and second the
code in the derived from.

This is the behavior I desire, but is this behavior what it is supposed
to do - can I rely on it?

Thank you,
Doug.
Richard Myers - 28 Apr 2005 00:12 GMT
Yes it is. You're using a handles statement which is registering each
method with the event. You'd be better off with something more like this:

' Base Class
Private Sub DataGrid1_MouseUp(ByVal sender As Object, ByVal e As
   System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseUp
   MyEventHandler(e)
End Sub

Protected Overridable Sub MyEventHandler(ByVal e As
System.Windows.Forms.MouseEventArgs)
'    Base Class Functionality
End Sub

' Derived Class
Protected Overrides Sub MyEventHandler(ByVal e As
System.Windows.Forms.MouseEventArgs)
   MyBase.MyEventHandler(e)
   ' Derived Functionality
End Sub

Same result but much cleaner.

Richard

> Is the following behavior normal?
>
[quoted text clipped - 42 lines]
> Thank you,
> Doug.
Dennis - 28 Apr 2005 01:54 GMT
I think you can do the same thing by using the "OnMouseUp" overridable event
of the base class:

In the derived class:

Protected Overrides Sub OnMouseUp(ByVal e As
System.Windows.Forms.MouseEventArgs)

'Do you thing here and don't call MyBase.OnMouseUp since this would in turn
call the MouseUp event in your base class. If you want to fire the MouseUp
event in the program's parent, then you can set up an event called mouseup in
the derived class and raise that event in this sub.

If you want to call that event, then add

MyBase.OnMouseUp(e)

End Sub

> Yes it is. You're using a handles statement which is registering each
> method with the event. You'd be better off with something more like this:
[quoted text clipped - 67 lines]
> > Thank you,
> > Doug.
Richard Myers - 28 Apr 2005 04:18 GMT
Hi Dennis

I disagree with what you have posted.

> I think you can do the same thing by using the "OnMouseUp" overridable event
> of the base class:

This would require he subclass the datagrid class it wouldn;t work using
his current form class. Which means displaying message boxes from controls
which is really ugly.

> In the derived class:
>
[quoted text clipped - 3 lines]
> 'Do you thing here and don't call MyBase.OnMouseUp since this would in turn
> call the MouseUp event in your base class.

He must call MyBase.OnMouseUp ( or whatever he decides to call it) as he
has functionlity that he wishes to run in the base class. He hasn't said
he's trying to suppress the event. He simply wants to run some code in base
class to derived class order which is the whole point of using
MyBase.OnMouseUp first.

If you want to fire the MouseUp
> event in the program's parent, then you can set up an event called mouseup in
> the derived class and raise that event in this sub.
[quoted text clipped - 4 lines]
>
> End Sub

Why does he need to create another event called MouseUp? He already has
this event in both the form and the datagrid? And how does this code fire
that event?
MyBase.OnMouseUp(e) calls the sub in the base class would in turn fires the
base class MouseUp event not his custom event.

> > Yes it is. You're using a handles statement which is registering each
> > method with the event. You'd be better off with something more like this:
[quoted text clipped - 67 lines]
> > > Thank you,
> > > Doug.
dbuchanan - 28 Apr 2005 19:20 GMT
Richard and Dennis,

Thank you for your discussion. In the mean time I made a few
discoveries and ended solving it in the following manner.

'Base Class
   Protected Overridable Sub DataGrid1_MouseUp(ByVal sender As Object,
ByVal e As System.Windows.Forms.MouseEventArgs) Handles
DataGrid1.MouseUp
       'MessageBox.Show("DataGrid1_MouseUp - frmForm1")
       'Highlight the entire row
       'Get the X and Y of the DataGrid from the mouse event
       Dim pt As New Point(e.X, e.Y)
       Dim hti As DataGrid.HitTestInfo = Me.DataGrid1.HitTest(pt)

       If hti.Type = DataGrid.HitTestType.Cell Then
           'Me.DataGrid1.CurrentCell = New DataGridCell(hti.Row,
hti.Column)
           Me.DataGrid1.Select(hti.Row)
       End If
       'MessageBox.Show("Row highlighted")
   End Sub

'Derived Class

   Protected Overrides Sub DataGrid1_MouseUp(ByVal sender As Object,
ByVal e As System.Windows.Forms.MouseEventArgs) Handles
DataGrid1.MouseUp
       'MessageBox.Show("DataGrid1_MouseUp - frmForm1")
       MyBase.DataGrid1_MouseUp(sender, e)
       'Local implementation
       Call PopulateStatusBarData()
       Call RowSelectedToEdit()
   End Sub

Is this the most efficient way to do this?

-Doug

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.