Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. cast string to control.Property

cast string to control.Property

Scheduled Pinned Locked Moved C#
question
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jojoba2010
    wrote on last edited by
    #1

    I have (button1 in my form) string nameProp = "button1.Text"; how can i set this property for button1.

    N S 2 Replies Last reply
    0
    • J jojoba2010

      I have (button1 in my form) string nameProp = "button1.Text"; how can i set this property for button1.

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      Start by reading a book.


      I know the language. I've read a book. - _Madmatt

      J 1 Reply Last reply
      0
      • N Not Active

        Start by reading a book.


        I know the language. I've read a book. - _Madmatt

        J Offline
        J Offline
        jojoba2010
        wrote on last edited by
        #3

        What do you mean ? Can u help?

        1 Reply Last reply
        0
        • J jojoba2010

          I have (button1 in my form) string nameProp = "button1.Text"; how can i set this property for button1.

          S Offline
          S Offline
          Shakeel Iqbal
          wrote on last edited by
          #4

          You can do like this

          // find control
          Control[] controls = this.Controls.Find("button1", true);

          if (controls.Length > 0)
          {

            Control control = controls\[0\];
          
            // get properties
            PropertyInfo\[\] properties = control.GetType().GetProperties();
          
            foreach (PropertyInfo controlProperty in properties)
            {
          
                // check property name
                if (controlProperty.Name == "Text")
                {
                    // set value of control
                    controlProperty.SetValue(control, "My new Value", null);
                }
             }
          

          }

          ----------------------------- Shakeel Iqbal ------------------------------ My New Article Task Manager

          J 1 Reply Last reply
          0
          • S Shakeel Iqbal

            You can do like this

            // find control
            Control[] controls = this.Controls.Find("button1", true);

            if (controls.Length > 0)
            {

              Control control = controls\[0\];
            
              // get properties
              PropertyInfo\[\] properties = control.GetType().GetProperties();
            
              foreach (PropertyInfo controlProperty in properties)
              {
            
                  // check property name
                  if (controlProperty.Name == "Text")
                  {
                      // set value of control
                      controlProperty.SetValue(control, "My new Value", null);
                  }
               }
            

            }

            ----------------------------- Shakeel Iqbal ------------------------------ My New Article Task Manager

            J Offline
            J Offline
            jojoba2010
            wrote on last edited by
            #5

            I know this one! I want something like this : string butt = "button1.Text"; (Button)butt="New Value";

            D 1 Reply Last reply
            0
            • J jojoba2010

              I know this one! I want something like this : string butt = "button1.Text"; (Button)butt="New Value";

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              It can't be done the way you want. You have no choice but to use reflection as was demonstrated.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007, 2008
              But no longer in 2009...

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups