nameing the labels

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Haneen
    New Member
    • Aug 2010
    • 3

    nameing the labels

    Hi, This is haneen :)

    I face a problem with labels
    I have a for loop and I want to pass over 15 labels like this:

    Code:
    string[15] name = new string [15];
    for(int i=0;i<15 ;i++)
    {
      labeli=name[i];
    }
    I can't use the variable i here .... so what can i do ??

    thanx
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    How does this
    Code:
    string[15] name = new string [15];
    compile?

    Comment

    • Haneen
      New Member
      • Aug 2010
      • 3

      #3
      sorry i've done a mistake..

      Code:
      string[] s = new string[15];

      Comment

      • newb16
        Contributor
        • Jul 2008
        • 687

        #4
        Still no luck. Is it java or c#?

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          If you have 15 labels, then why is the variable named labeli? Do you mean label[i] ?

          Comment

          • Haneen
            New Member
            • Aug 2010
            • 3

            #6
            It's C#
            ...

            I know I can't write labeli here in this code but i did so to declare what i need
            I need a way so that I can pass over the 15 label using there names

            instead of writing

            Code:
            label1.text="one";
            label2.text="two";
            ..
            I need to do this with a for loop like
            Code:
            for(int i=0;i<15;i++)
            {
              label i = "some thing";
            }
            but as I said I can't use labeli here ...
            if you know a way to do so please tell me

            thanx for being patient

            Comment

            • Joseph Martell
              Recognized Expert New Member
              • Jan 2010
              • 198

              #7
              Each form has a collection of controls that keeps track of everything that you have added to the form, including all of your labels. You can iterate through your form's control collection, checking each control to see if it is a label. When you find a label, you will have to cast the control to a label and then you can handle it as you please.

              Code:
              foreach (Control tempControl in this.Controls)
              {
                  if (tempControl.GetType() == typeof(System.Windows.Forms.Label))
                  {
                      tempLabel = (Label)tempControl;
                      //handle the label here
                  }
              }
              This is allows you to handle as many labels as you please, but is not terribly efficient.

              You could also create a collection for labels in the form and add the labels to the collection one at a time during the form's constructor or load event. Then you would be able to do something like

              Code:
              label[i].text = string[i];

              Comment

              Working...