Unhandled exception in the designer (OnPaint)
-
I have custom control that I drag from the ToolBox onto a form. Some of the custom control properties are set in the control's Properties Window after the control has been dropped onto the form. These control properties eg BindingSource and DataGridView depend on a BindingSource and DataGridView attached to the form. (Either before or after this custom control) As these properties are referenced (but not available) in the custom control's OnPaint method, a Null Reference Exception is raised in design mode as soon as the control is placed on the form and tries to paint itself. What would be a friendlier way to remind the developer (me) that these properties are required to be set? Should I just do a Try/Catch and not paint the complete control or maybe paint a blank control with text saying what other properties are required. I am the only one probably using this control so it is no real big deal, just a nuisance and doesn't seem to be the thing to do! Interested in how it would/should normally be handled?
-
I have custom control that I drag from the ToolBox onto a form. Some of the custom control properties are set in the control's Properties Window after the control has been dropped onto the form. These control properties eg BindingSource and DataGridView depend on a BindingSource and DataGridView attached to the form. (Either before or after this custom control) As these properties are referenced (but not available) in the custom control's OnPaint method, a Null Reference Exception is raised in design mode as soon as the control is placed on the form and tries to paint itself. What would be a friendlier way to remind the developer (me) that these properties are required to be set? Should I just do a Try/Catch and not paint the complete control or maybe paint a blank control with text saying what other properties are required. I am the only one probably using this control so it is no real big deal, just a nuisance and doesn't seem to be the thing to do! Interested in how it would/should normally be handled?
I would not go for a full paint of a custom Control in design view if that entails complex things such as database accesses. So either check DesignMode, or (my preference) check the relevant objects to avoid null references. Which basically means: paint what you've got. Chances are you want the same behavior at run-time anyway. And if you've got nothing, then maybe paint an appropriate placeholder. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
I have custom control that I drag from the ToolBox onto a form. Some of the custom control properties are set in the control's Properties Window after the control has been dropped onto the form. These control properties eg BindingSource and DataGridView depend on a BindingSource and DataGridView attached to the form. (Either before or after this custom control) As these properties are referenced (but not available) in the custom control's OnPaint method, a Null Reference Exception is raised in design mode as soon as the control is placed on the form and tries to paint itself. What would be a friendlier way to remind the developer (me) that these properties are required to be set? Should I just do a Try/Catch and not paint the complete control or maybe paint a blank control with text saying what other properties are required. I am the only one probably using this control so it is no real big deal, just a nuisance and doesn't seem to be the thing to do! Interested in how it would/should normally be handled?
I think that your control must be able to function even if it's not binded to data. Make it display something on it to inform you that there is no data binded (like a big red text "NO DATA BINDED" :)). This sould be easy if you say that you do the painting.
I have no smart signature yet...