Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Serialization of objects added to controls at design-time
 

Serialization of objects added to controls at design-time

I am trying to build a user control that has serveral pages, similar to a TabControl control. Below are some fragments of the control. For short I'll call it UC.

UC contains two panels. One is of no interest in this context, the other is called 'pan__panels' (actually a class derived from Panel) which accepts controls the user can drag to it. The user can add tabs to the control (which is not visible in the code below). For each tab he adds the method 'AddNewPanel' is invoked, i.e. the tab is associated with a panel. The tabs are owner drawn vertically or horizontally according to the text and image the user defines.

The panels linked to a tab are never visible. The only visible panel is pan__panels. If the user clicks a tab, all contols on 'pan__panels' are moved to the currently active panel, the panel associated to the clicked tab becomes the active one and its controls are moved to 'pan__panels'.

I create a TestForm, drag a UC control to it, add some tabs to it and on each page a control or two. This works quite fine so far. But the big problem is, that the whole stuff is not persistent (and thus of no use).

Even if I add the new panels to UC.Controls, they are not serialized. The controls I added, are all added to TestForm.Designer and so are the tabs. But how do I get the panels and the links between the controls and the panels to TestForm.Designer?

Though I have used days going through much of the stuff on the internet about serialization, I have not come accrosss anything that would help me any further.

I have also tried a totally different concept, adding a specific object to the Tag property of controls (not placing them an separate panels). But then the very same problem arises: how do I serialize the object assigned to Tag?

Maybe there's an altogether better approach to the whole thing. Any suggestions are welcome. And thanks for any hint anyway.

Walter

1//thisisthecontroltheusercandragfromthetoolboxtoaformorpanelorwhatever
2
3usingSystem;
4usingSystem.Collections.Generic;
5usingSystem.ComponentModel;
6usingSystem.Drawing;
7usingSystem.Data;
8usingSystem.Text;
9usingSystem.Windows.Forms;
10
11namespaceOwnComponents.NormTabs
12{
13
14//thisdesignerclassmakesthepanel'pan__panels'(placedonthisform)capableof
15//receivingcontrolsfromthetoolbox(bydraganddrop)
16[Designer(typeof(cmpNormTabsDesigner))]
17publicpartialclasscmpNormTabs:UserControl
18 {
19
20
21privatePanelfRecentTab;
22privateTTabPosfTabAlign;
23
24privateList<Panel>fPanels;
25
26

publicTTabPosTabAlign{get{returnfTabAlign;}set{HandleAlign(value);}}

// this actually does not belong to this class, just for info. The user can edit it in the designer, causing "AddTab" to be invoked.

27 // public TTabHelp [] Tabs { get { return fTabs; } set { HandleTabNames ( value ); } }

28//thiscrashesthevisualstudio
29//[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
30//publicList<Panel>Panels{get{returnfPanels;}set{fPanels=value;}}
31
32
33publiccmpNormTabs()
34{
35InitializeComponent();
36
37fPanels=newList<Panel>();
38fTabAlign=TTabPos.cTpBtm;
39HandleAlign(TTabPos.cTpLeftV);//Default
40}
41
42publicTNormTabsPanelTabPanel{get{returnpan__tabs;}}
43
44[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
45publicTNormTabsPanelControlPanel{get{returnpan__panels;}}
46
47//calledwhentheuserchangestheTabAlignproperty
48privatevoidHandleAlign(TTabPosaAlign)
49{
50//somecode
51}
52
53//calledwhenclickingonatab(atdesigntime,fromanotherclass)
54internalvoidHandleTab(PanelaPan)
55{
56if(fRecentTab==aPan)
57{return;}
58
59PanelxPanS=aPan;
60PanelxPanD=fRecentTab;
61
62while(pan__panels.Controls.Count>0)
63{
64xPanD.Controls.Add(pan__panels.Controls[0]);
65}
66
67while(xPanS.Controls.Count>0)
68{
69pan__panels.Controls.Add(xPanS.Controls[0]);
70}
71fRecentTab=aPan;
72}
73
74
75//calledwhentheuser(atdesigntime)changesthetabs
76internalPanelAddNewPanel()
77{
78PanelxPan=newPanel();
79
80xPan.Name="panel"+(fPanels.Count+1).ToString();
81xPan.Location=newPoint(0,0);
82xPan.Size=newSize(100,100);
83xPan.Visible=false;
84
85fPanels.Add(xPan);
86
87//doesn'tdoanyharmbutalsoisofnouse
88this.Controls.Add(xPan);
89
90if(fRecentTab==null)
91{fRecentTab=fPanels[0];}
92
93returnxPan;
94}
95}
96}
97
98
WaSaDu  Saturday, January 24, 2009 2:26 PM

Hi WaSaDu,

Based on my understanding, you have a UserControl which contains Panels on it. When you add this UserControl on a form at design time, youcan drag a control from Toolbox and drop it onto the Panels within the UserControl. The problem is that the code to add the control to the Panel isn't serialized. If I'm off base, please feel free to let me know.

You need to implement a custom CodeDomSerializer to serialize the code that adds the control to the Panel.

For more information on CodeDomSerializer, refer to the following document:

http://msdn.microsoft.com/en-us/library/system.componentmodel.design.serialization.codedomserializer.aspx

Hope this helps.

Sincerely,
Linda Liu

Linda Liu  Friday, January 30, 2009 7:26 AM

You can use google to search for other answers

Custom Search

More Threads

• Using WMI to get date and time on a remote computer
• How to make inplace editor not editable.
• How to have drawings show up in Design view
• Short cut to find a control in the designer
• Custom UITypeEditor not working with UITypeEditorEditStyle.Modal
• Docking Window Management Control
• Object does not match target type
• DuplicateItemsNotSupported
• dropdrown menu in toolbar
• Focusing a Control Child in UserControl