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. Get control form its NAME

Get control form its NAME

Scheduled Pinned Locked Moved C#
question
11 Posts 5 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.
  • R Offline
    R Offline
    Radgar
    wrote on last edited by
    #1

    Hi, I get the name of the control in string format in an event: "control1" And I want to modify its properties (for ex.: control1.BackColor = Color.White; How can I convert or point this string to the existing instance of the control?? regards Radgar "Imagination is more important than knowledge." - Albert Einstein

    C M M J 4 Replies Last reply
    0
    • R Radgar

      Hi, I get the name of the control in string format in an event: "control1" And I want to modify its properties (for ex.: control1.BackColor = Color.White; How can I convert or point this string to the existing instance of the control?? regards Radgar "Imagination is more important than knowledge." - Albert Einstein

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Reflection. Christian Graus - Microsoft MVP - C++

      R 1 Reply Last reply
      0
      • C Christian Graus

        Reflection. Christian Graus - Microsoft MVP - C++

        R Offline
        R Offline
        Radgar
        wrote on last edited by
        #3

        thanks Christian but it's a huge namespace for me to solve this issue right now. I tried PropertyInfo and checked the MemberInfo classes but still found no answers. Radgar "Imagination is more important than knowledge." - Albert Einstein

        C 1 Reply Last reply
        0
        • R Radgar

          thanks Christian but it's a huge namespace for me to solve this issue right now. I tried PropertyInfo and checked the MemberInfo classes but still found no answers. Radgar "Imagination is more important than knowledge." - Albert Einstein

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          You're saying that reflection is a huge space to learn, or that you're working in a huge namespace ? Christian Graus - Microsoft MVP - C++

          R 1 Reply Last reply
          0
          • C Christian Graus

            You're saying that reflection is a huge space to learn, or that you're working in a huge namespace ? Christian Graus - Microsoft MVP - C++

            R Offline
            R Offline
            Radgar
            wrote on last edited by
            #5

            I meant the Reflection namespace is huge but still I need to learn that. It's just that it's 5 am and I have a little time to finish what I'm writing. that's all. Anyway, thanks for the help.. I think I should use MemberInfo class and tried smth like this: System.Reflection.MemberInfo[] myMembers = ....GetType().GetMember("pictureBox1"); this.Controls.Remove(myMembers[0]); on it.. Radgar "Imagination is more important than knowledge." - Albert Einstein

            C 1 Reply Last reply
            0
            • R Radgar

              I meant the Reflection namespace is huge but still I need to learn that. It's just that it's 5 am and I have a little time to finish what I'm writing. that's all. Anyway, thanks for the help.. I think I should use MemberInfo class and tried smth like this: System.Reflection.MemberInfo[] myMembers = ....GetType().GetMember("pictureBox1"); this.Controls.Remove(myMembers[0]); on it.. Radgar "Imagination is more important than knowledge." - Albert Einstein

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              No, I don't think that's the way to go. Sorry, I don't know a lot more, but definately reflection is the only way you'll get variables out of names, unless you build a hash table before you start and look it up. Christian Graus - Microsoft MVP - C++

              R 1 Reply Last reply
              0
              • C Christian Graus

                No, I don't think that's the way to go. Sorry, I don't know a lot more, but definately reflection is the only way you'll get variables out of names, unless you build a hash table before you start and look it up. Christian Graus - Microsoft MVP - C++

                R Offline
                R Offline
                Radgar
                wrote on last edited by
                #7

                hmm.. if only there was an equivalent like java's eval function... but no.. thanx for your time Radgar "Imagination is more important than knowledge." - Albert Einstein

                1 Reply Last reply
                0
                • R Radgar

                  Hi, I get the name of the control in string format in an event: "control1" And I want to modify its properties (for ex.: control1.BackColor = Color.White; How can I convert or point this string to the existing instance of the control?? regards Radgar "Imagination is more important than knowledge." - Albert Einstein

                  M Offline
                  M Offline
                  mikker_123
                  wrote on last edited by
                  #8

                  Well friend I don't have fancy solution like reflection but this should do the trick... I bet 90% of people are using it... hey -> not all of us are MVPs ;)... NHF? I'm just kidding ;) foreach (Control c in this.Controls) { if (c.Name == "control1") { // do smthing like c.BackColor = Color.White } } Now if ur's event isn't in form which control u need to modify, than you'll need to pass that form, or it's collection Controls. Drop a line if u need more hints...

                  R 1 Reply Last reply
                  0
                  • M mikker_123

                    Well friend I don't have fancy solution like reflection but this should do the trick... I bet 90% of people are using it... hey -> not all of us are MVPs ;)... NHF? I'm just kidding ;) foreach (Control c in this.Controls) { if (c.Name == "control1") { // do smthing like c.BackColor = Color.White } } Now if ur's event isn't in form which control u need to modify, than you'll need to pass that form, or it's collection Controls. Drop a line if u need more hints...

                    R Offline
                    R Offline
                    Radgar
                    wrote on last edited by
                    #9

                    I thought about this method but I need to do it without loops.. It will slow down the app... thanks anyway. Radgar "Imagination is more important than knowledge." - Albert Einstein

                    1 Reply Last reply
                    0
                    • R Radgar

                      Hi, I get the name of the control in string format in an event: "control1" And I want to modify its properties (for ex.: control1.BackColor = Color.White; How can I convert or point this string to the existing instance of the control?? regards Radgar "Imagination is more important than knowledge." - Albert Einstein

                      M Offline
                      M Offline
                      Mohamad Al Husseiny
                      wrote on last edited by
                      #10

                      you may use reflection to get the type by name and then call InvokeMember

                      1 Reply Last reply
                      0
                      • R Radgar

                        Hi, I get the name of the control in string format in an event: "control1" And I want to modify its properties (for ex.: control1.BackColor = Color.White; How can I convert or point this string to the existing instance of the control?? regards Radgar "Imagination is more important than knowledge." - Albert Einstein

                        J Offline
                        J Offline
                        Joel Lucsy
                        wrote on last edited by
                        #11

                        Not sure if this'll compile, but I've done something similar to this: string controlname = "control1"; string propertyname = "BackColor"; object newvalue = Color.Black; Control control; control = form1.GetType().InvokeMember( controlname, BindingFlags.Instance|BindingFlags.GetField, null, form1, new object[] {} ); control.GetType().InvokeMember( propertyname, BindingFlags.Instance|BindingFlags.SetProperty, null, control, new object[] { newvalue } ); -- Joel Lucsy

                        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