loop through variables?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aas4mis
    New Member
    • Jan 2008
    • 97

    loop through variables?

    In this for loop is it possible to reference a different label with each iteration?

    c#
    Code:
     for (x = 1;x <=3; x++) 
    {
    label & x.tostring & .visible = true;
    }
    Sorry for the mess of code. That was the only way I know how to get my point across. I'm used to php and vb, my c# is embarrassing.
    Last edited by DrBunchman; Jun 17 '08, 09:07 PM. Reason: Added code tags - Please use the # button
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You can loop through the controls inside an object (like a form or page) and check their .Name property?
    I think is also a .FindControl(st ring) function that can do similar.

    Comment

    • aas4mis
      New Member
      • Jan 2008
      • 97

      #3
      Originally posted by Plater
      You can loop through the controls inside an object (like a form or page) and check their .Name property?
      I think is also a .FindControl(st ring) function that can do similar.
      this.FindContro l() doesn't show as a valid command. I'm able to get what I need by the following:
      [code=c#]
      if (label1.Visible == true)

      {

      label1.Visible = false;

      label2.Visible = true;

      label3.Visible = false;

      }

      else if (label2.Visible == true)

      {

      label1.Visible = false;

      label2.Visible = false;

      label3.Visible = true;

      }

      else if (label3.Visible == true)

      {

      label1.Visible = true;

      label2.Visible = false;

      label3.Visible = false;

      }
      [/code]There has to be a better way of getting this done? Any suggestions? This is done after a button click.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        this.FindContro l(myString) worked for me.
        Are you in a windows application and not a web application maybe?

        Comment

        • aas4mis
          New Member
          • Jan 2008
          • 97

          #5
          Originally posted by Plater
          this.FindContro l(myString) worked for me.
          Are you in a windows application and not a web application maybe?
          yes, I'm in windows application

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            ah ha! You had said you came from php so I fugred you must be doing a webbased app (well I guess there's TK and console uses for php too)
            You would have to loop through the controls checking the name property then.

            Comment

            • aas4mis
              New Member
              • Jan 2008
              • 97

              #7
              Originally posted by Plater
              ah ha! You had said you came from php so I fugred you must be doing a webbased app (well I guess there's TK and console uses for php too)
              You would have to loop through the controls checking the name property then.
              My c# abilities are poor. I like the IDE and intellisense that VS has and want to start learning c# instead of improving on my VB. Would you mind showing me a loop for controls?

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Something like this:
                [code=c#]private Control FindControl(Con trol startcontrol, string ControlName)
                {
                Control retval = null;
                foreach (Control c in startcontrol.Co ntrols)
                {
                if (c.Name.Equals( ControlName))
                {
                retval = c;
                break;
                }
                if (c.HasChildren)
                {
                Control temp = FindControl(c, ControlName);
                if (temp != null)
                {
                retval = temp;
                break;
                }
                }
                }
                return retval;
                }
                [/code]

                Comment

                Working...