Forum Discussion
Run sub command under other line
Hi thank you for replayManfred101 i mean after i select something from the list the command of stop-computer for example will running only for i marked
First: I don’t have a clue what you try to achieve. On your form there is one list whit multiple items and two buttons If you select for example “Phone 21” and then “OK”. You want to run a command Stop-computer…?
Second: Working whit Windows Forms is so much fun! But it can be challenging as well. I used Powershell studio in the past for making my forms. This is a more visual approach and gives you great insights on how you can use all Windows form elements, what their properties are, what methods to use etc. They have a 45 day trail and if you use it alongside your scripts it will help you a lot in getting started whit Windows Forms.
- KickAss1996May 16, 2020Copper Contributor
Manfred101 the stop-computer was an example, i mean that's my goal is to run a command only on one item from the list do not matter which item i pick.
if this is my list
phone 1
2
3
3...
i want when i click phone 1 the command run only for phone 1
and if i click on phone 3 the command run only for the phone 3
thank you!
- Manfred101May 16, 2020Iron Contributor
KickAss1996 The output in the code snippet below shows you a messagebox but you do whatever you want. Just create your own $ScriptToExecute scriptblock!
Goodluck!
Grtz, Manfred de Laat
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void] [System.Windows.Forms.Application]::EnableVisualStyles() $Form = New-Object system.Windows.Forms.Form $Form.Size = New-Object System.Drawing.Size(400,200) $Form.Width = 400 $Form.Height = 500 $form.MaximizeBox = $false $Form.StartPosition = "CenterScreen" $Form.FormBorderStyle = 'Fixed3D' $Form.Text = "My Application" $listBox = New-Object System.Windows.Forms.ListBox $listBox.Location = New-Object System.Drawing.Point(10,40) $listBox.Size = New-Object System.Drawing.Size(260,20) $listBox.Height = 80 [void] $listBox.Items.Add('Phone 10') [void] $listBox.Items.Add('Phone 11') [void] $listBox.Items.Add('Phone 12') [void] $listBox.Items.Add('Phone 13') [void] $listBox.Items.Add('Phone 14') [void] $listBox.Items.Add('Phone 15') [void] $listBox.Items.Add('Phone 16') $form.Controls.Add($listBox) $ScriptToExecute = { [System.Windows.MessageBox]::Show($listBox.SelectedItem) } $Okbutton = New-Object System.Windows.Forms.Button $Okbutton.Location = New-Object System.Drawing.Size(140,200) $Okbutton.Size = New-Object System.Drawing.Size(120,30) $Okbutton.Text = "Ok" $Okbutton.Add_Click($ScriptToExecute) $Form.Controls.Add($Okbutton) $Form.ShowDialog()