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.
This topic is 4 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
craigjb
Posts: 11
Last visit: Mon Oct 05, 2020 7:31 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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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
Last visit: Mon Oct 05, 2020 7:31 am

Re: Setting counter correctly

Post by craigjb »

Thank you so much! Works great!
This topic is 4 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked