473,480 Members | 1,964 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to clone a "Control"

Does anyone know how to do this accurately. I really only want to clone the
design-time properties which should make the task easier. I've searched high
and low however and still can't find a problem-free solution. Even (ad-hoc)
solutions posted by MSFT employees have problems. For instance, if you
simply copy all serializable properties, you may eventually receive an
"Object does not match target type" exception. In my case, this occurs when
I encounter (and clone) the "Location" property due to the fact that the
"Site" property was cloned earlier. If I clone "Location" first however
(before "Site"), it works fine. There must be a clean way to do this. Can
anyone offer any insight. Thanks.
Feb 22 '07 #1
3 46134
You can create the control using the designer, then copy the code and
remove the control, and then you can put the code in the code window and
create the control exactly the same way every time. Would that work for
you?
Robin S.
-------------------------------
"Rob Stevenson" <no_spam@_nospam.comwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
Does anyone know how to do this accurately. I really only want to clone
the design-time properties which should make the task easier. I've
searched high and low however and still can't find a problem-free
solution. Even (ad-hoc) solutions posted by MSFT employees have problems.
For instance, if you simply copy all serializable properties, you may
eventually receive an "Object does not match target type" exception. In
my case, this occurs when I encounter (and clone) the "Location" property
due to the fact that the "Site" property was cloned earlier. If I clone
"Location" first however (before "Site"), it works fine. There must be a
clean way to do this. Can anyone offer any insight. Thanks.

Feb 22 '07 #2
You can create the control using the designer, then copy the code and
remove the control, and then you can put the code in the code window and
create the control exactly the same way every time. Would that work for
you?
Thanks. I wish it were that easy however :) This is being done at runtime on
a machine where VS isn't even installed. I need to generically clone an
arbitrary control that I'm not familiar with ahead of time. A clone function
taking a "Control" argument and returning the cloned control is what I'm
after.
Feb 22 '07 #3
Hi Rob,

There may be better ways, but this may work

private void button1_Click(object sender, EventArgs e)
{
TextBox t = (TextBox)CloneObject(textBox1);
}

private object CloneObject(object o)
{
Type t = o.GetType();
PropertyInfo[] properties = t.GetProperties();

Object p = t.InvokeMember("", System.Reflection.BindingFlags.CreateInstance, null, o, null);

foreach(PropertyInfo pi in properties)
{
if(pi.CanWrite)
{
pi.SetValue(p, pi.GetValue(o, null), null);
}
}

return p;
}

This code should create a new object of the same type and any writable property will get their values copied.
There may be far better ways though.
On Thu, 22 Feb 2007 02:37:56 +0100, Rob Stevenson <no_spam@_nospam.comwrote:
Does anyone know how to do this accurately. I really only want to clone the
design-time properties which should make the task easier. I've searched high
and low however and still can't find a problem-free solution. Even (ad-hoc)
solutions posted by MSFT employees have problems. For instance, if you
simply copy all serializable properties, you may eventually receive an
"Object does not match target type" exception. In my case, this occurswhen
I encounter (and clone) the "Location" property due to the fact that the
"Site" property was cloned earlier. If I clone "Location" first however
(before "Site"), it works fine. There must be a clean way to do this. Can
anyone offer any insight. Thanks.


--
Happy coding!
Morten Wennevik [C# MVP]
Feb 22 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
5037
by: steve | last post by:
I am quite frustrated with php’s include, as I have spent a ton of time on it already... anyone can tell me why it was designed like this (or something I don’t get)? The path in include is...
99
5998
by: Jim Hubbard | last post by:
It seems that Microsoft not only does not need the classic Visual Basic developer army (the largest army of developers the world has ever seen), but now they don't need ANY Windows developer at a...
8
2089
by: Ravi Ambros Wallau | last post by:
Hey guys: What can I do when an "Error Creating Control" is displayed on the form (instead of the control), and a tooltip indicating the error never is displayed? Is there some log, some hidden...
4
1121
by: Randall Arnold | last post by:
My application addes or deletes tab pahes at the user's discretion. On each tab page are controls that must be duplicated. Here's a sample of my code: chart_tabcontrol.TabPages.Add(1)...
6
2242
by: cefrancke | last post by:
I've read up on Access and the limits of creating visible controls at run-time. I'm using Access 2003 and assume that it cant be done. I did find a clever method of having a main form (and in...
1
1503
by: Kevin L | last post by:
Can anyone tell me the RGB values for the "Control" System Color? Thanks
2
3962
by: Angus | last post by:
I am trying to change the selection in Javascript - but this HTML element is not a standard option control. On the web page it looks like a dropdown list - and you click on the right hand down...
1
1421
by: Joe | last post by:
anyone know how to use COM object (probably build in into Adobe Reader) to open, print and close PDF file? I can always do it through DOS command prompt, but printing 8,000 PDF files a day, we have...
3
2263
by: eskelies | last post by:
Good day all...I have a combo box that selects numbers (ie. 1,2,3, etc.). What I am looking for is the combo box to have an "ALL" selection. Right now this is what I have for my SQL code: SELECT...
0
6912
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7052
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7092
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5348
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3000
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2989
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
565
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
188
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.