control onpaint does not work

Discussion in 'C#' started by morfix78, Oct 2, 2007.

  1. morfix78

    morfix78 New Member

    Joined:
    Sep 16, 2007
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    hello,
    I have created a panel (I inherit from the form panel) and a control.
    when i add the panel to a form the onpaint of the panel, which I override is called. in the onpaint function of my panel i create an instance of my control and i add it to the controls of the panel. in my contorl I also override the onpaint of the contorl but this time the on paint of my contorl is not called when i add it to the conrols property of the panel which i created.
    how can i call the onpaint of the control from the onpaint of the panel?
    thanks
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You should not create a control in the OnPaint Method as it is called million times. Try creating the control in the OnCreateControl method because it will be called when the control is being created.
     
  3. morfix78

    morfix78 New Member

    Joined:
    Sep 16, 2007
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    I am sorry but i did not understand.
    which control i need to create in OnCreateControl method and where do i override the OnCreateControl? the panel or the conrtol?
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Instead of doing this in OnPaint you should be doing it in OnCreateControl. The OnPaint of which ever class you mentioned.
     
  5. morfix78

    morfix78 New Member

    Joined:
    Sep 16, 2007
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    thank for your help.
    just for general knowledge i would like to know why the onpaint of the panel is called automatically when i add it to the controls of the form and the onpaint of the control does not work when i add it to the controls of the panel.
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    OnPaint is called whenever control need to be painted and so when you add the new control it needs re-painting.
     
  7. morfix78

    morfix78 New Member

    Joined:
    Sep 16, 2007
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    thanks for your reply.
    if as you say when I add the new control it needs re-painting, then why the onpaint of the contorl is not called when i add it to the controls of the panel?
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    When the OnPaint will be called is totally dependent on the OS and you should read the documents about Windows Programming and Paint method use. Whenever windows think it needs the control to be painted it sends the Paint method. Your case could be that you are adding a control in one case its transparent and so parent is sent the paint method and other case its not transparent and so its not sent.
     
  9. morfix78

    morfix78 New Member

    Joined:
    Sep 16, 2007
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    hello,
    I did as you said but I have encountered in another problem. when i create the panel the oncreatecontrol is called and all the controls are created and painted in the panel but when i create the panel again not all the controls are painted in the panel and only one is painted in the panel. can you tell me what is the problem?
     
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You InValidate the other panels when they need to be painted.
     
  11. morfix78

    morfix78 New Member

    Joined:
    Sep 16, 2007
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    I call invalidate in the form and in the panel but then nothing is being painted
     
  12. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Then probably some other thing is wrong somewhere else.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice