13 Replies - 993 Views - Last Post: 13 August 2011 - 08:29 AM Rate Topic: -----

#1 wuu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 85
  • Joined: 16-March 09

How to put code in a loop?

Posted 13 August 2011 - 04:24 AM

Is there a way to put my code in some kind of loope?

            Label1.Text = Convert.ToString(SoncnaElektrarna.Instance.UrnaEnergijaSC[0]);
            Label2.Text = Convert.ToString(SoncnaElektrarna.Instance.UrnaEnergijaSC[1]);
            Label3.Text = Convert.ToString(SoncnaElektrarna.Instance.UrnaEnergijaSC[2]);
            Label4.Text = Convert.ToString(SoncnaElektrarna.Instance.UrnaEnergijaSC[3]);
            Label5.Text = Convert.ToString(SoncnaElektrarna.Instance.UrnaEnergijaSC[4]);
            Label6.Text = Convert.ToString(SoncnaElektrarna.Instance.UrnaEnergijaSC[5]);
            Label7.Text = Convert.ToString(SoncnaElektrarna.Instance.UrnaEnergijaSC[6]);
            Label8.Text = Convert.ToString(SoncnaElektrarna.Instance.UrnaEnergijaSC[7]);
            Label9.Text = Convert.ToString(SoncnaElektrarna.Instance.UrnaEnergijaSC[8]);
            Label10.Text = Convert.ToString(SoncnaElektrarna.Instance.UrnaEnergijaSC[9]);
            Label11.Text = Convert.ToString(SoncnaElektrarna.Instance.UrnaEnergijaSC[10]);
            Label12.Text = Convert.ToString(SoncnaElektrarna.Instance.UrnaEnergijaSC[11]);
            Label13.Text = Convert.ToString(SoncnaElektrarna.Instance.UrnaEnergijaSC[12]);
            Label14.Text = Convert.ToString(SoncnaElektrarna.Instance.UrnaEnergijaSC[13]);
            Label15.Text = Convert.ToString(SoncnaElektrarna.Instance.UrnaEnergijaSC[14]);
            Label16.Text = Convert.ToString(SoncnaElektrarna.Instance.UrnaEnergijaSC[15]);
            Label17.Text = Convert.ToString(SoncnaElektrarna.Instance.UrnaEnergijaSC[16]);
            Label18.Text = Convert.ToString(SoncnaElektrarna.Instance.UrnaEnergijaSC[17]);
            Label19.Text = Convert.ToString(SoncnaElektrarna.Instance.UrnaEnergijaSC[18]);
            Label20.Text = Convert.ToString(SoncnaElektrarna.Instance.UrnaEnergijaSC[19]);
            Label21.Text = Convert.ToString(SoncnaElektrarna.Instance.UrnaEnergijaSC[20]);
            Label22.Text = Convert.ToString(SoncnaElektrarna.Instance.UrnaEnergijaSC[21]);
            Label23.Text = Convert.ToString(SoncnaElektrarna.Instance.UrnaEnergijaSC[22]);
            Label24.Text = Convert.ToString(SoncnaElektrarna.Instance.UrnaEnergijaSC[23]);



Is This A Good Question/Topic? 0
  • +

Replies To: How to put code in a loop?

#2 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: How to put code in a loop?

Posted 13 August 2011 - 05:36 AM

Put your label objects into an array and loop i from 0 to 24.
Was This Post Helpful? 1
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: How to put code in a loop?

Posted 13 August 2011 - 06:14 AM

You can also use the Find method to find controls with a certain name. Something like this...

string labelName = string.Empty;

for (int i = 0; i < SoncnaElektrarna.Instance.UrnaEnergijaSC.Length; i++)
{
   int labelIndex = i + 1;
   labelName = "Label" + labelIndex.ToString();

   // returns as an array
   Control[] array = this.Controls.Find(labelName, true);
 
   // makes sure a control was found
   if (array.Length > 0)
   {
      Label myLabel = array[0] as Label;
      myLabel.Text = SoncnaElektrarna.Instance.UrnaEnergijaSC[i];
   }
}


Was This Post Helpful? 3
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6528
  • View blog
  • Posts: 14,438
  • Joined: 02-June 10

Re: How to put code in a loop?

Posted 13 August 2011 - 06:38 AM

Let me also throw in a couple tips:
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.
    btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

Was This Post Helpful? 1
  • +
  • -

#5 wuu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 85
  • Joined: 16-March 09

Re: How to put code in a loop?

Posted 13 August 2011 - 06:44 AM

View Posteclipsed4utoo, on 13 August 2011 - 07:14 AM, said:

You can also use the Find method to find controls with a certain name. Something like this...

string labelName = string.Empty;

for (int i = 0; i < SoncnaElektrarna.Instance.UrnaEnergijaSC.Length; i++)
{
   int labelIndex = i + 1;
   labelName = "Label" + labelIndex.ToString();

   // returns as an array
   Control[] array = this.Controls.Find(labelName, true);
 
   // makes sure a control was found
   if (array.Length > 0)
   {
      Label myLabel = array[0] as Label;
      myLabel.Text = SoncnaElektrarna.Instance.UrnaEnergijaSC[i];
   }
}



Thanks for help, but the problem is in this line
Control[] array = this.Controls.Find(labelName, true);


The problem is that Find doesn't exist in current context...
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: How to put code in a loop?

Posted 13 August 2011 - 06:50 AM

What version of .Net are you using? The Find method been there since .Net 2.0.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6528
  • View blog
  • Posts: 14,438
  • Joined: 02-June 10

Re: How to put code in a loop?

Posted 13 August 2011 - 06:52 AM

View Postwuu, on 13 August 2011 - 07:44 AM, said:

Thanks for help, but the problem is in this line
Control[] array = this.Controls.Find(labelName, true);


The problem is that Find doesn't exist in current context...



You can't literally use "labelName". You have to replace that with the actual name of your label.
Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: How to put code in a loop?

Posted 13 August 2011 - 06:53 AM

View PosttlhIn`toq, on 13 August 2011 - 09:52 AM, said:

View Postwuu, on 13 August 2011 - 07:44 AM, said:

Thanks for help, but the problem is in this line
Control[] array = this.Controls.Find(labelName, true);


The problem is that Find doesn't exist in current context...



You can't literally use "labelName". You have to replace that with the actual name of your label.


In my code, I use that variable to hold the actual name of the label.
Was This Post Helpful? 0
  • +
  • -

#9 wuu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 85
  • Joined: 16-March 09

Re: How to put code in a loop?

Posted 13 August 2011 - 06:53 AM

I'm using .NET 4.0. But is it possible that this doesn't exist, beacouse I'm using it in a ASP.NET project?
Was This Post Helpful? 0
  • +
  • -

#10 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: How to put code in a loop?

Posted 13 August 2011 - 06:57 AM

View Postwuu, on 13 August 2011 - 09:53 AM, said:

I'm using .NET 4.0. But is it possible that this doesn't exist, beacouse I'm using it in a ASP.NET project?


Well, yes. That would have been nice to know ahead of time.

In the future, please specify if you are doing ASP.Net because the code will be different.

You should be able to use FindControl instead of Find.

http://msdn.microsof...y/486wc64h.aspx
Was This Post Helpful? 0
  • +
  • -

#11 wuu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 85
  • Joined: 16-March 09

Re: How to put code in a loop?

Posted 13 August 2011 - 07:13 AM

View Posteclipsed4utoo, on 13 August 2011 - 07:57 AM, said:

View Postwuu, on 13 August 2011 - 09:53 AM, said:

I'm using .NET 4.0. But is it possible that this doesn't exist, beacouse I'm using it in a ASP.NET project?


Well, yes. That would have been nice to know ahead of time.

In the future, please specify if you are doing ASP.Net because the code will be different.

You should be able to use FindControl instead of Find.

http://msdn.microsof...y/486wc64h.aspx


So my code should look something like this?

            string labelName = "";
            for (int i = 0; i < SoncnaElektrarna.Instance.UrnaEnergijaSC.Count; i++)
            {
                int labelIndex = i + 1;
                labelName = "Label" + labelIndex.ToString();
             
                Control c = this.FindControl(labelName);

                if (c != null)
                {
                    Label myLabel = c as Label;
                    myLabel.Text = Convert.ToString(SoncnaElektrarna.Instance.UrnaEnergijaSC[i]);
                }
            }



Except now problem is that this line
Control c = this.FindControl(labelName);


always returns null. Why?
Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6528
  • View blog
  • Posts: 14,438
  • Joined: 02-June 10

Re: How to put code in a loop?

Posted 13 August 2011 - 07:31 AM

Maybe "this" doesn't have those controls.
Maybe something else does... a groupbox... a panel... a different class entirely...

If you put a breakpoint on line 7 you can hover over 'this', get a tooltip for it, then you can expand that to see the .Controls collection and examine it to see how many controls are in 'this' to confirm you are looking at the right object.

Attached Image

This post has been edited by tlhIn`toq: 13 August 2011 - 07:33 AM

Was This Post Helpful? 0
  • +
  • -

#13 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: How to put code in a loop?

Posted 13 August 2011 - 07:39 AM

Post your markup code.
Was This Post Helpful? 0
  • +
  • -

#14 wuu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 85
  • Joined: 16-March 09

Re: How to put code in a loop?

Posted 13 August 2011 - 08:29 AM

How silly of me… Of course "this" doesn't content label controls but the UpdatePanel1 does. So code is now working as it should. Thanks for your help!

Working code:

            string labelName = "";
            for (int i = 0; i < SoncnaElektrarna.Instance.UrnaEnergijaSC.Count; i++)
            {
                int labelIndex = i + 1;
                labelName = "Label" + labelIndex.ToString();

                Control c = UpdatePanel1.FindControl(labelName);

                if (c != null)
                {
                    Label myLabel = c as Label;
                    myLabel.Text = Convert.ToString(SoncnaElektrarna.Instance.UrnaEnergijaSC[i]);
                }
            }


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1