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

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

Post by jgq1985 »

Ah that is weird, I apologize. I swore I'd paste it and save and retried. I wasn't trying to stick with the bad logic. But I then also forgot to use the else{
return $false.
So it appears to be in order and working now. Thanks for being patient and compelled to assist.
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 »

Hmm will maybe it's not working on my other computer. I copy paste your entire code, but had to remove the lines 1 - 438, then removed 574-81.
When running, I still have to insert text into both fields but then switch back to first field and add more text for it to "detect" both fields have text in them.
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 can't just copy and paste code to make a form work. You must connect all of the events correctly.

Take some time to read and learn how forms work. There are many articles on this site.
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 great; thanks again
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 »

So here's where my confusion is/was. It wasn't around the logic of the text box containing text, it was that whatever I was trying in the "script" wasn't working for me.
The issue is that I didn't realize I wasn't editing the .ps1 file code, where you have to add the text field property into the region generated code part.

$textbox_LastName.add_TextChanged($textbox_LastName_TextChanged)

So my real question I guess is, if I create a File > New > Form and choose a template and hit OK, where/how do I see the .ps1 code behind this form?
Nothing in the comments within the PSF indicates that I need to hop over to the .ps1 to manually update copied/pasted text fields -- It only says:
#------------------------------------------------------
# NOTE: When a Control State changes you should call
# Update-NavButtons to trigger validation
#------------------------------------------------------

which I did.
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 »

Go to the "Deploy" tab and use the "Export to File" item to save the PSF to a PS1 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 »

jvierra wrote: Fri Jun 07, 2019 2:09 pm Go to the "Deploy" tab and use the "Export to File" item to save the PSF to a PS1 file.
@jvierra, thanks. So if I ever add a new text box on the PSF side, I have to export it to a PS1 and then go edit there accordingly?
I was thinking with Sapien I would have an all-in-one single pane of glass into the GUI and PS1 at the same time -- was I mistaken?
I was helped by a community member here but I don't know if that's what they had to do.
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 »

Why would you need to do that? Just build and test the code in psf and export when you want a PS! file to "deploy".
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 »

jvierra wrote: Fri Jun 07, 2019 2:32 pm Why would you need to do that? Just build and test the code in psf and export when you want a PS! file to "deploy".
When I'm working in the PSF I don't see anywhere in the "script" where the code for the textbox is adding this:
$textbox1.add_TextChanged($textbox1_TextChanged)

When I export to PS1 I see it:

Image



So the problem is if I copy/paste textbox1 in the GUI designer while working on the PSF, say I name it textbox2, the problem is it doesn't re-create as $textbox2.add_TextChanged($textbox2_TextChanged) in the code. I have to export to ps1 and then edit and add that.
So where I can see this line when working in the psf?

Image

So where in the PSF "script" is that information that is important to making the field work properly?

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(-not $textbox1.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
}



$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
#------------------------------------------------------

$textbox1_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 »

I tis added automatically by the IDE. That is the whole point of using an IDE. It does all of the mundane things for you and hides them since you should not be altering them. Just create the events in the designer and add the code to the event you have created.

This is how nearly all IDEs work. Visual Studio works exactly the same way.

I recommend that you start usi ng PSS after you have read the manual. It will explain much of this.

You can also view the videos on YouTube.
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