Setting counter correctly

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
User avatar
craigjb
Posts: 11
Joined: Mon Apr 23, 2018 7:30 am

Setting counter correctly

Post by craigjb »

I have a multi-form app I am working on. I have a picturebox in the middle of the main form that I update from the child form.

I have 40 images I am sorting through named "Pic1.jpg" through "Pic40.jpg" (need to restart from 1 when I go over 40). I need to update the picturebox every time I hit a button on the child form named "submit". The below counter is setup in the function $buttonSubmit_Click. However, every time I hit submit it stays at the the first picture "pic1.jpg". I am assuming it is because the counter just resets every time I hit submit. How do I get around this? Would I need to declare "$counter" a global variable? I followed the global variable documentation and was unsuccessful. I would use Get-Random but it really isn't that random.

Code: Select all

	$Counter
	$Counter++
	If ($Counter -eq 40)
	{
		$Counter = 0
		$Counter++
	}
	$PicFileName = "pic" + $Counter + ".jpg"
	$picturebox1.Image = [System.Drawing.Image]::FromFile("C:\Pres\$PicFileName")
Thank you!

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

Re: Setting counter correctly

Post by jvierra »

This would be a way to do this.

Code: Select all

	if($script:counter -eq 40){
		$script:counter = 1
	}else{
        $script:counter++
    }
	$fileName = 'C:\Pres\pic{0}.jpg' -f $counter
	$picturebox1.Image = [System.Drawing.Image]::FromFile($fileName)

User avatar
craigjb
Posts: 11
Joined: Mon Apr 23, 2018 7:30 am

Re: Setting counter correctly

Post by craigjb »

Thank you so much! Works great!

Locked