2017-10-16 39 views
0

我已經在C#,HTML和Xojo中找到了這樣的方法,但不是Powershell/Windows Forms ...Powershell列表框項目項目文本的不同值?

我創建了一個Listbox供用戶選擇我們使用的軟件的位置代碼... while軟件安裝需要特定的代碼(01-07),我想向用戶顯示Listbox UI中的實際位置。這可能嗎?像$ listBox.Items.Add(Value =「01」Text =「NYC」)?

請參閱以下代碼:

$label = New-Object System.Windows.Forms.Label 
$label.Location = New-Object System.Drawing.Point(10,20) 
$label.Size = New-Object System.Drawing.Size(280,20) 
$label.Text = "Please select a Location Code:" 
$form.Controls.Add($label) 

$listBox = New-Object System.Windows.Forms.ListBox 
$listBox.Location = New-Object System.Drawing.Point(10,40) 
$listBox.Size = New-Object System.Drawing.Size(150,20) 
$listBox.Height = 140 

[void] $listBox.Items.Add("01") 
[void] $listBox.Items.Add("02") 
[void] $listBox.Items.Add("03") 
[void] $listBox.Items.Add("04") 
[void] $listBox.Items.Add("06") 
[void] $listBox.Items.Add("07") 

$form1.Controls.Add($listBox) 

$form1.Topmost = $True 

$result1 = $form1.ShowDialog() 

if ($result1 -eq [System.Windows.Forms.DialogResult]::OK) 
{ 
    $Server = $listBox.SelectedItem 
    $Server 
} 

回答

0

短的尋找到怎樣一個列表框的功能,你可以只讓一個哈希表,以友好的名稱轉換爲數字的軟件安裝的內部運作。

$locHash = @{ 
    'NYC' = '01' 
    'Chicago' = '02' 
    'LA' = '03' 
    'Seattle' = '04' 
    'Orlando' = '05' 
    'Dallas' = '06' 
} 

然後,您可以將友好名稱添加到列表框中,並在安裝軟件時引用散列表。

[void] $listBox.Items.Add("NYC") 
[void] $listBox.Items.Add("Chicago") 
[void] $listBox.Items.Add("LA") 
[void] $listBox.Items.Add("Seattle") 
[void] $listBox.Items.Add("Orlando") 
[void] $listBox.Items.Add("Dallas") 
$form1.Controls.Add($listBox) 

$form1.Topmost = $True 

$result1 = $form1.ShowDialog() 

if ($result1 -eq [System.Windows.Forms.DialogResult]::OK) 
{ 
    $Server = $locHash[$listBox.SelectedItem] 
    $Server 
} 
+0

天才。這工作很好。非常感謝!!我的Google-Fu讓我空手而歸。 –