Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All
Greetings

I am creating a simple usercontrol which has 5 buttons in it. I am developing this just to learn how to create a usercontrol. I am 5 button during runtime to a list and simultaneously to the usercontrol. I am adding it to the list so that i can edit those buttons using property grid. the code is as follows


private List<Button> bttns = new List<Button>();

[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Editor(typeof(BlitzHeaderPanelCollectionEditor),typeof(UITypeEditor))]
public List<Button> Bttns
{
    get{return bttns;}
}

int the initilisecomponent function i am adding 5 buttons programatically.

Now my issue is i am able to see those 5 buttons as collections in propertygrid
if i edit them the changes take place during design time but when i run the program the changes are wiped off to the defualt. Why is this happening. Please help me experts
Posted
Updated 6-Oct-11 1:31am
v2

If you are creating dynamic buttons and then adding them to the list, they will not use the properties that you set at design time because these are different instances.

For this, you will need to set the properties of the button at run-time e.g. btn1.Background = Brushes.Pink;
 
Share this answer
 
Hi,

What happens is that the parent control, the main form, contains the new values that you have applied to the user control's properties during design time. Those settings are applied before your user control has initialized, and therefore user control's default state will take precedence.

If your control implements the ISupportInitialize interface, the EndInit method will by called by the parent control, the main form, once it has finished setting your control's properties. You can then do your initialization within EndInit.

To see this, inspect the InitializeComponent code on the main form to verify that infact EndInit is being called for your control.
 
Share this answer
 
There is a perfect example of this right here on codeproject...

Component and ISupportInitialize: A Quick Guide[^]
 
Share this answer
 
v3
Comments
walterhevedeich 7-Oct-11 4:31am    
Comment from OP(Removed from Answers): Thanks alot Szeeze. Its working now :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900