Author Posts

February 3, 2017 at 2:36 pm

Hi there

Short question: How can I close the current form while the other opens at the same time?
The form which should be closed is called $form1
The form which should be open is called $form2.
the Button which opens form2 is called $form1button

What I did was unter $form1button_Klick {

###the action here####

form1.close () }

But this doesnt work

fyi there are more forms but the mainform is form1. What do I wrong?

Regards

February 3, 2017 at 4:31 pm

Not ideal. Make an invisible parent form to 1 & 2. On the button click go back to the parent and execute code to open form 2.

February 3, 2017 at 4:59 pm

Here is an example.





#region Source: Startup.pss
#----------------------------------------------
#region Import Assemblies
#----------------------------------------------
[void][Reflection.Assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')

#endregion Import Assemblies

#Define a Param block to use custom parameters in the project
#Param ($CustomParameter)

function Main {

	Param ([String]$Commandline)
		
	#--------------------------------------------------------------------------
	#TODO: Add initialization script here (Load modules and check requirements)
	
	
	#--------------------------------------------------------------------------
	
	if((Show-MainForm_psf) -eq 'OK')
	{
		
	}
	
	$global:ExitCode = 0 #Set the exit code for the Packager
}







#endregion Source: Startup.pss

#region Source: MainForm.psf
function Show-MainForm_psf
{
	#----------------------------------------------
	#region Import the Assemblies
	#----------------------------------------------
	[void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
	
	#endregion Import Assemblies

	#----------------------------------------------
	#region Generated Form Objects
	#----------------------------------------------
	[System.Windows.Forms.Application]::EnableVisualStyles()
	$MainForm = New-Object 'System.Windows.Forms.Form'
	$button2 = New-Object 'System.Windows.Forms.Button'
	$button1 = New-Object 'System.Windows.Forms.Button'
	$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
	#endregion Generated Form Objects

	#----------------------------------------------
	# User Generated Script
	#----------------------------------------------
	
	$MainForm_Load={
	#TODO: Initialize Form Controls here
		
		
	}
	
	
	$button1_Click = {
		
		Show-sub_psf $MainForm
		
	}
	
	$button2_Click = {
		
		Show-sub2_psf $MainForm
		
	}
		# --End User Generated Script--
	#----------------------------------------------
	#region Generated Events
	#----------------------------------------------
	
	$Form_StateCorrection_Load=
	{
		#Correct the initial state of the form to prevent the .Net maximized form issue
		$MainForm.WindowState = $InitialFormWindowState
	}
	
	$Form_StoreValues_Closing=
	{
		#Store the control values
	}

	
	$Form_Cleanup_FormClosed=
	{
		#Remove all event handlers from the controls
		try
		{
			$button2.remove_Click($button2_Click)
			$button1.remove_Click($button1_Click)
			$MainForm.remove_Load($MainForm_Load)
			$MainForm.remove_Load($Form_StateCorrection_Load)
			$MainForm.remove_Closing($Form_StoreValues_Closing)
			$MainForm.remove_FormClosed($Form_Cleanup_FormClosed)
		}
		catch { Out-Null  }
	}
	#endregion Generated Events

	#----------------------------------------------
	#region Generated Form Code
	#----------------------------------------------
	$MainForm.SuspendLayout()
	#
	# MainForm
	#
	$MainForm.Controls.Add($button2)
	$MainForm.Controls.Add($button1)
	$MainForm.AutoScaleDimensions = '6, 13'
	$MainForm.AutoScaleMode = 'Font'
	$MainForm.ClientSize = '215, 170'
	$MainForm.Name = 'MainForm'
	$MainForm.StartPosition = 'CenterScreen'
	$MainForm.Text = 'Main Form'
	$MainForm.add_Load($MainForm_Load)
	#
	# button2
	#
	$button2.Location = '48, 117'
	$button2.Name = 'button2'
	$button2.Size = '75, 23'
	$button2.TabIndex = 1
	$button2.Text = 'button2'
	$button2.UseVisualStyleBackColor = $True
	$button2.add_Click($button2_Click)
	#
	# button1
	#
	$button1.Location = '48, 48'
	$button1.Name = 'button1'
	$button1.Size = '75, 23'
	$button1.TabIndex = 0
	$button1.Text = 'button1'
	$button1.UseVisualStyleBackColor = $True
	$button1.add_Click($button1_Click)
	$MainForm.ResumeLayout()
	#endregion Generated Form Code

	#----------------------------------------------

	#Save the initial state of the form
	$InitialFormWindowState = $MainForm.WindowState
	#Init the OnLoad event to correct the initial state of the form
	$MainForm.add_Load($Form_StateCorrection_Load)
	#Clean up the control events
	$MainForm.add_FormClosed($Form_Cleanup_FormClosed)
	#Store the control values when form is closing
	$MainForm.add_Closing($Form_StoreValues_Closing)
	#Show the Form
	return $MainForm.ShowDialog()

}
#endregion Source: MainForm.psf

#region Source: Globals.ps1
	#--------------------------------------------
	# Declare Global Variables and Functions here
	#--------------------------------------------
	
	
	#Sample function that provides the location of the script
	function Get-ScriptDirectory
	{
	
		[OutputType([string])]
		param ()
		if ($hostinvocation -ne $null)
		{
			Split-Path $hostinvocation.MyCommand.path
		}
		else
		{
			Split-Path $script:MyInvocation.MyCommand.Path
		}
	}
	
	#Sample variable that provides the location of the script
	[string]$ScriptDirectory = Get-ScriptDirectory
	
	
	
#endregion Source: Globals.ps1

#region Source: sub.psf
function Show-sub_psf
{
PARAM($main)

	#----------------------------------------------
	#region Import the Assemblies
	#----------------------------------------------
	[void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
	[void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
	[void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
	[void][reflection.assembly]::Load('System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
	[void][reflection.assembly]::Load('System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
	#endregion Import Assemblies

	#----------------------------------------------
	#region Generated Form Objects
	#----------------------------------------------
	[System.Windows.Forms.Application]::EnableVisualStyles()
	$formForm1 = New-Object 'System.Windows.Forms.Form'
	$button1 = New-Object 'System.Windows.Forms.Button'
	$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
	#endregion Generated Form Objects

	#----------------------------------------------
	# User Generated Script
	#----------------------------------------------
	
	
	
	$formForm1_Load={
		
		$main.visible = $false
		
	}
	
	$button1_Click={
		
		
		
	}
	
	
	
	$formForm1_FormClosed=[System.Windows.Forms.FormClosedEventHandler]{
	#Event Argument: $_ = [System.Windows.Forms.FormClosedEventArgs]
		$main.visible = $true
	}
	
	# --End User Generated Script--
	#----------------------------------------------
	#region Generated Events
	#----------------------------------------------
	
	$Form_StateCorrection_Load=
	{
		#Correct the initial state of the form to prevent the .Net maximized form issue
		$formForm1.WindowState = $InitialFormWindowState
	}
	
	$Form_StoreValues_Closing=
	{
		#Store the control values
	}

	
	$Form_Cleanup_FormClosed=
	{
		#Remove all event handlers from the controls
		try
		{
			$button1.remove_Click($button1_Click)
			$formForm1.remove_FormClosed($formForm1_FormClosed)
			$formForm1.remove_Load($formForm1_Load)
			$formForm1.remove_Load($Form_StateCorrection_Load)
			$formForm1.remove_Closing($Form_StoreValues_Closing)
			$formForm1.remove_FormClosed($Form_Cleanup_FormClosed)
		}
		catch { Out-Null  }
	}
	#endregion Generated Events

	#----------------------------------------------
	#region Generated Form Code
	#----------------------------------------------
	$formForm1.SuspendLayout()
	#
	# formForm1
	#
	$formForm1.Controls.Add($button1)
	$formForm1.AutoScaleDimensions = '6, 13'
	$formForm1.AutoScaleMode = 'Font'
	$formForm1.ClientSize = '196, 93'
	$formForm1.Name = 'formForm1'
	$formForm1.Text = 'Form 1'
	$formForm1.add_FormClosed($formForm1_FormClosed)
	$formForm1.add_Load($formForm1_Load)
	#
	# button1
	#
	$button1.DialogResult = 'OK'
	$button1.Location = '57, 32'
	$button1.Name = 'button1'
	$button1.Size = '75, 23'
	$button1.TabIndex = 0
	$button1.Text = 'button1'
	$button1.UseVisualStyleBackColor = $True
	$button1.add_Click($button1_Click)
	$formForm1.ResumeLayout()
	#endregion Generated Form Code

	#----------------------------------------------

	#Save the initial state of the form
	$InitialFormWindowState = $formForm1.WindowState
	#Init the OnLoad event to correct the initial state of the form
	$formForm1.add_Load($Form_StateCorrection_Load)
	#Clean up the control events
	$formForm1.add_FormClosed($Form_Cleanup_FormClosed)
	#Store the control values when form is closing
	$formForm1.add_Closing($Form_StoreValues_Closing)
	#Show the Form
	return $formForm1.ShowDialog()

}
#endregion Source: sub.psf

#region Source: sub2.psf
function Show-sub2_psf
{
param($main)

	#----------------------------------------------
	#region Import the Assemblies
	#----------------------------------------------
	[void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
	[void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
	[void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
	[void][reflection.assembly]::Load('System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
	[void][reflection.assembly]::Load('System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
	#endregion Import Assemblies

	#----------------------------------------------
	#region Generated Form Objects
	#----------------------------------------------
	[System.Windows.Forms.Application]::EnableVisualStyles()
	$formForm2 = New-Object 'System.Windows.Forms.Form'
	$button1 = New-Object 'System.Windows.Forms.Button'
	$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
	#endregion Generated Form Objects

	#----------------------------------------------
	# User Generated Script
	#----------------------------------------------
	
	
	
	$formForm2_Load={
		
		$main.visible = $false
		
	}
	
	
	
	$formForm2_FormClosed=[System.Windows.Forms.FormClosedEventHandler]{
	#Event Argument: $_ = [System.Windows.Forms.FormClosedEventArgs]
		$main.visible = $true
	}
	
	# --End User Generated Script--
	#----------------------------------------------
	#region Generated Events
	#----------------------------------------------
	
	$Form_StateCorrection_Load=
	{
		#Correct the initial state of the form to prevent the .Net maximized form issue
		$formForm2.WindowState = $InitialFormWindowState
	}
	
	$Form_StoreValues_Closing=
	{
		#Store the control values
	}

	
	$Form_Cleanup_FormClosed=
	{
		#Remove all event handlers from the controls
		try
		{
			$formForm2.remove_FormClosed($formForm2_FormClosed)
			$formForm2.remove_Load($formForm2_Load)
			$formForm2.remove_Load($Form_StateCorrection_Load)
			$formForm2.remove_Closing($Form_StoreValues_Closing)
			$formForm2.remove_FormClosed($Form_Cleanup_FormClosed)
		}
		catch { Out-Null  }
	}
	#endregion Generated Events

	#----------------------------------------------
	#region Generated Form Code
	#----------------------------------------------
	$formForm2.SuspendLayout()
	#
	# formForm2
	#
	$formForm2.Controls.Add($button1)
	$formForm2.AutoScaleDimensions = '6, 13'
	$formForm2.AutoScaleMode = 'Font'
	$formForm2.ClientSize = '250, 135'
	$formForm2.Name = 'formForm2'
	$formForm2.Text = 'Form 2'
	$formForm2.add_FormClosed($formForm2_FormClosed)
	$formForm2.add_Load($formForm2_Load)
	#
	# button1
	#
	$button1.DialogResult = 'OK'
	$button1.Location = '88, 50'
	$button1.Name = 'button1'
	$button1.Size = '75, 23'
	$button1.TabIndex = 0
	$button1.Text = 'button1'
	$button1.UseVisualStyleBackColor = $True
	$formForm2.ResumeLayout()
	#endregion Generated Form Code

	#----------------------------------------------

	#Save the initial state of the form
	$InitialFormWindowState = $formForm2.WindowState
	#Init the OnLoad event to correct the initial state of the form
	$formForm2.add_Load($Form_StateCorrection_Load)
	#Clean up the control events
	$formForm2.add_FormClosed($Form_Cleanup_FormClosed)
	#Store the control values when form is closing
	$formForm2.add_Closing($Form_StoreValues_Closing)
	#Show the Form
	return $formForm2.ShowDialog()

}
#endregion Source: sub2.psf

#Start the application
Main ($CommandLine)


February 4, 2017 at 9:58 am

Hi
Thanks for that.

you mean like: form1.visible = $None in the button click?

I tried that but this does not work

February 4, 2017 at 2:29 pm

The buttons to launch each form are on the main form. In each you call the form using the function name passing the main form as a parameter. Show-sub_psf $MainForm. In the load event of the subs you can now control the visibility of the main form on the load and closed events. If you want to go even further such as launching form2 from form1 you can use the same method and problematically click the form button with &$button1_click.

function Show-sub_psf
{
param($main)

$formForm1_Load={

$main.visible = $false

}

$formForm1_FormClosed=[System.Windows.Forms.FormClosedEventHandler]{

$main.visible = $true
}