Page 1 of 1

Setting counter correctly

Posted: Thu Nov 21, 2019 10:10 pm
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!

Re: Setting counter correctly

Posted: Fri Nov 22, 2019 2:42 am
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)

Re: Setting counter correctly

Posted: Fri Nov 22, 2019 10:13 am
by craigjb
Thank you so much! Works great!