Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop windows forms objects incremently

I have some webBrowsers on my C# application, infact I have 10. webBrowser0, webBrowser1, webBrowser2 and so on..

Anyways, I'm performing a loop to count each screen to place a webBrowser on each screen I have, all this is done easily, but in my loop, if have something like this.

for (index = 0; index <= totalScreens; index++)
{

if (index == 0)
{
webBrowser0.Width = x;
webBrowser0.Height = x;
}

if (index == 1)
{
webBrowser1.Width = x;
webBrowser1.Height = x;
}

}

As you cansee, I'm doubling up on code quite a lot, so if I could refer to webBrowser{index} that would be perfect but that of course isn't working.

like image 438
Richard Dickinson Avatar asked Mar 02 '26 03:03

Richard Dickinson


1 Answers

Create collection of your WebBrowsers.

List<WebBrowser> browsers = new List<WebBrowser> {webBrowser0,webBrowser1};

for (index = 0; index <= totalScreens; index++)
{
    if(index < browsers.Count)
    {
        browsers[index].Width = x;
        browsers[index].Height = x;
    }
}
like image 197
Stecya Avatar answered Mar 04 '26 18:03

Stecya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!