Button Generating question

Hello, I have been working on a powershell gui app that gets all the session for putty that I have saved in the registry and creates a menu for them. I am able to get the list and assign it to a list object, but also want a button menu that has a clickable button for each session. I am able to generate and name the buttons, but when I try to assign the click cmdlet to it, it assigns the same value to every button, which makes it so every button opens the same session, the last button created. All the variables within the cmdlet, except for $i, are passed into the code from a previous function. Code is below:


Function Menu_Select {

$objForm3 = New-Object System.Windows.Forms.Form 
$objForm3.Text = "Operator Menu"
$objForm3.Size = New-Object System.Drawing.Size(600,1000) 
$objForm3.StartPosition = "CenterScreen"

$w=10
$v=10
get-childitem -Path HKCU:\Software\SimonTatham\PuTTY\Sessions -Name | Foreach-object {$y += $_}

ForEach($i in $y){

$w+=30
if ($w -gt 800)
{  
$v+=200
$w=10
}
$z = New-Object System.Windows.Forms.Button
$z.Location = New-Object System.Drawing.Size($v,$w)
$z.Size = New-Object System.Drawing.Size(175,23)
$z.Text = "$i"
$z.Add_Click({& 'C:\PuTTY\putty.exe' $ld $i $login $user $pw $pass})
$objForm3.Controls.Add($z)
}
$ReturnButton = New-Object System.Windows.Forms.Button
$ReturnButton.Location = New-Object System.Drawing.Size(150,920)
$ReturnButton.Size = New-Object System.Drawing.Size(200,23)
$ReturnButton.Text = "Return to Main"
$ReturnButton.Add_Click({$objForm3.Close()})
$objForm3.Controls.Add($ReturnButton)
$objForm3.Add_Shown({$objForm3.Activate()})
$objForm3.ShowDialog()

}

June 30th, 2015 5:25am

This line won't work:

get-childitem -Path HKCU:\Software\SimonTatham\PuTTY\Sessions -Name | Foreach-object {$y += $_}

Try this:

Function Menu_Select {
    $runCode={
        $arglist=@($ld, $this.Text, $login, $user, $pw, $pass)
        [void][System.Windows.Forms.MessageBox]::Show($this.Text)
        Start-Process -FilePath 'C:\PuTTY\putty.exe' -ArgumentList $arglist
    }
    
    $form = New-Object System.Windows.Forms.Form
    $form.Text = 'Operator Menu'
    $form.Size = '600, 1000'
    $form.StartPosition = 'CenterScreen'
    
    $w = 10
    $v = 10
    $y=(get-item -Path HKCU:\Software\SimonTatham\PuTTY\SshHostKeys).Property
    ForEach ($i in $y) {
        
        $w+=30
        if ($w -gt 800) {
            $v += 200
            $w = 10
        }
        $z = New-Object System.Windows.Forms.Button
        $z.Location = "$v, $w"
        $z.Size = '175, 23'
        $z.Text=$i
        $z.Add_Click($runCode)
        $form.Controls.Add($z)
    }
    
    $ReturnButton=New-Object System.Windows.Forms.Button
    $ReturnButton.Location ='150, 920'
    $ReturnButton.Size ='200, 23'
    $ReturnButton.Text ='Return to Main'
    $ReturnButton.DialogResult='Ok'
    $form.Controls.Add($ReturnButton)
    
    $form.Add_Shown({ $form.Activate() })
    $form.ShowDialog()
    
}


Free Windows Admin Tool Kit Click here and download it now
June 30th, 2015 7:42am

Here is an even easier way to populate a form.

function Menu_Select {
    
    Add-Type -AssemblyName System.Windows.Forms
    [System.Windows.Forms.Application]::EnableVisualStyles()
    $form1 = New-Object System.Windows.Forms.Form
    $flowlayoutpanel1 = New-Object System.Windows.Forms.FlowLayoutPanel
    $buttonOK = New-Object System.Windows.Forms.Button
    
    $runCode = {
        $arglist = @($ld, $this.Text, $login, $user, $pw, $pass)
        [void][System.Windows.Forms.MessageBox]::Show($this.Text)
        Start-Process -FilePath 'C:\PuTTY\putty.exe' -ArgumentList $arglist
    }
    
    $form1_Load = {
        
        (get-item -Path HKCU:\Software\SimonTatham\PuTTY\SshHostKeys).Property |
        ForEach-Object{
            $z = New-Object System.Windows.Forms.Button
            $z.Size = '175, 23'
            $z.Text = $_
            $z.Add_Click($runCode)
            $flowlayoutpanel1.Controls.Add($z)
        }
        
        
    }
    
    $form1.SuspendLayout()
    $form1.Controls.Add($flowlayoutpanel1)
    $form1.Controls.Add($buttonOK)
    $form1.AcceptButton = $buttonOK
    $form1.ClientSize = '284, 331'
    $form1.FormBorderStyle = 'FixedDialog'
    $form1.MaximizeBox = $False
    $form1.MinimizeBox = $False
    $form1.StartPosition = 'CenterScreen'
    $form1.Text = 'Operator Menu'
    $form1.add_Load($form1_Load)
    
    $flowlayoutpanel1.FlowDirection = 'TopDown'
    $flowlayoutpanel1.Location = '12, 12'
    $flowlayoutpanel1.Name = "flowlayoutpanel1"
    $flowlayoutpanel1.Size = '260, 276'
    $flowlayoutpanel1.TabIndex = 1
    $flowlayoutpanel1.WrapContents = $False
    
    $buttonOK.Anchor = 'Bottom, Right'
    $buttonOK.DialogResult = 'OK'
    $buttonOK.Location = '207, 308'
    $buttonOK.Name = "buttonOK"
    $buttonOK.Size = '75, 23'
    $buttonOK.TabIndex = 0
    $buttonOK.Text = '&OK'
    $buttonOK.UseVisualStyleBackColor = $True
    $form1.ResumeLayout()
    
    return $form1.ShowDialog()
    
}

Menu_Select

June 30th, 2015 8:16am

I am using powershell 1.0. I dont know if that has anything to do with the following line, but this line works fine.

get-childitem -Path HKCU:\Software\SimonTatham\PuTTY\Sessions -Name | Foreach-object {$y += $_}

 My issue is in applying a different command to each button. When I run what I have, the buttons generate, and each one has the proper test in it, but the click action that should be attached to each one is not supposed to be exactly the same. In the following line of code, $i should be changing every time is loops

$z.Add_Click({& 'C:\PuTTY\putty.exe' $ld $i $login $user $pw $pass})

but after the buttons generate, all buttons have the same value for $i in that snippet as the last run through the loop.



  • Edited by Jason D2 4 hours 10 minutes ago
Free Windows Admin Tool Kit Click here and download it now
June 30th, 2015 9:55pm

I answered that above. Re-asking the same question will not change the answer.

Part of the issue is that you are making a very common error in your understanding of how a loop works and how assignments work.  Try the code I posted until you understand why it works.

Beyond that we cannot be of more help.

June 30th, 2015 10:02pm

Thank you! I tried the first post you made, but it did not work. The second script that you gave me mostly worked and allowed me to see where i need to make the change. I also had been thinking of doing the flowcontrols things, but was trying to figure out my issue before adding new things.
Free Windows Admin Tool Kit Click here and download it now
July 1st, 2015 12:06am

I am using powershell 1.0. I dont know if that has anything to do with the following line, but this line works fine.

get-childitem -Path HKCU:\Software\SimonTatham\PuTTY\Sessions -Name | Foreach-object {$y += $_}

 My issue is in applying a different command to each button. When I run what I have, the buttons generate, and each one has the proper test in it, but the click action that should be attached to each one is not supposed to be exactly the same. In the following line of code, $i should be changing every time is loops

$z.Add_Click({& 'C:\PuTTY\putty.exe' $ld $i $login $user $pw $pass})

but after the buttons generate, all buttons have the same value for $i in that snippet as the last run through the loop.



  • Edited by Jason D2 Wednesday, July 01, 2015 2:58 AM
July 1st, 2015 1:51am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics