New customer, new wizard form, validate multiple text boxes?

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 9 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
jgq1985
Posts: 23
Last visit: Thu Dec 07, 2023 7:30 am

New customer, new wizard form, validate multiple text boxes?

Post by jgq1985 »

I created a new form "Wizard style" in PowerShell Studio 2019. Out of the box it works fine. However I wanted to add an additional text box to validate text exist in it as well. It seems hit or miss. I put text in first text box and then second, and it doesn't show the NExt button to be clickable. If I delete text from the first text box, even just the last letter in the text value, it's like it forces another update and Next button becomes clikable.

Is there problem with this code snippet?

Code: Select all

function Test-WizardPage
{
<#
	Add TabPages and place the validation code in this function
#>
	[OutputType([boolean])]
	param([System.Windows.Forms.TabPage]$tabPage)
	
	if($tabPage -eq $tabpageStep1)
	{
		#TODO: Enter Validation Code here for Step 1
		if(-not $textbox_FirstName.Text)
		{
			return $false
		}
		
		if (-not $textbox_LastName.Text)
		{
			return $false
		}
		
		
		
		return $true
	}
	elseif ($tabPage -eq $tabpageStep2)
	{
		#TODO: Enter Validation Code here for Step 2
		if($checkboxCheckToContinue.Checked)
		{
			return $true
		}
		
		return $false
	}
	elseif ($tabPage -eq $tabpageStep3)
	{
		#TODO: Enter Validation Code here for Step 3
		if(	$radiobuttonOption1.Checked -or
			$radiobuttonOption2.Checked -or
			$radiobuttonOption3.Checked)
		{
			return $true
		}
	}
	#Add more pages here
	
	return $false
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: New customer, new wizard form, validate multiple text boxes?

Post by jvierra »

Avoid using negative logic. Try to always analyze in the affirmative. TO validate two textboxes do the following:

Code: Select all

#TODO: Enter Validation Code here for Step 1
if ($textbox_FirstName.Text -and $textbox_LastName.Text) {
    return $true
}else{
    return $false
}
User avatar
jgq1985
Posts: 23
Last visit: Thu Dec 07, 2023 7:30 am

Re: New customer, new wizard form, validate multiple text boxes?

Post by jgq1985 »

Thanks I will try. I was just adding to the existing code from the template which used negative logic
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: New customer, new wizard form, validate multiple text boxes?

Post by jvierra »

There is no negative logic in the provided code. It always test for "true".

By negative logic I mean using "-not" to test with. Always test for $true and alter the "if/else" logic to react accordingly. It is easier to understand and prevents logic sense inversion errors.

Also you wanted both boxes to be valid but tested each one independently which would validate the page if either one had a value. Logic is additive in this case as opposed to checking if any one of many is true which is multiplicative logic.

$true -and $true #is the only valid condition for "add". Testing separately is the same as using "-or".
User avatar
jgq1985
Posts: 23
Last visit: Thu Dec 07, 2023 7:30 am

Re: New customer, new wizard form, validate multiple text boxes?

Post by jgq1985 »

Ok thanks well I must be missing something else.
Am I supposed to add a $textbox_LastName_TextChanged={Update-NavButtons} ?
All I've done was go to File > New > Forms, then select "Wizard style" and hit OK.

Then changed the first text and textbox and duplicated and named those.

Tried using the suggested logic but what happens is if I type text into the first name, Next button is greyed. Then I type text into Last name, it's still greyed. But then I click to first name and add additional text, Next button becomes clickable.

Image

Image


The full code:

Code: Select all


#-------------------------------------------------------
# NOTE: When new TabPage added place the validation code
# 		in the Test-WizardPage function.
#-------------------------------------------------------
function Test-WizardPage
{
<#
	Add TabPages and place the validation code in this function
#>
	[OutputType([boolean])]
	param([System.Windows.Forms.TabPage]$tabPage)
	
	if($tabPage -eq $tabpageStep1)
	{
		#TODO: Enter Validation Code here for Step 1
		if ($textbox_FirstName.Text -and $textbox_LastName.Text)
		{
			return $true
		}
		else
		{
			return $false
		}
		
		return $true
	}
	elseif ($tabPage -eq $tabpageStep2)
	{
		#TODO: Enter Validation Code here for Step 2
		if($checkboxCheckToContinue.Checked)
		{
			return $true
		}
		
		return $false
	}
	elseif ($tabPage -eq $tabpageStep3)
	{
		#TODO: Enter Validation Code here for Step 3
		if(	$radiobuttonOption1.Checked -or
			$radiobuttonOption2.Checked -or
			$radiobuttonOption3.Checked)
		{
			return $true
		}
	}
	#Add more pages here
	
	return $false
}



$buttonFinish_Click={
	#-------------------------------------------------------
	# TODO: Place finalization script here
	#-------------------------------------------------------
	
}

#region Events and Functions
$formWizard_Load={
	Update-NavButtons
}

function Update-NavButtons
{
	<# 
		.DESCRIPTION
		Validates the current tab and Updates the Next, Prev and Finish buttons.
	#>
	$enabled = Test-WizardPage $tabcontrolWizard.SelectedTab
	$buttonNext.Enabled = $enabled -and ($tabcontrolWizard.SelectedIndex -lt $tabcontrolWizard.TabCount - 1)
	$buttonBack.Enabled = $tabcontrolWizard.SelectedIndex -gt 0
	$buttonFinish.Enabled = $enabled -and ($tabcontrolWizard.SelectedIndex -eq $tabcontrolWizard.TabCount - 1)	
	#Uncomment to Hide Buttons
	#$buttonNext.Visible = ($tabcontrolWizard.SelectedIndex -lt $tabcontrolWizard.TabCount - 1)
	#$buttonFinish.Visible = ($tabcontrolWizard.SelectedIndex -eq $tabcontrolWizard.TabCount - 1)
}

$script:DeselectedIndex = -1
$tabcontrolWizard_Deselecting=[System.Windows.Forms.TabControlCancelEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.TabControlCancelEventArgs]
	# Store the previous tab index
	$script:DeselectedIndex = $_.TabPageIndex
}

$tabcontrolWizard_Selecting=[System.Windows.Forms.TabControlCancelEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.TabControlCancelEventArgs]
	# We only validate if we are moving to the Next TabPage. 
	# Users can move back without validating
	if($script:DeselectedIndex -ne -1 -and $script:DeselectedIndex -lt $_.TabPageIndex)
	{
		#Validate each page until we reach the one we want
		for($index = $script:DeselectedIndex; $index -lt $_.TabPageIndex; $index++)
		{
			$_.Cancel = -not (Test-WizardPage $tabcontrolWizard.TabPages[$index])
			
			if($_.Cancel) 
			{
				# Cancel and Return if validation failed.
				return;
			}
		}
	}
	
	Update-NavButtons
}

$buttonBack_Click={
	#Go to the previous tab page
	if($tabcontrolWizard.SelectedIndex -gt 0)
	{
		$tabcontrolWizard.SelectedIndex--
	}
}

$buttonNext_Click={	
	#Go to the next tab page
	if($tabcontrolWizard.SelectedIndex -lt $tabcontrolWizard.TabCount - 1)
	{
		$tabcontrolWizard.SelectedIndex++
	}
}

#endregion

#------------------------------------------------------
# NOTE: When a Control State changes you should call
# 		Update-NavButtons to trigger validation
#------------------------------------------------------

$textbox_FirstName_TextChanged={
	Update-NavButtons
}

$checkboxCheckToContinue_CheckedChanged={
	Update-NavButtons
}

$radiobuttonOption_CheckedChanged={
	
	if($this.Checked)
	{
		Update-NavButtons
	}
}

jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: New customer, new wizard form, validate multiple text boxes?

Post by jvierra »

Please post your PSF file.
User avatar
jgq1985
Posts: 23
Last visit: Thu Dec 07, 2023 7:30 am

Re: New customer, new wizard form, validate multiple text boxes?

Post by jgq1985 »

Here is the PSF:

Code: Select all


#-------------------------------------------------------
# NOTE: When new TabPage added place the validation code
# 		in the Test-WizardPage function.
#-------------------------------------------------------
function Test-WizardPage
{
<#
	Add TabPages and place the validation code in this function
#>
	[OutputType([boolean])]
	param([System.Windows.Forms.TabPage]$tabPage)
	
	if($tabPage -eq $tabpageStep1)
	{
		#TODO: Enter Validation Code here for Step 1
		if ($textbox_LastName.TextLength -eq 0 -or
			$textboxFirstName.TextLength -eq 0			
		)
			{
			return $false
		}
		
		return $true
		
	
	}
	elseif ($tabPage -eq $tabpageStep2)
	{
		#TODO: Enter Validation Code here for Step 2
		if ($checkboxCheckToContinue.Checked)
		{
			return $true
		}
		
		return $false
	}
	elseif ($tabPage -eq $tabpageStep3)
	{
		#TODO: Enter Validation Code here for Step 3
		if ($radiobuttonOption1.Checked -or
			$radiobuttonOption2.Checked -or
			$radiobuttonOption3.Checked)
		{
			return $true
		}
	}
	#Add more pages here
	
	return $false
}



$buttonFinish_Click={
	#-------------------------------------------------------
	# TODO: Place finalization script here
	#-------------------------------------------------------
	
}

#region Events and Functions
$formWizard_Load={
	Update-NavButtons
}

function Update-NavButtons
{
	<# 
		.DESCRIPTION
		Validates the current tab and Updates the Next, Prev and Finish buttons.
	#>
	$enabled = Test-WizardPage $tabcontrolWizard.SelectedTab
	$buttonNext.Enabled = $enabled -and ($tabcontrolWizard.SelectedIndex -lt $tabcontrolWizard.TabCount - 1)
	$buttonBack.Enabled = $tabcontrolWizard.SelectedIndex -gt 0
	$buttonFinish.Enabled = $enabled -and ($tabcontrolWizard.SelectedIndex -eq $tabcontrolWizard.TabCount - 1)	
	#Uncomment to Hide Buttons
	#$buttonNext.Visible = ($tabcontrolWizard.SelectedIndex -lt $tabcontrolWizard.TabCount - 1)
	#$buttonFinish.Visible = ($tabcontrolWizard.SelectedIndex -eq $tabcontrolWizard.TabCount - 1)
}

$script:DeselectedIndex = -1
$tabcontrolWizard_Deselecting=[System.Windows.Forms.TabControlCancelEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.TabControlCancelEventArgs]
	# Store the previous tab index
	$script:DeselectedIndex = $_.TabPageIndex
}

$tabcontrolWizard_Selecting=[System.Windows.Forms.TabControlCancelEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.TabControlCancelEventArgs]
	# We only validate if we are moving to the Next TabPage. 
	# Users can move back without validating
	if($script:DeselectedIndex -ne -1 -and $script:DeselectedIndex -lt $_.TabPageIndex)
	{
		#Validate each page until we reach the one we want
		for($index = $script:DeselectedIndex; $index -lt $_.TabPageIndex; $index++)
		{
			$_.Cancel = -not (Test-WizardPage $tabcontrolWizard.TabPages[$index])
			
			if($_.Cancel) 
			{
				# Cancel and Return if validation failed.
				return;
			}
		}
	}
	
	Update-NavButtons
}

$buttonBack_Click={
	#Go to the previous tab page
	if($tabcontrolWizard.SelectedIndex -gt 0)
	{
		$tabcontrolWizard.SelectedIndex--
	}
}

$buttonNext_Click={	
	#Go to the next tab page
	if($tabcontrolWizard.SelectedIndex -lt $tabcontrolWizard.TabCount - 1)
	{
		$tabcontrolWizard.SelectedIndex++
	}
}

#endregion

#------------------------------------------------------
# NOTE: When a Control State changes you should call
# 		Update-NavButtons to trigger validation
#------------------------------------------------------


$textbox_LastName_TextChanged = {
	Update-NavButtons
}

$textboxFirstName_TextChanged={
	Update-NavButtons
}



$checkboxCheckToContinue_CheckedChanged={
	Update-NavButtons
}

$radiobuttonOption_CheckedChanged={
	
	if($this.Checked)
	{
		Update-NavButtons
	}
}

jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: New customer, new wizard form, validate multiple text boxes?

Post by jvierra »

Please post the PSF as an attachment. It is pretty useless as a paste.
User avatar
jgq1985
Posts: 23
Last visit: Thu Dec 07, 2023 7:30 am

Re: New customer, new wizard form, validate multiple text boxes?

Post by jgq1985 »

Got it. Here it is
Attachments
wiztest1.psf
(31.38 KiB) Downloaded 87 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: New customer, new wizard form, validate multiple text boxes?

Post by jvierra »

You didn't use the code I posted. You just changed it to do exactly what I didn't want you to do.

Here is my code.
Attachments
wiztest1_fix.psf
(36.8 KiB) Downloaded 113 times
This topic is 4 years and 9 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