0

I have this loop and multiple leds. The names of the leds are Led0, Led1, Led2 etc Now i want to change the background of each Led with this loop so i use the counter iTeller. I use WPF and only work in the mainwindow.

for (int iTeller = 0; iTeller < bits.Count(); iTeller++)
{
    if (bits[iTeller] == 1)
    {
        //this doesn't work
        *Led+iTeller+.Background = Brushes.Green;*
    }

}
GvS
  • 52,015
  • 16
  • 101
  • 139
user2827958
  • 357
  • 1
  • 4
  • 20
  • u can iterate over the Forms Controls-Collection and get all Checkbox elements from there. Then you simple need to compare it with your name, and determine whether it should be changed or not. – dognose Feb 13 '14 at 10:33
  • What's the compiler error? – Filip Feb 13 '14 at 10:35
  • See [How can I find WPF controls by name or type?](http://stackoverflow.com/questions/636383/how-can-i-find-wpf-controls-by-name-or-type). – CodeCaster Feb 13 '14 at 10:44

4 Answers4

2

Try Like This (WPF)

for (int iTeller = 0; iTeller < bits.Count(); iTeller++)
    {
        if (bits[iTeller] == 1)
        {

               object i = this.FindName("Led" & iTeller);
               if (i is CheckBox) 
              { 
                CheckBox k = (CheckBox)i;
                MessageBox.Show(k.Name);
              }

           }

    }
Sathish
  • 4,419
  • 4
  • 30
  • 59
  • I get this error: Error 1 The type or namespace name 'Checkbox' could not be found (are you missing a using directive or an assembly reference?) C:\Users\Administator\Dropbox\2NMCT1\Datacommunicatie\labo\Oefeningen\2-Merlijn.DeWandel-digitaalInOut-01\2-Merlijn.DeWandel-digitaalInOut-01\MainWindow.xaml.cs 50 21 2-Merlijn.DeWandel-digitaalInOut-01 – user2827958 Feb 13 '14 at 10:39
  • You probably missing a using statement. Mouse over 'Checkbox' right click is and pick 'resolve' (or maybe some other statement) – Christian Sauer Feb 13 '14 at 10:41
  • That's because `this.Controls.Find()` is WinForms and OP is using WPF. @user: according to your filenames, this is homework. Are you sure you want others to do that for you? – CodeCaster Feb 13 '14 at 10:43
  • I'm in class right now actually, and yes. In this school we have to do a lot ourselves, and it's supported asking on the internet. So yes – user2827958 Feb 13 '14 at 10:44
  • @user I think that before "asking on the internet" comes "searching the web", which would have led you to [How can I find WPF controls by name or type?](http://stackoverflow.com/questions/636383/how-can-i-find-wpf-controls-by-name-or-type), which you can use to find all checkboxes. You can't expect us to help you get up and running with C# and WPF, try to read a tutorial. :-) – CodeCaster Feb 13 '14 at 10:45
  • I did that at first, didn't found it. So i asked it here. And it works now thank you for that, but don't make me (and others of my school) feel bad for asking it here, it's been like this for 2 years and we have no other choice really. Thanks anyway – user2827958 Feb 13 '14 at 10:51
  • @CodeCaster I see what you mean, but I already have a lot of work so instead of having to read tons of tutorials before I find that one thing I need, it's much more efficient to ask people who actually know it. – user2827958 Feb 13 '14 at 10:54
  • _"it's much more efficient to ask people who actually know it"_ - please don't go that way, it may make you look lazy or uninterested to actually learn anything. Good you got it solved anyway. :) – CodeCaster Feb 13 '14 at 10:55
1

This will not work for a lot of reasons. The first is, that your leds are some type of control, which you need in a variable, you cannot simply call them like this. Do you use WPF or Winforms? You need a list of your leds, then you can iterate over the list and assign the value to each led

Christian Sauer
  • 10,351
  • 10
  • 53
  • 85
0

You cannot resolve a variable name like this, you can use the Find method (at least in Windows Forms), to find a named control.

You can also store the controls in an array, that way you prevent using relatively slow Find calls and other error checking:

var leds = new CheckBox[] { Led0, Led1, Led2, Led3, Led4, Led5, Led6, Led7 };

for (int iTeller = 0; iTeller < bits.Count(); iTeller++)
{
    if (bits[iTeller] == 1)
    {
        leds[iTeller].Background = Brushes.Green;
    }

 }
GvS
  • 52,015
  • 16
  • 101
  • 139
  • I get this error: Error 1 The type or namespace name 'Checkbox' could not be found (are you missing a using directive or an assembly reference?) No resolve – user2827958 Feb 13 '14 at 10:46
  • Typo, should be CheckBox (capital B), or whatever other class/control you have used for the control. – GvS Feb 13 '14 at 10:47
0

This worked

for (int iTeller = 0; iTeller < bits.Count(); iTeller++)
            {
                if (bits[iTeller] == 1)
                {
                    var myCheckbox = (CheckBox)this.FindName("Led" + iTeller);
                    myCheckbox.Background = Brushes.Green;
                }

            }

Thanks everyone

user2827958
  • 357
  • 1
  • 4
  • 20