How to create a Control given its type _as string_?

Hello out there,
de-serializing a collection of controls I have code like this to
recreate them:
    Control ctrl;
    switch (type) {
        case "Button": ctrl = new Button(); break;
        case "Label": ctrl = new Label(); break;
        // and loads more of these
    }
Surely there is a "generic" way to create controls given the type -
but I don't arrive at a solution.
Any hints will be greatly appreciated.
Best regards
Helmut Giese
0
Helmut
11/13/2009 1:42:06 PM
dotnet.languages.csharp 1931 articles. 0 followers. Follow

4 Replies
845 Views

Similar Articles

[PageSpeed] 51

"Helmut Giese" <hgiese@ratiosoft.com> wrote in message 
news:u4oqf5978rukj9915gf7v1ssliocb25iti@4ax.com...

> Hello out there,
> de-serializing a collection of controls I have code like this to
> recreate them:
>    Control ctrl;
>    switch (type) {
>        case "Button": ctrl = new Button(); break;
>        case "Label": ctrl = new Label(); break;
>        // and loads more of these
>    }
> Surely there is a "generic" way to create controls given the type -
> but I don't arrive at a solution.
> Any hints will be greatly appreciated.

Look up "Activator.CreateInstance method".


0
Jeff
11/13/2009 2:46:49 PM
On Nov 13, 8:42=A0am, Helmut Giese <hgi...@ratiosoft.com> wrote:
> Hello out there,
> de-serializing a collection of controls I have code like this to
> recreate them:
> =A0 =A0 Control ctrl;
> =A0 =A0 switch (type) {
> =A0 =A0 =A0 =A0 case "Button": ctrl =3D new Button(); break;
> =A0 =A0 =A0 =A0 case "Label": ctrl =3D new Label(); break;
> =A0 =A0 =A0 =A0 // and loads more of these
> =A0 =A0 }
> Surely there is a "generic" way to create controls given the type -
> but I don't arrive at a solution.
> Any hints will be greatly appreciated.
> Best regards
> Helmut Giese

You can use AppDomain.CreateInstance or better
Activator.CreateInstance , go and check the different parameters those
methods need.
0
Ignacio
11/13/2009 3:13:53 PM
Jeff and Ignacio,
thanks, this looks promising.
I see that there is a variant of CreateInstance which takes a string,
but (needed in a different context) a follow-up question:
	How to go from a string "Button" to the type 'Button'?
Best regards
Helmut Giese
0
Helmut
11/13/2009 8:39:36 PM
"Helmut Giese" <hgiese@ratiosoft.com> wrote in message 
news:hpgrf55ekq5svilnjj6l8qgjp5vq99fahn@4ax.com...

> thanks, this looks promising.
> I see that there is a variant of CreateInstance which takes a string,
> but (needed in a different context) a follow-up question:
> How to go from a string "Button" to the type 'Button'?

Easy: don't use "Button"; ALWAYS use "System.Windows.Forms.Button". 


0
Jeff
11/13/2009 10:18:13 PM
Reply:

Similar Artilces:

A Static linked control problem
Hello Everyone, I have a problem with static-linked library in my MFC based project. The structure of the projects is like this: MainControl project contains MainControl.cpp and mainControl.h HeadApp.project contains all application files, and the AppDoc class here (created by MFC wizard) contains the MainControl object: CMainCtrl *m_control; It compiles fine intill I try to create the control on Runtime: m_control = new CMainCtrl(); Here I get an unresolved external symbol error, even if I write the exact path to the MainControl header file.. What's wrong? Thanks for your time.. ...

PUB2003 creates text errors in 2000 doc
I have produced a manual in PUB2000 and now that I am using 2003, it prints errors which do not show on the screen -- only in Preview and in Printing. It has inserted numbers occasionally and randomly throughout. Usually these numbers are at the left of a text box, and often correspond to the approximate page number. They are in different font than the correct text and not in line with the text. They are not in the background or anywhere accessible. They just appear on the printed copy and the preview. Has anyone got any idea on how to correct this? Check with the manufacturer'...

How do I create a running report on Excel
I am trying to create a running production census for a medical facility. I just need to know how to get the data to automaticly contine with the ongoing numbers. If your data starts in A2, put the following formula in B2 and copy it down your sheet. =A2+B1 HTH, Gary Brown "Michellle M" <MichellleM@discussions.microsoft.com> wrote in message news:47144A4B-A4A6-4B5B-B9E1-20BD06743793@microsoft.com... >I am trying to create a running production census for a medical facility. I > just need to know how to get the data to automaticly contine with the > ongoing >...

hyperlinks in CRichEdit controls?
Does anyone know of an easy way to place hyperlinks into a CRichEdit control? ...

Exchange 2007 recipient policies create duplicate addresses
Bug report: The Exchange 2007 recipient policies will gladly create two objects with the same address. We get no indication of the situation until senders inform us that their messages to the address that was duplicated are bouncing. This is aggravated by the aggressive enforcement of recipient policies in Exchange 2007. We hope you're able to fix this as soon as possible. Thanks ...

Create files and performance design
Hi, I've a MFC VC++ 6 program which needs to be updated to write out image (jpg, 640x480) files at a rate of 1s. What is the best approach in designing the writer class, such that, it does not block the main thread while the images are written onto disk? My initial thought is the put the Writer class onto a separate thread? What are my alternatives? >My initial thought is the put the Writer class onto a separate thread? What >are my alternatives? Overlapped IO - see the FILE_FLAG_OVERLAPPED option of the CreateFile API. Dave You have a couple choices here. One is to ...

Chart Types #2
Are there any downloadable Types of Charts in EXCEL OfficeXP probably 3D Charts. I want to use other types in my Annual Report Project. Thanks. Hi you may have a look at a third party add-in for this. One is the following http://www.crystalgraphics.com/presentations/charts.main.asp there are many others. Try a Google search (though most well not be free of charge) -- Regards Frank Kabel Frankfurt, Germany MELL wrote: > Are there any downloadable Types of Charts in EXCEL > OfficeXP probably 3D Charts. I want to use other types > in my Annual Report Project. Thanks. You could...

How do I show a negative control as a positive number
I have a calculated control on a report where the value is shown as -45. How can I format this number to show it as 45? Thanks Tony -- Why don't my grey cells communicate with each as fast as they used to? I hate getting old! Message posted via http://www.accessmonster.com "TonyWilliams via AccessMonster.com" <u56994@uwe> wrote in message news:a219779e31378@uwe... > I have a calculated control on a report where the value is shown as -45. How > can I format this number to show it as 45? > Thanks > Tony =Abs([FieldName]) where FieldNa...

Create 10-up business card page?
I created a business card in Publisher 2000 using the single card template. I've got the instructions for printing this as "10-up" using the Page Setup (I think - I've got it printed out but not handy). Is there any way, though, to convert this to a single 8 1/2 x 11 page with multiple cards on it? Would everything stay "preserved" if I simply did a Select All, Copy, and Paste onto a blank full page? Thank you. Ed Ed <ed_millis@NO_SPAM.yahoo.com> was very recently heard to utter: > I created a business card in Publisher 2000 using the single card >...

Create contract lines from Order
Hi ! I want to create contract line automatically after Order creating. What is best practics for it? Please explain which customization i need to do. Thanks. Nobody can not help? What about relationship system-custom-system, order-custom entity-contract,can some body explain? Thanks is_vlb50@hotmail.com wrote: > Hi ! > I want to create contract line automatically after Order creating. > What is best practics for it? Please explain which customization i need > to do. > Thanks. I think I found solution: http://www.tconv.com/blogs/crm3tips/2005/11/creating-many-to-many-relati...

Combining 2 different types of bar graph
Hi I have 3 series of sales data I need to plot - actual, max budget & min budget - for 12 months. With the months being the x axis, I need the graph to be a standard bar chart, with the colums side by side for each month (clustered column). However, for the 2 bdget columns I need them to be of the "stacked" column type - so that when the graph is presented we can see exactly how the actual column each month compares to both budget figures. I've tried changing the chart type for each of the series, but it seems to change all 3 series each time. I could easily combin...

Rich edit control
I am mainly interested in using only the StreamIn() and StreamOut() members of CRichEditCtrl. Can I have this control inside a DLL without the UI? Is it possible to use other member functions also (may be other than the UI related ones)? Any pointers to samples (if available) please? (BTW, why this newsgroup has become silent? why nobody is answering here?) What do you mean by "other than UI related ones". Everything a CRichEditCtrl does is UI releated. In order to use any of it's methods you will have to create the CRichEditCtrl, and that requires a parent window. AliR...

create xslt stylesheet in code
Hi, As a follow on from an earlier post I have another question about xslt. Is it possible to create the stylsheet programatically? Is this sensible? In the first phase I needed to map element name from inbound xml to my internal elements to standardize disparate input. Now I could just create an xslt stylesheet for each possible inbound format and be done, but I think it would be powerful to be able store this mapping in a database and programatically create the stylsheet. This way I don't have to maintain potentially a great number of stylesheets. The trouble is, I can't find an...

Problem Creating MailBoxes #2
Hello, Thankyou for helping me out. I followed your advice and used the troubleshooting guide. I have ascertained that it is a security issue as the query returned 0 results in events 8012 each time I created a new user with a mailbox. However I cant see what security setting I am missing. My Exchange server is a member of Domain Computers, Exchange Domain Servers & Exchange Enterprise Servers. Where my Domain Controller is a member of Domain Controllers & Exchange Domain Servers. What security permissions am I missing? Please note that we are only running the one domain controller an...

creating relationship between entity case and entity quote.
Hi everybody I am trying to create a relationship between entity case and entity quote. But I cannot able to do it. Is it Possible anyway. Regards Thanks in advance. -- Jaber You can't do this, unfortunately; CRM does not allow you to create new relationships between system entities. It depends on your data model, but one option might be to create a custom entity that has a relationship to both the case and the quote -- David Jennaway - Microsoft Dynamics CRM MVP Web: http://www.excitation.co.uk "Jaber" wrote: > Hi everybody > I am trying to create a relationship ...

"Office Update ActiveX Control Failed to Load"
I keep getting "Office Update ActiveX Control Failed to Load." I cannot get ActiveX to load for Office update even though I've carefully followed ALL the directions on the MS web site (http://office.microsoft.com/en-us/assistance/HA011357171033.aspx) . Can anyone suggest what I might do next? Thanks and regards, PJF On Sun, 07 Aug 2005 15:37:23 -1000, PJF <pjf10@earthlink.net> wrote: > I keep getting "Office Update ActiveX Control Failed to Load." I cannot > get > ActiveX to load for Office update even though I've carefully followed ALL > t...

Subclassing a Dialog Control ?
Hi guys, When I put homemade controls in my dialog boxes I usually just use ClassWizard to create a member variable of the control type of my derived class, for example, I create a custom edit box derived from CEdit and ClassWizard recognizes it. Is there a way I can do it without having to have member variables of my derived class, so I can use GetDlgItem() ? I really only want to do this for consistency because for my other controls I don't have dialog member variables (at least not declared explicitly). I think it's called subclassing but am probably wrong. Thanks. "Robe...

working with groups in Excel and creating combinations
I have the following problem with excel. I have a varied amount of groups of names and each group contains a different amount of names. I need excel to list all the possible combinations from each group. The combination size also varies. EG Group 1 Group 2 Group 3 Dave Fred Janet Harry Bob Selina Jim jane Jo The group sizes vary and the amount of group vary. I need excel to make various combinations eg In a "2" combination Dave can go with Fred or Janet. Fr...

Creating an Account from a contact
Hi I have a customer who wants to automatically create an account when the contact is created, and to make the contact the Primary Contact for that account. (The scenario is that the contact phones, and so is created, but the account is ready for other contacts to be linked to. In this implementation, the only role of the account is as a container to group contacts into families.) I've looked at doing it through workflow (post url), which seems not to work. Has anyone done this? Thanks Gill Try having them create a Lead instead and then convert the Lead to an Account and Co...

How do I control the location of xmlns attributes when serializing
I need to generate an XML document that will be validated against a DTD. Up to this point, I have been using XML Serialization with good results, but there is one item I haven't been able to figure out. The generated XML needs to look something like this: <?xml version="1.0" encoding="utf-8" standalone="no"?> <!DOCTYPE MY_XML_Doc SYSTEM "http://dtd_location.com/abc.dtd"[]> <MY_XML_Doc attr1="val"> <Element1> <SubElement1 xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href=http://compan...

how do i type infomation on a pdf application
how do i type infomation on a pdf application Typewriter? "pete" <pete@discussions.microsoft.com> wrote in message news:41C9A123-C830-47FE-89CA-2CFF188C8F91@microsoft.com... > how do i type infomation on a pdf application "pete" <pete@discussions.microsoft.com> wrote in message news:41C9A123-C830-47FE-89CA-2CFF188C8F91@microsoft.com... > how do i type infomation on a pdf application Some pdfs are set up to allow input fields. Others aren't. Depends on how the author set it up. -- John Blessing http://www.LbeHelpdesk.com - Help Desk softwa...

Setting up flat file for IM
I am setting up a new integration between our payroll (outside vendor) and GP. I need to use the Transaction Type field for standard and reversing. I do not know what to tell them to but in this field to identify the difference between standard and reversing. Clinton, Most payroll transactions are integrated into GP as Standard GL transactions -- you can have the field in IM set to Use Default as that is the default GL transaction type. A reversing entry will be used whenever you have GL transactions that need to be backed out in the following period, such as certain accruals, and ...

Typing in Cells
How do you make the cells remember - Like I start to type 'Phoenix' and the word just pops in like it remembered the word. Is this random or does it have to see a pattern? "jeanngoodyear" <jeanngoodyear@discussions.microsoft.com> wrote: > How do you make the cells remember - Like I start to type 'Phoenix' and > the > word just pops in like it remembered the word. Is this random or does it > have to see a pattern? In Excel 2003, this is controlled by the AutoComplete option under Tools > Options > Edit. Not random. Cont...

Make a cell blank given a code
I am trying to achieve a blank cell which currently has a formula relating to a different sheet. On sheet named "week" In cell M32 I have the following code =IF(OR(D33>0,G33>0),'Stats-Job'!M9,) However if the code M is in another cell, at say H32, then I want the cell M32 to remain blank, Any help appreciated. Mel just wrap in another if =if(h32="M","", yourformula) -- Don Guillett SalesAid Software dguillett1@austin.rr.com "Mel" <mgsg@bigpond.net.au> wrote in message news:ukfmit1oGHA.1600@TK2MSFTNGP04.ph...

How are you creating reports on file (viewable)?
I would like to know from whoever is creating reports on file (viewable) what methods they are using? Presently I am either writing plain reports to text files or when formatting and shapes such as rectangles are required, I am using RTF files. RTF files are cumbersome to work with and very time consuming. I am looking for alternatives that do not require 3rd party software. Thanks, Charles. "Charles C." <binstar@aei.ca> wrote in message news:Ow1ylxlHEHA.2720@TK2MSFTNGP11.phx.gbl... > I would like to know from whoever is creating reports on file (viewable) > what ...