none
Add event handler to Powershell based form

    Question

  • I wrote a small script to display a Windows form allowing me to enable or disable the Proxy setting for IE.  When I change the ComboBox value and click OK, the event doesn't fire.  How can I connect my switch (shown below) with the OK button?

    # Load assemblies for forms
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
    
    # Add functions
    #region Functions
    
    	function Disable-Proxy {
    		Push-Location "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings";
    		set-itemproperty . ProxyEnable 0;
    		Pop-Location;
    	}
    
    	function Enable-Proxy {
    		Push-Location "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings";
    		set-itemproperty . ProxyEnable 1;
    		Pop-Location;
    	}
    	
    	function Get-ProxyState {
    		return (Get-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings").ProxyEnable;
    	}
    
    #endregion Functions
    
    # Add form
    $objForm = New-Object System.Windows.Forms.Form;
    $objForm.ShowIcon = $false;
    $objForm.Text = "Enable Internet Proxy"
    $objForm.Size = New-Object System.Drawing.Size(300,170) 
    $objForm.StartPosition = "CenterScreen"
    $objForm.MinimizeBox = $false;
    $objForm.MaximizeBox = $false;
    
    $objForm.KeyPreview = $True
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
        {$x=$objTextBox.Text;$objForm.Close()}})
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
        {$objForm.Close()}})
    
    # Get proxy state
    $proxystate = Get-ProxyState;
    
    # Add label showing proxy state
    $objLabelProxyState = New-Object System.Windows.Forms.Label
    $objLabelProxyState.Location = New-Object System.Drawing.Size(10,10) 
    $objLabelProxyState.Size = New-Object System.Drawing.Size(280,20) 
    switch($proxystate) {
    	0 {
    		$objLabelProxyState.Text = "The proxy is disabled.";
    	}
    	1 {
    		$objLabelProxyState.Text = "The proxy is enabled.";
    	}
    }
    $objForm.Controls.Add($objLabelProxyState) 
    
    # Add label instructing to select an option
    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(10,30) 
    $objLabel.Size = New-Object System.Drawing.Size(280,20) 
    $objLabel.Text = "Set the proxy state."
    $objForm.Controls.Add($objLabel) 
    
    # Add combobox for selection
    $objComboBox = New-Object System.Windows.Forms.ComboBox;
    $ComboBoxOptions = "Disabled", "Enabled";
    $objComboBox.Items.AddRange($ComboBoxOptions);
    $objComboBox.SelectedIndex = 0;
    $objComboBox.Location = New-Object System.Drawing.Size(10,50) 
    $objComboBox.Size = New-Object System.Drawing.Size(260,20) 
    $objForm.Controls.Add($objComboBox)
    
    # Add Ok button
    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Size(50,90)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = "OK"
    $OKButton.Add_Click({$x=$objTextBox.Text;$objForm.Close()})
    $objForm.Controls.Add($OKButton)
    
    # Add cancel button
    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Size(150,90)
    $CancelButton.Size = New-Object System.Drawing.Size(75,23)
    $CancelButton.Text = "Cancel"
    $CancelButton.Add_Click({$objForm.Close()})
    $objForm.Controls.Add($CancelButton)
    
    
    # Set form on top
    $objForm.Topmost = $True
    
    # Show fim
    $objForm.Add_Shown({$objForm.Activate()})
    [void] $objForm.ShowDialog()
    
    # Set selection to ComboBox selected text.
    $selection = $objComboBox.SelectedText;
    
    # Set ProxyEnable from ComboBox.
    switch($selection) {
    	"Disabled" {
    		Disable-Proxy;
    	}
    	"Enabled" {
    		Enable-Proxy;
    	}
    }
    
    $x
    


    Tuesday, September 20, 2011 10:07 PM

Answers

  • # Load assemblies for forms
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
    
    # Add functions
    #region Functions
    
    	function Disable-Proxy {
    		Push-Location "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings";
    		set-itemproperty . ProxyEnable 0;
    		Pop-Location;
    	}
    
    	function Enable-Proxy {
    		Push-Location "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings";
    		set-itemproperty . ProxyEnable 1;
    		Pop-Location;
    	}
    	
    	function Get-ProxyState {
    		return (Get-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings").ProxyEnable;
    	}
    
    #endregion Functions
    
    # Add form
    $objForm = New-Object System.Windows.Forms.Form;
    $objForm.ShowIcon = $false;
    $objForm.Text = "Enable Internet Proxy"
    $objForm.Size = New-Object System.Drawing.Size(300,170) 
    $objForm.StartPosition = "CenterScreen"
    $objForm.MinimizeBox = $false;
    $objForm.MaximizeBox = $false;
    
    $objForm.KeyPreview = $True
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
        {$x=$objTextBox.Text;$objForm.Close()}})
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
        {$objForm.Close()}})
    
    # Get proxy state
    $proxystate = Get-ProxyState;
    
    $objForm.Controls.Add($objLabelProxyState) 
    # Add label showing proxy state
    $objLabelProxyState = New-Object System.Windows.Forms.Label
    $objLabelProxyState.Location = New-Object System.Drawing.Size(10,10) 
    $objLabelProxyState.Size = New-Object System.Drawing.Size(280,20) 
    
    switch($proxystate) {
    	0 {
    		$objLabelProxyState.Text = "The proxy is disabled.";
    	}
    	1 {
    		$objLabelProxyState.Text = "The proxy is enabled.";
    	}
    }
    
    # Add label instructing to select an option
    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(10,30) 
    $objLabel.Size = New-Object System.Drawing.Size(280,20) 
    $objLabel.Text = "Set the proxy state."
    $objForm.Controls.Add($objLabel) 
    
    # Add combobox for selection
    $objComboBox = New-Object System.Windows.Forms.ComboBox;
    $ComboBoxOptions = "Disabled", "Enabled";
    $objComboBox.Items.AddRange($ComboBoxOptions);
    $objComboBox.SelectedIndex = 0;
    $objComboBox.Location = New-Object System.Drawing.Size(10,50) 
    $objComboBox.Size = New-Object System.Drawing.Size(260,20) 
    $objForm.Controls.Add($objComboBox)
    
    # Add Ok button
    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Size(50,90)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = "OK"
    $OKButton.Add_Click({
    	switch ($objComboBox.SelectedItem)
    	{
    		"Enabled" {Enable-Proxy}
    		"Disabled" {Disable-Proxy}
    	}
    	$objForm.Close()})
    $objForm.Controls.Add($OKButton)
    
    # Add cancel button
    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Size(150,90)
    $CancelButton.Size = New-Object System.Drawing.Size(75,23)
    $CancelButton.Text = "Cancel"
    $CancelButton.Add_Click({$objForm.Close()})
    $objForm.Controls.Add($CancelButton)
    
    
    # Set form on top
    $objForm.Topmost = $True
    
    [void] $objForm.ShowDialog()
    

    • Edited by KazunMVP Tuesday, September 20, 2011 10:35 PM
    • Marked as answer by Will Steele Wednesday, September 21, 2011 1:16 AM
    Tuesday, September 20, 2011 10:34 PM