Windows form - Label

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fayazmd
    New Member
    • Jul 2007
    • 41

    Windows form - Label

    Hi,

    I am stuck with a problem on labels. I have 30 labels on my from. And from one method i am returning a value, based on it i have to make one label as invisible.

    I took switch - case, but i am guided to take object of that label and make it invisible.

    I followed this code, but it is throwing error.

    Label ToHide = new Label();
    ToHide = Convert.ChangeT ype("Label"+ind , TypeCode.Object );
    (ToHide as Label).Visible = false;

    Here ind is int returning from one method, and i am concatinating it to get label name.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You would need to get the instance of that control first and not just create a new label.
    Try something like:
    Code:
    Label ToHide;
    ToHide =this.Controls.Find("Label" + ind, true);
    if(ToHide!=null)
    {//it found your label
       (ToHide as Label).Visible = false;
    }

    Comment

    • fayazmd
      New Member
      • Jul 2007
      • 41

      #3
      Thanks for your reply.
      I am sorry, I forgot to mention. I am working in .net 2003.
      Where i couldn't find 'this.Controls. Find'. Please suggest me how can i proceed.


      Originally posted by Plater
      You would need to get the instance of that control first and not just create a new label.
      Try something like:
      Code:
      Label ToHide;
      ToHide =this.Controls.Find("Label" + ind, true);
      if(ToHide!=null)
      {//it found your label
         (ToHide as Label).Visible = false;
      }

      Comment

      • jjvainav
        New Member
        • Feb 2008
        • 25

        #4
        Here is another way you can do this without using the Controls.Find method:

        Code:
        string labelToHide = "Label" + ind;
        if(this.Controls.ContainsKey(labelToHide))
        {
            this.Controls[labelToHide].Visible = false;
        }
        A word of caution, the this.Controls.C ontainsKey method does not do a recursive search like the Controls.Find method does. So if your labels are added to a container, such as a panel, then the this.Controls.C ontainsKey method will return false.

        Originally posted by fayazmd
        Hi,

        I am stuck with a problem on labels. I have 30 labels on my from. And from one method i am returning a value, based on it i have to make one label as invisible.

        I took switch - case, but i am guided to take object of that label and make it invisible.

        I followed this code, but it is throwing error.

        Label ToHide = new Label();
        ToHide = Convert.ChangeT ype("Label"+ind , TypeCode.Object );
        (ToHide as Label).Visible = false;

        Here ind is int returning from one method, and i am concatinating it to get label name.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Then I guess you will need to make a mimic of this function:

          Code:
          private Control Find(Control startpoint, string ControlName, bool Recurse)
          {
          	foreach (Control c in startpoint.Controls)
          	{
          	  if (c.Name == ControlName)
          	  {
          	  	return c;
          	  }
          	  if ((Recurse) & (c.HasChildren))
          	  {
          	    Control temp=Find(c, ControlName, Recurse);
          	    if (temp != null)
          	    {
          	      return temp;
          	    }
          	  }
          	}
          return null;
          }
          Then you can use it like:
          Code:
          Control myc=Find(this,"Label"+ind,true);
          if(myc!=null)
          {//found the control
          }

          Comment

          • fayazmd
            New Member
            • Jul 2007
            • 41

            #6
            Thanks, it's working.


            Originally posted by Plater
            Then I guess you will need to make a mimic of this function:

            Code:
            private Control Find(Control startpoint, string ControlName, bool Recurse)
            {
            	foreach (Control c in startpoint.Controls)
            	{
            	  if (c.Name == ControlName)
            	  {
            	  	return c;
            	  }
            	  if ((Recurse) & (c.HasChildren))
            	  {
            	    Control temp=Find(c, ControlName, Recurse);
            	    if (temp != null)
            	    {
            	      return temp;
            	    }
            	  }
            	}
            return null;
            }
            Then you can use it like:
            Code:
            Control myc=Find(this,"Label"+ind,true);
            if(myc!=null)
            {//found the control
            }

            Comment

            • fayazmd
              New Member
              • Jul 2007
              • 41

              #7
              Hi Plater,

              Nice method. It replaced my lines of coding to few lines.

              I understood the method that you are looking for each control and control's name against all controls in your form. But, i didn't get recurse and has children part. This block of code

              if ((Recurse) & (c.HasChildren) )
              {
              Control temp=Find(c, ControlName, Recurse);
              if (temp != null)
              {
              return temp;
              }
              }

              Could you please explain me this block. Thanks in advance.


              Originally posted by Plater
              Then I guess you will need to make a mimic of this function:

              Code:
              private Control Find(Control startpoint, string ControlName, bool Recurse)
              {
              	foreach (Control c in startpoint.Controls)
              	{
              	  if (c.Name == ControlName)
              	  {
              	  	return c;
              	  }
              	  if ((Recurse) & (c.HasChildren))
              	  {
              	    Control temp=Find(c, ControlName, Recurse);
              	    if (temp != null)
              	    {
              	      return temp;
              	    }
              	  }
              	}
              return null;
              }
              Then you can use it like:
              Code:
              Control myc=Find(this,"Label"+ind,true);
              if(myc!=null)
              {//found the control
              }

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                The reason you have to check for children is that the Controls is really a "tree" heirarchy.
                Say you have a form called Form1
                Then you add a Texbox tb1 and a groupbox gb1.
                In the group box you stick two labels lb1 and lb2

                Form1.Controls will contain:
                tb1
                gb1

                gb1 will "HasChildre n"
                and gb1.Controls will contain
                lb1
                lb2

                So just searching in Form1.Controls will not contain child controls of it's children.
                Therefor I recurse(if you set the boolean to true) through the children to look for other children to see if it's a match.

                Comment

                • fayazmd
                  New Member
                  • Jul 2007
                  • 41

                  #9
                  Thank you once again for explanation.

                  Originally posted by Plater
                  The reason you have to check for children is that the Controls is really a "tree" heirarchy.
                  Say you have a form called Form1
                  Then you add a Texbox tb1 and a groupbox gb1.
                  In the group box you stick two labels lb1 and lb2

                  Form1.Controls will contain:
                  tb1
                  gb1

                  gb1 will "HasChildre n"
                  and gb1.Controls will contain
                  lb1
                  lb2

                  So just searching in Form1.Controls will not contain child controls of it's children.
                  Therefor I recurse(if you set the boolean to true) through the children to look for other children to see if it's a match.

                  Comment

                  Working...