473,387 Members | 1,388 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 46095
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
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
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
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
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
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
by: Kevin L | last post by:
Can anyone tell me the RGB values for the "Control" System Color? Thanks
2
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
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

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.