c# - using the loop to reach out to pictureBoxes -


i have picturebox1, picturebox2, picturebox3, .... in c# ,is there way can change 1st picturebox background color loop,and wait, , change background of picturebox2?

would this

    private void button1_click(object sender, eventargs e)     {         foreach (var pb in this.controls.oftype<picturebox>())         {             pb.backcolor = color.red;             thread.sleep(2000);         }     } 

but error

error 1 non-invocable member 'system.windows.forms.control.controls' cannot used method. c:\users\test\desktop\windowsformsapplication1\form1.cs 22 37 windowsformsapplication1

try using threadpool (as have noted, may cause threading exceptions, though didn't test or you... depend call code believe):

threadpool.queueuserworkitem(_ => {     foreach (var pb in this.controls.oftype<picturebox>())     {         pb.backcolor = color.red;         thread.sleep(2000);      }  }); 

in repsonse jeff bridgman:

i surprised didn't exception, tested out , working fine. completeness though (it may cause exceptions depending on code called from), here safe version:

threadpool.queueuserworkitem(_ => {     foreach (var pb in controls.oftype<picturebox>())     {         var pb1 = pb;         pb1.begininvoke((action) (() =>             {                 pb1.backcolor = color.red;             }));         thread.sleep(2000);     } }); 

just note why used var pb1 = pb; inside loop (i've had question co-worker). here explanation resharper wiki.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -