Powershell组合框项目可变 [英] powershell combobox items to variable

查看:61
本文介绍了Powershell组合框项目可变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Gui创建一个powershell脚本,该GUI为用户提供了可用驱动器的下拉列表,然后当用户选择驱动器并单击确定"时,该脚本将驱动器映射为M:\但是我无法解决如何将组合框中的所选项目传递到变量$ MapDrive

I am trying to create a powershell Script with a Gui that gives the user a drop list of available drives and then when the user selects the drive and clicks ok the script maps that drive as M:\ But I cant work out how to get the selected item in the combo box to be passed into the variable $MapDrive

#List of Drives To Map
$DriveList = @("0.  DGL_Data","1.  P1","2.  DGLFSG3","3.  p3 (TPC)","4.  p4","6.  p6","7.  p7","8.  p8","9.  p9","10. p10","11. p11","12. p12")
#Displays With Drive to Map
$MapDrive = convert.ToString($Dropdown.SelectedItem)

Function MapDrive {

If ($MapDrive -eq $DriveList[0]){
Write-Output Sucess > "C:\Users\andy.burton\Desktop\Practice Selector\Success.txt"}

ElseIf ($MapDrive -eq $DriveList[1]){
Write-Output BooYah > "C:\Users\andy.burton\Desktop\Practice Selector\Yes.txt"
}

Else {
Write-Output Failure > "C:\Users\andy.burton\Desktop\Practice Selector\Failed.txt"}
}

#test Function 
Function test {
Write-Output $MapDrive > "C:\Users\andy.burton\Desktop\Practice Selector\Success.txt"
}


#Function to Create Form
Function GenerateForm {

#Define Drive Selector Main Form
Add-Type -AssemblyName System.Windows.Forms
$DGL = New-Object system.Windows.Forms.Form 
$DGL.Text = "DGL Practice Manager"
$DGL.TopMost = $true
$DGL.BackgroundImage = [system.drawing.image]::FromFile("C:\Users\andy.burton\Desktop\Practice Selector\Images\medical.jpg")
$DGL.Icon = New-Object system.drawing.icon("C:\Users\andy.burton\Desktop\Practice Selector\Images\medical2.ico")
$DGL.Width = 600
$DGL.Height = 265
$DGL.MinimizeBox = $False
$DGL.MaximizeBox = $False

#Label to Display Instuctions
$label2 = New-Object system.windows.Forms.Label 
$label2.Text = "Select which drive"
$label2.BackColor = "#e4f3fa"
$label2.AutoSize = $true
$label2.Width = 25
$label2.Height = 10
$label2.location = new-object system.drawing.point(20,28)
$label2.Font = "Microsoft Sans Serif,10"
$DGL.controls.Add($label2) 

#Dropdown Box For Selecting Practice
$Dropdown = New-Object system.windows.Forms.ComboBox 
$Dropdown.BackColor = "#e4f3fa"
$DropDown.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList
$Dropdown.Width = 243
$Dropdown.Height = 20
$Dropdown.location = new-object system.drawing.point(21,73)
$Dropdown.Font = "Microsoft Sans Serif,10"
$DropDown.items.addrange($DriveList)
$DGL.controls.Add($Dropdown) 

#Cancel Button to Cancel drive Selection
$Cancelbutton = New-Object system.windows.Forms.Button 
$Cancelbutton.Text = "Cancel"
$Cancelbutton.Width = 60
$Cancelbutton.Height = 30
$Cancelbutton.location = new-object system.drawing.point(210,120)
$Cancelbutton.Font = "Microsoft Sans Serif,10"
$DGL.CancelButton = $Cancelbutton
$CancelButton.Add_Click({ $DGL.close();[System.Windows.Forms.Application]::Exit($null)})
$DGL.controls.Add($Cancelbutton) 

#OK Button to Select Drive
$OKbutton = New-Object system.windows.Forms.Button 
$OKbutton.Text = "OK"
$OKbutton.Width = 60
$OKbutton.Height = 30
$OKbutton.location = new-object system.drawing.point(140,120)
$OKbutton.Font = "Microsoft Sans Serif,10"
$DGL.AcceptButton = $OKbutton

$MapDrive = convert.ToString($Dropdown.SelectedItem)
#On click call PracticeSelectedCallBack to launch the application
$OKbutton.Add_Click({test ; $DGL.close()})
$DGL.controls.Add($OKbutton) 

    #Display the Form
$DGL.Add_Shown({$DGL.Activate()})
$DGL.ShowDialog()
}


 GenerateForm

我也想隐藏powershell窗口,而不是我尝试过的gui -hiddenwindow隐藏了,但隐藏了所有内容

I also want to hide the powershell window but not the gui I have tried -window hidden but that hid everything

推荐答案

ComboBox具有几个事件,您可以将其绑定到其中,这些事件可以完成各种操作.事件之一是SelectedIndexChanged.您可以将该事件添加到ComboBox对象中,并更新 $ MapDrive

The ComboBox has several events that you can tie into that will do various things. One of the events is a SelectedIndexChanged. You can add that event to your ComboBox object and update $MapDrive

此代码 $ MapDrive = convert.ToString($ Dropdown.SelectedItem)在初始编译期间仅触发一次.在运行时进行编译后,必须使用事件来触发代码更改.

This code $MapDrive = convert.ToString($Dropdown.SelectedItem) will only fire once during the initial compile. You have to use events to trigger code changes after compile during runtime.

此外,在Powershell中,您可以使用以下命令.gm 以获得对象的方法和属性的列表.您可以使用 [System.Windows.Forms.checkedlistbox] .GetEvents()来获取对象的事件列表.

Also, in Powershell you can use the following command [System.Windows.Forms.ComboBox] | gm to get a list of the methods, and properties of the object. You can use [System.Windows.Forms.checkedlistbox].GetEvents() to get a list of events of an object.

这篇关于Powershell组合框项目可变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆