cannot draw graphics on panel

Posted on 2008-11-20
Last Modified: 2013-12-17
hi -- I have two panels that are added to a tab control that make up the primary division of my windows form application. The right hand panel (called graph_panel) will draw appropriately when a Graphics object is created, attached to the graph_panel. However, based on a parameter passed to the method, i want to add a child panel (programmatically), create a Graphics object, and put some lines in the child panel. when going into run, the child panel flickers briefly and then whatever is drawn disappears. Does anybody know what limitations there are to creating a child panel as a graphics container?
Question by:jdcoburn
    LVL 18

    Expert Comment

    Is the child panel inside the graph_panel or is the child the left hand panel?  I would not be surprised if layering multiple panels would not display properly.  You might want to draw the panel (i.e. rectangle) instead of trying to layer two panels.

    Author Comment

    hi --thanks for the comment. i finally got the child panel to work -- i had to call childPanel.Refresh() before i created teh graphics object to draw the line. any idea why that would be the case? it doesn't work if i call refresh after the graphics object is created. what is refresh actually doing?
    thanks for any help.
    LVL 18

    Accepted Solution

    Not sure about refresh.  I haven't ever tried to create a panel and draw on it programmatically.  From what you described, I was thinking you could have a persistent child panel, and depending on the param in your draw method, call out childpanel.invalidate.  Then, in childpanel_paint event, call a method to draw on your child panel with childpanel.creategraphics passed as the graphics object.  

    In case you haven't set these options, here are a couple gdi+ settings I've found helpful:

    Put in the load event of the form

    causes objects to repaint when the form is resized
    Me.SetStyle(ControlStyles.ResizeRedraw, True)

    I think this one smooths out the drawing process, less flickering
    Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)


    Author Comment

    ok -- I suspect a lot of the problem is related to when and how the drawing takes place. I have the main panel.paint event wired to the original method, so that it re-calls the entire drawing process. this works fine for other controls. but it doesn't resize the programmatically added controls, even though i'm using the parent width property. the other anomoly is related to docking. if i programmatically dock, it doesn't work.
    i'll try your recommendations and get back to you.

    Author Comment

    in the for what it's worth category, I was able to solve the problem by using handles to the controls, which allowed me to explicitly control the drawing order and could be passed to the paint method.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now