Referencing components with index
-
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
-
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
-
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".
-
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
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
-
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
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
-
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
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
-
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
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
-
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
Replace
_bars[i] = ...
with_bars[i].Value = ...
(Sorry, my mistake) Robert -
Replace
_bars[i] = ...
with_bars[i].Value = ...
(Sorry, my mistake) Robert