Results 1 to 7 of 7

Thread: Cast a string to the control type

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    england
    Posts
    598

    Cast a string to the control type

    Hi,

    I dont know if this is possible or not but if it is could someone show me how to do it please?

    I have a string which is "System.Windows.Forms.Button".

    How can I turn that string into a button?

    Thanks

    Loftty

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Cast a string to the control type

    You could use Activator.CreateInstance, but you will get an ObjectHandle reference returned, so you'd still have to unwrap and cast it to access members of the Button class. There seem to have been a lot of questions lately on using strings to specify types. More often than not if you find yourself in that situation it's through bad design rather than necessity. Where has this string come from in the first place? It may be that you don't need to do it that way.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    england
    Posts
    598

    Re: Cast a string to the control type

    Hi,

    The string came from a xml file, as I am saving all the controls on a form to xml but when I read the file back in I dont want a switch statement for each diffrent control type.

    Thanks

    Loftty

  4. #4
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Cast a string to the control type

    Quote Originally Posted by loftty
    Hi,

    The string came from a xml file, as I am saving all the controls on a form to xml but when I read the file back in I dont want a switch statement for each diffrent control type.

    Thanks

    Loftty
    Why would save them in the XML File?

    Do you mean to say that you are saving the properties in the XML file?
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    england
    Posts
    598

    Re: Cast a string to the control type

    Yes I am saving the properties to xml plus the control type.

    Thanks

    Loftty

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Cast a string to the control type

    You cannot just turn a String into a Type. Like I said, even if you use CreateInstance you will still only get an ObjectHandle from it. You can then Unwrap that to get an Object. You could confidently cast that as a Control, but you'd still need to use reflection to set any properties specific to the actual type. It sounds to me like you should investigate serialization. That's the in-built mechanism for saving objects to XML.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Cast a string to the control type

    I believe there is a XUL parser for Mono (I could be wrong here as I've never done anything with Mono). It's somewhat creating a component base from the xml file from somewhere. And oh, I don't know if there are any third party stuff for .Net on this one.

    http://xul.sourceforge.net/post/2003...l_for_uis.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width