Loop cycle through tabcontrol tabs

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
thatsameer
Posts: 18
Joined: Fri Aug 03, 2018 7:02 am

Loop cycle through tabcontrol tabs

Post by thatsameer » Mon Feb 18, 2019 2:55 pm

Hi,

I have managed to successfully add new tabs to a form for each URL listed in a text file.

This is what it looks like:
cycleloop.PNG
cycleloop.PNG (15.41 KiB) Viewed 548 times
I have added button1 as what I would like is for the tabs to cycle through in a loop, staying on each tab for lets say 5 seconds.

I have tried this:

$button1_Click = {
while (1 -eq 1)
{
$tabcontrol1.SelectedIndex++
UpdateNavButtons
Start-Sleep -s 5
$i++
}
}

The tabs cycle through but the form becomes inoperable and basically "freezes" the form. Also once it has reached the last tab it will not go further as it hits a virtual 'wall'...

Any ideas at all how to have the tabcontrol's tabs cycle through displaying each tab for X time in a continuous loop.

After this my project for this form will be complete so really would be helpful. thanks

User avatar
jvierra
Posts: 13792
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Loop cycle through tabcontrol tabs

Post by jvierra » Mon Feb 18, 2019 5:04 pm

Your code runs in an infinite loop. Here is the correct way to prevent infinite loops. Use a deterministic loop.

Code: Select all

$button1_Click={
    for ($i = 0; $i -lt $tabControl1.TabCount;$i++) {
        $tabcontrol1.SelectedIndex = $i
        Write-Host $tabcontrol1.SelectedTab.Name
        UpdateNavButtons
        Start-Sleep -Seconds 5
    }
    Write-Host finished
}

thatsameer
Posts: 18
Joined: Fri Aug 03, 2018 7:02 am

Re: Loop cycle through tabcontrol tabs

Post by thatsameer » Tue Feb 19, 2019 3:29 am

Thanks for this.

It works in terms of it cycles through the tabs however whilst cycling, the content of the tabs do not display as it seems while the process is running it locks up the form until its over...

I hope this makes sense? Please try recreating to see issue i am having if not clear.


many thanks

User avatar
jvierra
Posts: 13792
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Loop cycle through tabcontrol tabs

Post by jvierra » Tue Feb 19, 2019 5:40 am

I have no idea what you are trying to do. You asked about the tabs freezing. The code I posted does not cause the tabs to freeze.

thatsameer
Posts: 18
Joined: Fri Aug 03, 2018 7:02 am

Re: Loop cycle through tabcontrol tabs

Post by thatsameer » Tue Feb 19, 2019 7:16 am

The code cycles through the tabs successfully. But the tab contents do not display, and the GUI is unclickable while the loop is running. Only once the tab have completed it's cycle does it show the page it finished on.

I would like to see each tab and tab contents one by one in a loop

User avatar
jvierra
Posts: 13792
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Loop cycle through tabcontrol tabs

Post by jvierra » Tue Feb 19, 2019 8:01 am

Yes. That is how forms work. Perhaps you need to rethink your design.

Loading a browser is a heavy lift. Loading a bunch of browsers is going to lock up the form until all of the loading is done,

User avatar
jvierra
Posts: 13792
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Loop cycle through tabcontrol tabs

Post by jvierra » Tue Feb 19, 2019 9:38 am

Demo of loading multiple tabs:
Attachments
Test-TabSelection.psf
(25.03 KiB) Downloaded 19 times

User avatar
jvierra
Posts: 13792
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Loop cycle through tabcontrol tabs

Post by jvierra » Thu Feb 21, 2019 4:23 am

Here is an even better way to generate tab pages with browser controls.
Attachments
Test-TabCreation.psf
(10.25 KiB) Downloaded 20 times

Locked