Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Unhandled exception in the designer (OnPaint)

Unhandled exception in the designer (OnPaint)

Scheduled Pinned Locked Moved C#
designquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AussieLew
    wrote on last edited by
    #1

    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?

    L S 2 Replies Last reply
    0
    • A AussieLew

      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?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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).


      1 Reply Last reply
      0
      • A AussieLew

        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?

        S Offline
        S Offline
        Stanciu Vlad
        wrote on last edited by
        #3

        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...

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups