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 »

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 1080 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

jvierra
Posts: 14546
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Loop cycle through tabcontrol tabs

Post by jvierra »

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 »

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

jvierra
Posts: 14546
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Loop cycle through tabcontrol tabs

Post by jvierra »

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 »

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

jvierra
Posts: 14546
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Loop cycle through tabcontrol tabs

Post by jvierra »

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,

jvierra
Posts: 14546
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Loop cycle through tabcontrol tabs

Post by jvierra »

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

jvierra
Posts: 14546
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Loop cycle through tabcontrol tabs

Post by jvierra »

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

Locked