Just wondering it this code is safe. Thanks for input. MESSAGE CLOSED
-
I've tried this implementation and it works but I am wondering if there are any underlying problems with it; ie Loading Panel at runtime. I am not that experienced in VB. Thanks Class MyClass Inherits UserControl Private WithEvents _MyParentPanel As Panel Private Sub PChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.ParentChanged Me._MyParentPanel = CType(sender, Control).Parent end sub Private Sub ParentScrollChange(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles _MyParentPanel.Scroll ' Handle change End Sub End Class
-
I've tried this implementation and it works but I am wondering if there are any underlying problems with it; ie Loading Panel at runtime. I am not that experienced in VB. Thanks Class MyClass Inherits UserControl Private WithEvents _MyParentPanel As Panel Private Sub PChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.ParentChanged Me._MyParentPanel = CType(sender, Control).Parent end sub Private Sub ParentScrollChange(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles _MyParentPanel.Scroll ' Handle change End Sub End Class
What happens if
Parent
is not aPanel
?
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
I've tried this implementation and it works but I am wondering if there are any underlying problems with it; ie Loading Panel at runtime. I am not that experienced in VB. Thanks Class MyClass Inherits UserControl Private WithEvents _MyParentPanel As Panel Private Sub PChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.ParentChanged Me._MyParentPanel = CType(sender, Control).Parent end sub Private Sub ParentScrollChange(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles _MyParentPanel.Scroll ' Handle change End Sub End Class
Yeah, there's lots of problems with it. We'll start with your assumption that the control will over ever be dropped on a Panel control. What if you accidentally drop it on a Form instead? You're code is going to throw an exception and the designer is going to show a problem that prevents rendering it and you'll probably freak out when that happens. A control can have many different control types as a parent. Change the references "Panel" to "Control" so you can support different container controls, including a Form since the Form class derives from Control. It's also not a good idea to rename your event handlers, specifically
PChanged
, to whatever is shorter. It make the code harder to read and understand without your eyes darting to the end of each line to figure out which event this method handles. Your name is also not descriptive at all. What's a "P" and what changes about it?Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
I've tried this implementation and it works but I am wondering if there are any underlying problems with it; ie Loading Panel at runtime. I am not that experienced in VB. Thanks Class MyClass Inherits UserControl Private WithEvents _MyParentPanel As Panel Private Sub PChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.ParentChanged Me._MyParentPanel = CType(sender, Control).Parent end sub Private Sub ParentScrollChange(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles _MyParentPanel.Scroll ' Handle change End Sub End Class
To All, Sorry for not providing enough information. I was focused on the implementation of whether Me._MyParentPanel = CType(sender,Control).Parent had any underlying issues as far as 'attaching' the control to the Panels Scroll event because of loading _MyParentPanel at runtime. I understand and agree with the issues you raised but: This control is for a specific application and will not be dropped onto the Panel or be used for any other purpose. It is created by a button click from the Panel and is added by the Panel onto the Panel. Thanks
-
To All, Sorry for not providing enough information. I was focused on the implementation of whether Me._MyParentPanel = CType(sender,Control).Parent had any underlying issues as far as 'attaching' the control to the Panels Scroll event because of loading _MyParentPanel at runtime. I understand and agree with the issues you raised but: This control is for a specific application and will not be dropped onto the Panel or be used for any other purpose. It is created by a button click from the Panel and is added by the Panel onto the Panel. Thanks
speedbump99 wrote:
This control is for a specific application and will not be dropped onto the Panel or be used for any other purpose.
Famous last words. Controls always start out that way, but then next year, "Hey! We've got this control we want to use somewhere else..." Do it right the first time.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
speedbump99 wrote:
This control is for a specific application and will not be dropped onto the Panel or be used for any other purpose.
Famous last words. Controls always start out that way, but then next year, "Hey! We've got this control we want to use somewhere else..." Do it right the first time.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak