 |
|
 |
Hi,
Thanks for the article, it certainly helped shed some light on reflection. My question is that I am troubled by the meaning of "serialization" mentioned in the article, because I am till this minute unsuccessful in actually serializing the control.
CBFormCtrl has a Hashtable so this means that no xmlserialzer can take it (in .net 2.0) I have replaced it with a custom implementation that is serializable but now i am getting an error about the Color property not being serializable in the combobox or picturebox.
Any idea how to achieve actual serialization to file?
thanks
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
 |
Here the code I added to manage TabControls cloning:
First, I created a new function to clone all controls for all pages:
public static Control CloneTabControl(TabControl origCtrl) { TabControl oResult = new TabControl(); foreach (TabPage page in origCtrl.TabPages) { TabPage newPage = new TabPage(page.Text); foreach (Control control in page.Controls) { Control ctrl = ControlFactory.CloneCtrl(control); newPage.Controls.Add(ctrl); ctrl.Show(); } oResult.TabPages.Add(newPage); } return oResult; }
Then, I changed the original code to get the TabControl in my new function like this :
public static Control CreateControl(string ctrlName,string partialName,Control original) { try { Control ctrl; switch(ctrlName) { ...
case "TabControl": ctrl = CloneTabControl((TabControl)original); break; } return ctrl; ... } }
public static Control CloneCtrl(Control ctrl) { ... Control newCtrl = ControlFactory.CreateControl(cbCtrl.CtrlName,cbCtrl.PartialName,ctrl); ... }
public static Control GetCtrlFromClipBoard() { ... ctrl = ControlFactory.CreateControl(cbCtrl.CtrlName,cbCtrl.PartialName,null); ... }
Enjoy
modified on Tuesday, July 21, 2009 9:03 AM
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Thank you very much it helped me a lot
I created a function to clone a group of controls
private Control CopyProperties(Control ctSource) { Control ctNew = (Control)NewAs(ctSource.GetType());
PropertyDescriptorCollection p = TypeDescriptor.GetProperties(ctSource);
Hashtable proplist = new Hashtable(p.Count);
foreach (PropertyDescriptor d in p) { //need to check this if (d.PropertyType.IsSerializable) { proplist.Add(d.Name, d.GetValue(ctSource)); } }
if (ctSource is ComboBox) // || ctSource is xpWinFormsControls.xpDropDown) { Object[] objItems = new Object[((ComboBox)ctSource).Items.Count]; ((ComboBox)ctSource).Items.CopyTo(objItems, 0); //for (int q = 0; q < c.Items.Count; c++) //{ ((ComboBox)ctNew).Items.AddRange(objItems); //} }
if (ctSource is xpWinFormsControls.xpDropDown) { Object[] objItems = new Object[((xpWinFormsControls.xpDropDown)ctSource).Items.Count]; ((xpWinFormsControls.xpDropDown)ctSource).Items.CopyTo(objItems, 0); //for (int q = 0; q < c.Items.Count; c++) //{ ((xpWinFormsControls.xpDropDown)ctNew).Items.AddRange(objItems); //} }
//This is Writtent here to avoide exception occured due to setting SelectedIndex more than 0 //When there are no any Items in Items collection .
//Get the properties collection from new created control. PropertyDescriptorCollection pdNew = TypeDescriptor.GetProperties(ctNew);
//Copy every property from hash table to this newly created property collection. for (int h = 0; h < pdNew.Count; h++) { if (proplist.Contains(pdNew[h].Name)) { try { pdNew[h].SetValue(ctNew, proplist[pdNew[h].Name]); } catch (Exception ex) { continue; } } } return ctNew; }
But I am unable to copy methods associated with this particular instance
Can you help me in this?
Tahnk you Sandesh Daddi http://sanshark.com
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
It works great, seems like it did copy all the properties, but waht baout data? How can we also copy the data. I have a user control that consists 3 datagridviews, when I make the clone, the data is not copied. What can I do to copy data in the correct cells of the datagrids? Thanks
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Warning 5 'System.Reflection.Assembly.LoadWithPartialName(string)' is obsolete: 'This method has been deprecated. Please use Assembly.Load() instead. http://go.microsoft.com/fwlink/?linkid=14202' ControlFactory.cs 59
Could u post some update? What should I correct?
Thanx in advce
Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi,
My application has a Form which contains a data table,graph, and many other
controls.I want to save the Form as it is at some point of time while the form
is running. when i tried to serialize the form object i am getting an
exception as System.Windows.Forms.Form is not marked as serilized.
How can i rectify this error and save the form object.
Regards, Sakthi
|
| Sign In·View Thread·PermaLink | 1.67/5 |
|
|
|
 |
|
 |
If you add this code to the method CloneCtrl:
foreach (Control child in ctrl.Controls) { newCtrl.Controls.Add(CloneCtrl(child)); }
It will clone the children of the control, what is useful for copying containers such groupboxes.
Thanks for your code!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Your code works very well, but I've got a problem with Events. Indeed, if my control (a Label for example) has been subscribed to an event (eg MouseClick), the clone method doesn't copy that event in the new object. Have you got a solution to solve this problem ? Thank you very much !
Stéphane
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
hi, to copy the event of a control, you need to rely on the CodeDOM solution, please refer to the implementation of SharpDevelop or find some info in msdn about CodeDOM.
thanks.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi!
I tried to use this code with my extended control. For example I have a my usercontrol with 2 properties like this:
public Color BorderColor { get { border_color; }
set { border_color = value; } }
public int BorderWidth { get { border_w; }
set { border_w = value; } }
this properties is setted well but CloneCtrl simply display a rectangle with size and location correctly (this is default properties....) but don't show my border properties....
have a solution for this problem?
TNX!
Best regards!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi, I've tested your problem with a usercongrol, the user-defined properties are copied correctly. You can observe the values to see if they are being copyied when debugging. If you want to show the properties in the VS propertygrid, try to add Browserable attribute like this:
[Browsable(true)] public Color BorderColor { get { return border_color; }
set { border_color = value; } }
hope it helps.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
there is one problem with personalized controls though - if they have a name like "Me.My.Label", they will not be copied, as your method only looks at the "last name" of the control (hence trying to create a Windows.Forms.Label). It was easy to fix though - remove the part of your code that switches on the name, and it is done.
Apart from that, I think you have done a really good job.
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
 |
Nice work. I've wondered if there are any facilities in the FrameWork that could be exploited for using reflection and whatever-other techniques at run-time to parse all the properties of a control and clone/serialize.
Would like to know more about what's under the hood in the IDE when you use it to copy a control at design-time.
best, Bill
"The greater the social and cultural distances between people, the more magical the light that can spring from their contact." Milan Kundera in Testaments Trahis
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
 |
Hi Bill, I am also wondering how the VS.net IDE copy&paste a control at design time. What I can tell is that it copies/serializes not only the control properties, but also the non-property members. It is more like a memberwise clone. But I can not figure out how to accomplish that without relying on System.Windows.Forms.Design
Maybe you can find some hint from SharpDevelop.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
lxwde wrote: I am also wondering how the VS.net IDE copy&paste a control at design time.
I assume it uses the CodeDom classes in the same way as it does when it "serializes" a control between editing sessions?
Team Leader - Team Code Project[^] 
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi,
Yes, that is exactly how it works. The controls are serialized to a code stream and deserialized at the other end. Essentially it is a stream of text. VG.net works the same way.
check out VG.net: www.vgdotnet.com An animated vector graphics system integrated in Visual Studio
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |