WinForms: programatically created control will not paint
-
I have created a custom control, which I am trying to programatically add to my windows form. The problem is that the control's OnPaint method is never invoked, even though the control invalidates itself on regular intervals (it uses a timer). If the control is added to my form in Visual Studio's designer, all works perfectly. However, I can't add the control in the designer because I don't know how many instances of my control I will need until my application reaches a certain point. Any help is much appreciated! Thanks
-
I have created a custom control, which I am trying to programatically add to my windows form. The problem is that the control's OnPaint method is never invoked, even though the control invalidates itself on regular intervals (it uses a timer). If the control is added to my form in Visual Studio's designer, all works perfectly. However, I can't add the control in the designer because I don't know how many instances of my control I will need until my application reaches a certain point. Any help is much appreciated! Thanks
You are almost certainly overlooking some of the code the designer would create for you. Did you remember to add your new control to the form's controls collection? Absolute faith corrupts as absolutely as absolute power Eric Hoffer All that is necessary for the triumph of evil is that good men do nothing. Edmund Burke
-
You are almost certainly overlooking some of the code the designer would create for you. Did you remember to add your new control to the form's controls collection? Absolute faith corrupts as absolutely as absolute power Eric Hoffer All that is necessary for the triumph of evil is that good men do nothing. Edmund Burke
Thank you for your reply Rob!! Yes, I have added the control to the collection. I have tried to mimic what the designer does, but maybe I have missed some small detail anyway. Does the paint event have to be tied to something before it can be invoked after invalidation of the control? I guess the windows form's message pump is involved somehow...perhaps it is not setup properly. I don't know much about the inner workings of windows forms and their controls, so I'm quite confused.
-
Thank you for your reply Rob!! Yes, I have added the control to the collection. I have tried to mimic what the designer does, but maybe I have missed some small detail anyway. Does the paint event have to be tied to something before it can be invoked after invalidation of the control? I guess the windows form's message pump is involved somehow...perhaps it is not setup properly. I don't know much about the inner workings of windows forms and their controls, so I'm quite confused.
-
Thank you for your reply Rob!! Yes, I have added the control to the collection. I have tried to mimic what the designer does, but maybe I have missed some small detail anyway. Does the paint event have to be tied to something before it can be invoked after invalidation of the control? I guess the windows form's message pump is involved somehow...perhaps it is not setup properly. I don't know much about the inner workings of windows forms and their controls, so I'm quite confused.
Perhaps a snippet or two of your code would help. The implementation of the OnPaint handler, and the code used to add the control dynamically, for instance... Absolute faith corrupts as absolutely as absolute power Eric Hoffer All that is necessary for the triumph of evil is that good men do nothing. Edmund Burke
-
A thought: does your code call the Invalidate() method of the parent control? This method has multiple overloads, three of which take a boolean parameter "invalidateChildren".
I managed to get my control to show up finally, but it doesn't happen the way I want to. I had not actually added the control to the form's controls collection, but instead in the DataGridTextBoxColumn.TextBox.Controls. If I add the control to the form's collection, it shows up like it should. However, this is not the way I want to do it, since my form should only be aware of the columnstyles I use, and not of the controls they may use. I've written a new thread about this with a complete sample code that illustrates the problem. I hope you can help me! Thanks!!