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. Simple question about controls

Simple question about controls

Scheduled Pinned Locked Moved C#
tutorialquestioncsharp
9 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.
  • B Offline
    B Offline
    bouli
    wrote on last edited by
    #1

    Hello gurus, I have a simple question for C# masters concerning controls. I have a series of label with a formated name. In a loop, I want to recover a pointer on the control (a label) by giving its name as a string. for example: Label lbl = null; string lblName = ""; for (int i=0; i<5; i++) { lblName = "lblL" + i.ToString(); // lblName will contain "lblL0", "lblL1" etc. // here is what I'm looking for... lbl = ???; // the label is supposed to point on the label with the built name contained in the variable lblName. // now the lbl variable points on the right control with name given by the variable lblName lbl.Text = i.ToString(); } How to make the control pointing to the right one given its name? I hope you understood my question in my poor english. Best regards. Fred.

    There is no spoon.

    R Y M 3 Replies Last reply
    0
    • B bouli

      Hello gurus, I have a simple question for C# masters concerning controls. I have a series of label with a formated name. In a loop, I want to recover a pointer on the control (a label) by giving its name as a string. for example: Label lbl = null; string lblName = ""; for (int i=0; i<5; i++) { lblName = "lblL" + i.ToString(); // lblName will contain "lblL0", "lblL1" etc. // here is what I'm looking for... lbl = ???; // the label is supposed to point on the label with the built name contained in the variable lblName. // now the lbl variable points on the right control with name given by the variable lblName lbl.Text = i.ToString(); } How to make the control pointing to the right one given its name? I hope you understood my question in my poor english. Best regards. Fred.

      There is no spoon.

      R Offline
      R Offline
      RyanMorris
      wrote on last edited by
      #2

      Hi Fred, Would the following solve your problem:

      lbl = new Label();
      lbl = (Label)FindControl("lblName");

      Ryan

      B 1 Reply Last reply
      0
      • B bouli

        Hello gurus, I have a simple question for C# masters concerning controls. I have a series of label with a formated name. In a loop, I want to recover a pointer on the control (a label) by giving its name as a string. for example: Label lbl = null; string lblName = ""; for (int i=0; i<5; i++) { lblName = "lblL" + i.ToString(); // lblName will contain "lblL0", "lblL1" etc. // here is what I'm looking for... lbl = ???; // the label is supposed to point on the label with the built name contained in the variable lblName. // now the lbl variable points on the right control with name given by the variable lblName lbl.Text = i.ToString(); } How to make the control pointing to the right one given its name? I hope you understood my question in my poor english. Best regards. Fred.

        There is no spoon.

        Y Offline
        Y Offline
        Yusuf
        wrote on last edited by
        #3

        Is this[^] what you looking?

        Yusuf Oh didn't you notice, analogous to square roots, they recently introduced rectangular, circular, and diamond roots to determine the size of the corresponding shapes when given the area. Luc Pattyn[^]

        B 1 Reply Last reply
        0
        • R RyanMorris

          Hi Fred, Would the following solve your problem:

          lbl = new Label();
          lbl = (Label)FindControl("lblName");

          Ryan

          B Offline
          B Offline
          bouli
          wrote on last edited by
          #4

          Yes, that's it, in a recursive way and for WinForms.

          There is no spoon.

          modified on Thursday, March 12, 2009 10:41 AM

          R 1 Reply Last reply
          0
          • Y Yusuf

            Is this[^] what you looking?

            Yusuf Oh didn't you notice, analogous to square roots, they recently introduced rectangular, circular, and diamond roots to determine the size of the corresponding shapes when given the area. Luc Pattyn[^]

            B Offline
            B Offline
            bouli
            wrote on last edited by
            #5

            Something like that but for WinForms...

            There is no spoon.

            Y 1 Reply Last reply
            0
            • B bouli

              Yes, that's it, in a recursive way and for WinForms.

              There is no spoon.

              modified on Thursday, March 12, 2009 10:41 AM

              R Offline
              R Offline
              RyanMorris
              wrote on last edited by
              #6

              Sorry, my mistake. How about something like this:

              Control myControl = new Control();
              foreach (Control c in this.Controls)
              {
              if (c.Name == "lblName")
              {
              myControl = c;
              }
              }

              1 Reply Last reply
              0
              • B bouli

                Something like that but for WinForms...

                There is no spoon.

                Y Offline
                Y Offline
                Yusuf
                wrote on last edited by
                #7

                The is no FindContol method in WinForm. You can implenet your own for this to work the control names need to be unique.

                public static Control FindControl(string controlName)
                {
                if (!controlName.Empty)
                return this.Controls.Find(controlName, true)[0];

                return null;

                }

                Yusuf Oh didn't you notice, analogous to square roots, they recently introduced rectangular, circular, and diamond roots to determine the size of the corresponding shapes when given the area. Luc Pattyn[^]

                B 1 Reply Last reply
                0
                • B bouli

                  Hello gurus, I have a simple question for C# masters concerning controls. I have a series of label with a formated name. In a loop, I want to recover a pointer on the control (a label) by giving its name as a string. for example: Label lbl = null; string lblName = ""; for (int i=0; i<5; i++) { lblName = "lblL" + i.ToString(); // lblName will contain "lblL0", "lblL1" etc. // here is what I'm looking for... lbl = ???; // the label is supposed to point on the label with the built name contained in the variable lblName. // now the lbl variable points on the right control with name given by the variable lblName lbl.Text = i.ToString(); } How to make the control pointing to the right one given its name? I hope you understood my question in my poor english. Best regards. Fred.

                  There is no spoon.

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

                  you dont need to loop the controls, you can simple access it like an array and specify the control name, i.e. Label lbl = (Label)Form1.Controls[lblName];

                  Life goes very fast. Tomorrow, today is already yesterday.

                  1 Reply Last reply
                  0
                  • Y Yusuf

                    The is no FindContol method in WinForm. You can implenet your own for this to work the control names need to be unique.

                    public static Control FindControl(string controlName)
                    {
                    if (!controlName.Empty)
                    return this.Controls.Find(controlName, true)[0];

                    return null;

                    }

                    Yusuf Oh didn't you notice, analogous to square roots, they recently introduced rectangular, circular, and diamond roots to determine the size of the corresponding shapes when given the area. Luc Pattyn[^]

                    B Offline
                    B Offline
                    bouli
                    wrote on last edited by
                    #9

                    Ok, got it :) Thanks.

                    There is no spoon.

                    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