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.
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.
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".
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.
#-------------------------------------------------------
# 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
}
}
#-------------------------------------------------------
# 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
}
}
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.