Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Controls disabled when setting Visible property outside of runspace with synced hashtable

$
0
0

First, I open a GUI in it's own runspace using a synced hashtable. Initially, I set the Visible property of a radio button group to false because I don't want it to display until a certain option has been selected by another control.

When the option has been selected I make the control visible again. I am able to see the control, but cannot change it.

I've tried many different things including setting the enabled property again for the groupbox and radiobutton controls, setting the CanSelect property, etc...  Many times it even locks up the form and cannot be closed by the 'X' in the upper-right corner of the window.

Is there a way around this?

I'm attaching a brief example of code that should produce the behavior I am experiencing.

function Load-Form {
    $RunSpace = [Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace()
    $RunSpace.ApartmentState = "STA"
    $RunSpace.ThreadOptions = "ReuseThread"
    $RunSpace.Open()
    $RunSpace.SessionStateProxy.SetVariable("SyncHT",$Script:SyncHT)
    $PowerShellCmd = [Management.Automation.PowerShell]::Create().AddScript({
        [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
        [Reflection.Assembly]::LoadWithPartialName("System.Drawing")
        [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms.VisualStyles")

        $Script:SyncHT.formMain = New-Object System.Windows.Forms.Form
        $Script:SyncHT.radiobuttonYes = New-Object System.Windows.Forms.RadioButton
        $Script:SyncHT.radiobuttonNo = New-Object System.Windows.Forms.RadioButton

        $Script:SyncHT.formMain.Size = New-Object System.Drawing.Size(300,100)
        $Script:SyncHT.formMain.MaximumSize = New-Object System.Drawing.Size(300,100)
        $Script:SyncHT.formMain.MinimumSize = New-Object System.Drawing.Size(300,100)
        $Script:SyncHT.formMain.StartPosition = "CenterScreen"
        $Script:SyncHT.formMain.Text = "Radio Button Test"
        $Script:SyncHT.formMain.MaximizeBox = $false
        $Script:SyncHT.formMain.MinimizeBox = $true

        $Script:SyncHT.radiobuttonYes.Size = New-Object System.Drawing.Size(50,20)
        $Script:SyncHT.radiobuttonYes.Location = New-Object System.Drawing.Size(10,23)
        $Script:SyncHT.radiobuttonYes.Checked = $false
        $Script:SyncHT.radiobuttonYes.Text = "Yes"
        #change commenting on the next line for testing
        $Script:SyncHT.radiobuttonYes.Visible = $false
        $Script:SyncHT.radiobuttonYes.add_Click({$Script:SyncHT.FullscreenChanged="Yes"})

        $Script:SyncHT.radiobuttonNo.Size = New-Object System.Drawing.Size(50,20)
        $Script:SyncHT.radiobuttonNo.Location = New-Object System.Drawing.Size(110,23)
        $Script:SyncHT.radiobuttonNo.Checked = $false
        $Script:SyncHT.radiobuttonNo.Text = "No"
        #change commenting on the next line for testing
        $Script:SyncHT.radiobuttonNo.Visible = $false
        $Script:SyncHT.radiobuttonNo.add_Click({$Script:SyncHT.FullscreenChanged="No"})

        $Script:SyncHT.formMain.Controls.Add($Script:SyncHT.radiobuttonYes)
        $Script:SyncHT.formMain.Controls.Add($Script:SyncHT.radiobuttonNo)

        $Script:SyncHT.formMain.ShowDialog()
    })

    $PowerShellCmd.Runspace = $RunSpace
    $handle = $PowerShellCmd.BeginInvoke()

    while ( -Not $handle.IsCompleted ) {
        #confirm form completely loads before executing other code
        if ( $Script:SyncHT.firstIteration -eq 1 ) {
            Start-Sleep -Seconds 1
            #change commenting on the next two lines for testing
            $Script:SyncHT.firstIteration = 0
            #$Script:SyncHT.firstIteration = 2
        } else {
            if ( $Script:SyncHT.firstIteration -eq 0 ) {
                $Script:SyncHT.radiobuttonYes.Visible = $true
                $Script:SyncHT.radiobuttonNo.Visible = $true
                $Script:SyncHT.formMain.Refresh()
                $Script:SyncHT.firstIteration = 2
            }
            Start-Sleep -Milliseconds 100
        }
    }
    $PowerShellCmd.EndInvoke($handle)
    $RunSpace.Close()
    $PowerShellCmd.Dispose()
}

$Script:SyncHT = [Hashtable]::Synchronized(@{})
$Script:SyncHT.firstIteration = 1
Load-Form


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>