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. .NET (Core and Framework)
  4. Referencing components with index

Referencing components with index

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpquestiondatabasetutorial
9 Posts 3 Posters 1 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.
  • S Offline
    S Offline
    sysrev
    wrote on last edited by
    #1

    If I'm in the wrong place or this is a stupid question I apologise in advance. I am having to write a data acquisition program in .NET (C#) before I’ve had time to learn it which is making for some interesting code. What I would really like to do is reference a group of 24 progress bar components within a for/next loop using the index of the loop. Does anyone know how to do this? The code I use at the moment is very inelegant and bugs me. Would appreciate any pointers (sorry ‘bout pun)

    Ted Edwards

    L R 2 Replies Last reply
    0
    • S sysrev

      If I'm in the wrong place or this is a stupid question I apologise in advance. I am having to write a data acquisition program in .NET (C#) before I’ve had time to learn it which is making for some interesting code. What I would really like to do is reference a group of 24 progress bar components within a for/next loop using the index of the loop. Does anyone know how to do this? The code I use at the moment is very inelegant and bugs me. Would appreciate any pointers (sorry ‘bout pun)

      Ted Edwards

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      Wow Ted, that just sounds horrible. But given your situation:

      sysrev wrote:

      before I’ve had time to learn it

      and

      sysrev wrote:

      a group of 24 progress bar components

      With that situation I would go with "if it works, I don't care".

      S 1 Reply Last reply
      0
      • L led mike

        Wow Ted, that just sounds horrible. But given your situation:

        sysrev wrote:

        before I’ve had time to learn it

        and

        sysrev wrote:

        a group of 24 progress bar components

        With that situation I would go with "if it works, I don't care".

        S Offline
        S Offline
        sysrev
        wrote on last edited by
        #3

        But I'll need to do it again somewhere - and again, and again.........

        Ted Edwards

        1 Reply Last reply
        0
        • S sysrev

          If I'm in the wrong place or this is a stupid question I apologise in advance. I am having to write a data acquisition program in .NET (C#) before I’ve had time to learn it which is making for some interesting code. What I would really like to do is reference a group of 24 progress bar components within a for/next loop using the index of the loop. Does anyone know how to do this? The code I use at the moment is very inelegant and bugs me. Would appreciate any pointers (sorry ‘bout pun)

          Ted Edwards

          R Offline
          R Offline
          Robert Rohde
          wrote on last edited by
          #4

          Hi, hard to give a good answer without knowing your exact situation. You could add all progress bars to a List. After you have done this once you can iterate the list with for/foreach or whatever technique you like. Robert

          S 1 Reply Last reply
          0
          • R Robert Rohde

            Hi, hard to give a good answer without knowing your exact situation. You could add all progress bars to a List. After you have done this once you can iterate the list with for/foreach or whatever technique you like. Robert

            S Offline
            S Offline
            sysrev
            wrote on last edited by
            #5

            Thanks for the suggestion Robert. The progress bars display the output of a pressure sensor over a 24 hour period. When I get a new reading, I shift values chronologically "down" one bar and put the new value in the "top" (most recent) bar. The coding begs for an iterative loop but I don't know how to address the bars name property with the index. So I ended up using a switch construct which works okay but is awkward to maintain. The List class is new to me and on your suggestion I had a quick look at it. I must say, it does not look promising but will try it out if I can.

            Ted

            R 1 Reply Last reply
            0
            • S sysrev

              Thanks for the suggestion Robert. The progress bars display the output of a pressure sensor over a 24 hour period. When I get a new reading, I shift values chronologically "down" one bar and put the new value in the "top" (most recent) bar. The coding begs for an iterative loop but I don't know how to address the bars name property with the index. So I ended up using a switch construct which works okay but is awkward to maintain. The List class is new to me and on your suggestion I had a quick look at it. I must say, it does not look promising but will try it out if I can.

              Ted

              R Offline
              R Offline
              Robert Rohde
              wrote on last edited by
              #6

              Hi, I'll try to outline what I meant:

              //Field declaration in the Form/UserCotrol
              private List _bars;

              //somewhere after the InitializeComponents call
              _bars = new List(new ProgressBar[] { pb1, pb2, pb3, ... });

              //when you want to update the bars
              for (int i = 0; i < 24; i++)
              {
              _bars[i] = someArrayWithTheValues[i];
              }

              With the mod operator you could also easily switch which hour should be displayed at the top: int hourToDisplayAtTop = 4;

              for (int i = 0; i < 24; i++)
              {
              _bars[i] = someArrayWithTheValues[(i + hourToDisplayAtTop) % 24];
              }

              Robert

              S 1 Reply Last reply
              0
              • R Robert Rohde

                Hi, I'll try to outline what I meant:

                //Field declaration in the Form/UserCotrol
                private List _bars;

                //somewhere after the InitializeComponents call
                _bars = new List(new ProgressBar[] { pb1, pb2, pb3, ... });

                //when you want to update the bars
                for (int i = 0; i < 24; i++)
                {
                _bars[i] = someArrayWithTheValues[i];
                }

                With the mod operator you could also easily switch which hour should be displayed at the top: int hourToDisplayAtTop = 4;

                for (int i = 0; i < 24; i++)
                {
                _bars[i] = someArrayWithTheValues[(i + hourToDisplayAtTop) % 24];
                }

                Robert

                S Offline
                S Offline
                sysrev
                wrote on last edited by
                #7

                I think I can see what you are suggesting and am trying to experiment with it in a new application as here: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace IndexComponents { public partial class Form1 : Form { private ProgressBar pb1; private ProgressBar pb2; private ProgressBar pb3; private List _bars; public Form1() { InitializeComponent(); int[] someArrayWithTheValues = new int[3] { 1, 2, 3 }; //Field declaration in the Form/UserCotrol /// private List _bars; //somewhere after the InitializeComponents call /// _bars = new List(new ProgressBar[] { pb1, pb2, pb3 });//when you want to update the bars _bars = new List(new ProgressBar[] { pb1, pb2, pb3 });//when you want to update the bars for (int i = 0; i < 3; i++) { _bars[i] = someArrayWithTheValues[i]; } //With the mod operator you could also easily switch which hour should be displayed at the top: int hourToDisplayAtTop = 2; for (int i = 0; i < 3; i++) { _bars[i] = someArrayWithTheValues[(i + hourToDisplayAtTop) % 24]; } } } } I am now working on the last two(I hope!) build error which are flagged within the two for loops as below: Error 1 Cannot convert type 'int' to 'System.Windows.Forms.ProgressBar' Error 2 Cannot implicitly convert type 'int' to 'System.Windows.Forms.ProgressBar' Ted Edwards

                R 1 Reply Last reply
                0
                • S sysrev

                  I think I can see what you are suggesting and am trying to experiment with it in a new application as here: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace IndexComponents { public partial class Form1 : Form { private ProgressBar pb1; private ProgressBar pb2; private ProgressBar pb3; private List _bars; public Form1() { InitializeComponent(); int[] someArrayWithTheValues = new int[3] { 1, 2, 3 }; //Field declaration in the Form/UserCotrol /// private List _bars; //somewhere after the InitializeComponents call /// _bars = new List(new ProgressBar[] { pb1, pb2, pb3 });//when you want to update the bars _bars = new List(new ProgressBar[] { pb1, pb2, pb3 });//when you want to update the bars for (int i = 0; i < 3; i++) { _bars[i] = someArrayWithTheValues[i]; } //With the mod operator you could also easily switch which hour should be displayed at the top: int hourToDisplayAtTop = 2; for (int i = 0; i < 3; i++) { _bars[i] = someArrayWithTheValues[(i + hourToDisplayAtTop) % 24]; } } } } I am now working on the last two(I hope!) build error which are flagged within the two for loops as below: Error 1 Cannot convert type 'int' to 'System.Windows.Forms.ProgressBar' Error 2 Cannot implicitly convert type 'int' to 'System.Windows.Forms.ProgressBar' Ted Edwards

                  R Offline
                  R Offline
                  Robert Rohde
                  wrote on last edited by
                  #8

                  Replace _bars[i] = ... with _bars[i].Value = ... (Sorry, my mistake) Robert

                  S 1 Reply Last reply
                  0
                  • R Robert Rohde

                    Replace _bars[i] = ... with _bars[i].Value = ... (Sorry, my mistake) Robert

                    S Offline
                    S Offline
                    sysrev
                    wrote on last edited by
                    #9

                    That's great. It all works - I can experiment further now I have a working example. Thanks for your help.

                    Ted

                    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