Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Designer keeps adding tablestyles in VS 2003.
 

Designer keeps adding tablestyles in VS 2003.

Hi, I have a problem that is very annoying. I have a custom datagrid that extends the System.Windows.Forms.Datagrid, and are also using a custom Tablestyle that extends from the System.Windows.Forms.DatagridTableStyle. the tablestyle is created in the constructor of the custom datagrid. Everytime I open up the form that have the custom datagrid in Visual Studios designer, the Designer add a new tablestyle to the form and adds it to the Datagrid.Tablestyles.Addrange collection. So it doesnt take long before I have 10-15 tablestyles in that collection that are taking up space in the code, and I do not want any of them.

Any ideas how I can get rid of this behaviour??

/Christian
Christian_G  Thursday, September 29, 2005 7:46 AM

Hi Christian,

I'm not sure, but i suppose that:
(i) The ctor adds a new style (a StyleCollection member). This happens each time you open the form.
(ii) when closed, the form (designer) serializes the whole collection (with the item added in this session).

You open the form again and get two items in the second design session, 3 in the 3rd, etc., so you know exactly, how many times you have changed the form, since the grid was created :).

Break the loop. There are at least two simple ways.
(A) Do not create items in the ctor. The design time user must create an item (or 2, or 3, ...) "manually", i.e. with the help of the style collection designer.
(B) Make sure that "the item" (if you need only one) is created only once, i.e. only when nothing is persisted, when a control instance is created for the 1st time. How to distinguish? In ctor you can not know will something be depersisted or not, this is done later. Thus, you must wait until everything is depersisted, look is the item there or not (you must know how to check for "the item" -- by name, by some properties, if the user (designer) can rename the item) and create one only if needed. The best [the only?] point i know, is ISupportInitialize.EndInit(). This method will be called when all components are already depersisted.

HTH,
Dima.

DRaiko  Thursday, September 29, 2005 8:31 AM

You can use google to search for other answers

Custom Search

More Threads

• Datagridview: columns keep shifting position
• Listbox Ownerdraw-Problem
• Visual Studio .Net 2003 error: Exception occurred while painting the label for property
• Custom Control inherited from GroupBox
• An item with the same key has already been added
• to know the system date
• Continuous Forms
• How to remove Image from custom control at design time
• VS Bogging Down - How Do I Stop It
• Dynamically Embed Resources