问题转移 ListboxItem 并在哈希表中解决
Issue transfering ListboxItem and resolve in a Hashtable
为了更好地学习和理解 PowerShell,我正在尝试使用 PowerShell 中的表单构建词汇训练器。
词汇存储在不同的主题中,每个主题存储在哈希表中。
也许我以错误的方式处理这个问题,但在尝试使用列表框项时,我无法弄清楚如何以这种方式传输代码,使其进入正确的哈希表并显示第一个键。
我尝试组合几个字符串并尝试使用管道传输列表框项目。无论哪种方式我都失败了。请查看代码,以便我进一步解释。
CLS
Add-Type -assembly System.Windows.Forms
$Script:Counter = 1
$Weekdays = [ordered]@{Monday = 'Montag';Tuesday = 'Dienstag';Wednesday = 'Mittwoch'}
$Months = [ordered]@{January = 'Januar';February = 'Februar';March = 'März'}
$Numbers =[ordered]@{One = 'Eins';Zwei = 'Two';Drei = 'Three'}
$Vocabulary = @{Weekdays = '$Weekdays';Months = '$months';Numbers = '$Numbers'}
$Main_Form = New-Object System.Windows.Forms.Form
$Main_Form.Icon = $objIcon
$Main_Form.Text ='Test Test Test'
$Main_Form.Size = '1000,600'
$Main_Form.StartPosition = "CenterScreen"
$Main_Form.AutoSize = $true
$Main_Form.BringToFront()
$Label = New-Object System.Windows.Forms.label
$Label.Location = '10,20'
$Label.Size = '200,60'
$Label.Font = New-Object System.Drawing.Font("Times New Roman",16,[System.Drawing.FontStyle]::Bold)
$Label.BackColor = "Transparent"
$Label.ForeColor = "Blue"
$Label.Text = 'Counter '+$Script:Counter
$Main_Form.Controls.Add($Label)
$ListBox = New-Object System.Windows.Forms.ListBox
$ListBox.Location = '20,80'
$ListBox.Size = '300,50'
$ListBox.Font = New-Object System.Drawing.Font("Times New Roman",20,[System.Drawing.FontStyle]::Bold)
$ListBox.Height = 150
$ListBox.items.AddRange($Vocabulary.Keys)
$Main_Form.Controls.Add($ListBox)
$Labelannouncement = New-Object System.Windows.Forms.label
$Labelannouncement.Location = '400,80'
$Labelannouncement.Size = '400,60'
$Labelannouncement.Font = New-Object System.Drawing.Font("Times New Roman",18,[System.Drawing.FontStyle]::Bold)
$Labelannouncement.BackColor = "Transparent"
$Labelannouncement.ForeColor = "Black"
$Labelannouncement.Text = 'Nothing chosen yet'
$Main_Form.Controls.Add($Labelannouncement)
$LabelContent = New-Object System.Windows.Forms.label
$LabelContent.Location = '400,160'
$LabelContent.Size = '400,60'
$LabelContent.Font = New-Object System.Drawing.Font("Times New Roman",18,[System.Drawing.FontStyle]::Bold)
$LabelContent.BackColor = "Transparent"
$LabelContent.ForeColor = "Black"
$Main_Form.Controls.Add($LabelContent)
$Labeltest = New-Object System.Windows.Forms.label
$Labeltest.Location = '400,250'
$Labeltest.Size = '400,60'
$Labeltest.Font = New-Object System.Drawing.Font("Times New Roman",18,[System.Drawing.FontStyle]::Bold)
$Labeltest.BackColor = "Transparent"
$Labeltest.ForeColor = "Red"
$Labeltest.Text = 'Test'
$Main_Form.Controls.Add($Labeltest)
$Button = New-Object System.Windows.Forms.Button
$Button.Location = '50,250'
$Button.Size = '200,75'
$Button.Font = New-Object System.Drawing.Font("Arial",16,[System.Drawing.FontStyle]::Bold)
$Button.Text = 'Next'
$Main_form.Controls.Add($Button)
$ButtonClickEvent = {
$Script:Counter++
$Label.Text = "Counter $Script:Counter"
$Labelannouncement.Text = $Listbox.SelectedItems
$Labeltest.Text = ''
}
$Button.Add_Click($ButtonClickEvent)
$Main_Form.ShowDialog()
想法是,当 运行 脚本时,在列表框中,我可以从 3 个项目中进行选择。
目标是,一旦我选择了其中一个项目,我就想拥有相关哈希表的第一个键。
因此,当我选择“工作日”并按下“下一步”按钮时,红色测试应替换为 "Monday"。
哈希表名为“$Vocabulary”的原因是,获取值:
$Vocabulary.item($Listbox.SelectedItem)
但是问题来了。要将上面的示例与 "Monday" 一起使用,我必须使用以下代码:
$($months.Keys)[0]
这就是我正在解决的问题。
我是否必须通过管道传输或合并 listbox.item 的结果并将其传递,我该怎么做?
为了进行测试,我尝试将其作为文本添加到最后一行。
$ButtonClickEvent = {
$Script:Counter++
$Label.Text = "Counter $Script:Counter"
$Labelannouncement.Text = $Listbox.SelectedItems
$Labeltest.Text = ''
这样当按到工作日然后按下一步按钮时,我会收到星期一作为红色文本。
正在尝试
$({$Vocabulary.item($Listbox.SelectedItem)}.Values)[0]
确实给了我一个空数组错误的索引并使用管道,我还不明白我将如何转移到,所以它的作用是一样的:
$($months.Values)[0]
并尝试使用:
$($_.Values)[0]
没有帮助。有没有办法解决这个问题或者是错误的尝试?
感谢您的帮助,非常感谢
麦克
我不会在 $Vocabulary
哈希表中存储哈希表,而是在每个 Key:
下添加 arrays
$Weekdays = 'Montag','Dienstag','Mittwoch'
$Months = 'Januar','Februar','März'
$Numbers = 'Eins','Zwei','Drei'
$Vocabulary = @{Weekdays = $Weekdays; Months = $Months; Numbers = $Numbers}
接下来,将$ButtonClickEvent
改为
$ButtonClickEvent = {
$Script:Counter++
$Label.Text = "Counter $Script:Counter"
$Labelannouncement.Text = $Listbox.SelectedItem
$Labeltest.Text = $Vocabulary[$Listbox.SelectedItem][0]
}
$Listbox.SelectedItem
(单数)获取用户感兴趣的Vocabulary的键名。
$Vocabulary
在每个键下存储数组,你想显示第一个,所以只需使用第一项的索引 [0]。
P.S。完成后,不要忘记用 $Main_Form.Dispose()
清理表格
希望对您有所帮助
为了更好地学习和理解 PowerShell,我正在尝试使用 PowerShell 中的表单构建词汇训练器。
词汇存储在不同的主题中,每个主题存储在哈希表中。
也许我以错误的方式处理这个问题,但在尝试使用列表框项时,我无法弄清楚如何以这种方式传输代码,使其进入正确的哈希表并显示第一个键。
我尝试组合几个字符串并尝试使用管道传输列表框项目。无论哪种方式我都失败了。请查看代码,以便我进一步解释。
CLS
Add-Type -assembly System.Windows.Forms
$Script:Counter = 1
$Weekdays = [ordered]@{Monday = 'Montag';Tuesday = 'Dienstag';Wednesday = 'Mittwoch'}
$Months = [ordered]@{January = 'Januar';February = 'Februar';March = 'März'}
$Numbers =[ordered]@{One = 'Eins';Zwei = 'Two';Drei = 'Three'}
$Vocabulary = @{Weekdays = '$Weekdays';Months = '$months';Numbers = '$Numbers'}
$Main_Form = New-Object System.Windows.Forms.Form
$Main_Form.Icon = $objIcon
$Main_Form.Text ='Test Test Test'
$Main_Form.Size = '1000,600'
$Main_Form.StartPosition = "CenterScreen"
$Main_Form.AutoSize = $true
$Main_Form.BringToFront()
$Label = New-Object System.Windows.Forms.label
$Label.Location = '10,20'
$Label.Size = '200,60'
$Label.Font = New-Object System.Drawing.Font("Times New Roman",16,[System.Drawing.FontStyle]::Bold)
$Label.BackColor = "Transparent"
$Label.ForeColor = "Blue"
$Label.Text = 'Counter '+$Script:Counter
$Main_Form.Controls.Add($Label)
$ListBox = New-Object System.Windows.Forms.ListBox
$ListBox.Location = '20,80'
$ListBox.Size = '300,50'
$ListBox.Font = New-Object System.Drawing.Font("Times New Roman",20,[System.Drawing.FontStyle]::Bold)
$ListBox.Height = 150
$ListBox.items.AddRange($Vocabulary.Keys)
$Main_Form.Controls.Add($ListBox)
$Labelannouncement = New-Object System.Windows.Forms.label
$Labelannouncement.Location = '400,80'
$Labelannouncement.Size = '400,60'
$Labelannouncement.Font = New-Object System.Drawing.Font("Times New Roman",18,[System.Drawing.FontStyle]::Bold)
$Labelannouncement.BackColor = "Transparent"
$Labelannouncement.ForeColor = "Black"
$Labelannouncement.Text = 'Nothing chosen yet'
$Main_Form.Controls.Add($Labelannouncement)
$LabelContent = New-Object System.Windows.Forms.label
$LabelContent.Location = '400,160'
$LabelContent.Size = '400,60'
$LabelContent.Font = New-Object System.Drawing.Font("Times New Roman",18,[System.Drawing.FontStyle]::Bold)
$LabelContent.BackColor = "Transparent"
$LabelContent.ForeColor = "Black"
$Main_Form.Controls.Add($LabelContent)
$Labeltest = New-Object System.Windows.Forms.label
$Labeltest.Location = '400,250'
$Labeltest.Size = '400,60'
$Labeltest.Font = New-Object System.Drawing.Font("Times New Roman",18,[System.Drawing.FontStyle]::Bold)
$Labeltest.BackColor = "Transparent"
$Labeltest.ForeColor = "Red"
$Labeltest.Text = 'Test'
$Main_Form.Controls.Add($Labeltest)
$Button = New-Object System.Windows.Forms.Button
$Button.Location = '50,250'
$Button.Size = '200,75'
$Button.Font = New-Object System.Drawing.Font("Arial",16,[System.Drawing.FontStyle]::Bold)
$Button.Text = 'Next'
$Main_form.Controls.Add($Button)
$ButtonClickEvent = {
$Script:Counter++
$Label.Text = "Counter $Script:Counter"
$Labelannouncement.Text = $Listbox.SelectedItems
$Labeltest.Text = ''
}
$Button.Add_Click($ButtonClickEvent)
$Main_Form.ShowDialog()
想法是,当 运行 脚本时,在列表框中,我可以从 3 个项目中进行选择。 目标是,一旦我选择了其中一个项目,我就想拥有相关哈希表的第一个键。 因此,当我选择“工作日”并按下“下一步”按钮时,红色测试应替换为 "Monday"。
哈希表名为“$Vocabulary”的原因是,获取值:
$Vocabulary.item($Listbox.SelectedItem)
但是问题来了。要将上面的示例与 "Monday" 一起使用,我必须使用以下代码:
$($months.Keys)[0]
这就是我正在解决的问题。
我是否必须通过管道传输或合并 listbox.item 的结果并将其传递,我该怎么做? 为了进行测试,我尝试将其作为文本添加到最后一行。
$ButtonClickEvent = {
$Script:Counter++
$Label.Text = "Counter $Script:Counter"
$Labelannouncement.Text = $Listbox.SelectedItems
$Labeltest.Text = ''
这样当按到工作日然后按下一步按钮时,我会收到星期一作为红色文本。
正在尝试
$({$Vocabulary.item($Listbox.SelectedItem)}.Values)[0]
确实给了我一个空数组错误的索引并使用管道,我还不明白我将如何转移到,所以它的作用是一样的:
$($months.Values)[0]
并尝试使用:
$($_.Values)[0]
没有帮助。有没有办法解决这个问题或者是错误的尝试?
感谢您的帮助,非常感谢
麦克
我不会在 $Vocabulary
哈希表中存储哈希表,而是在每个 Key:
$Weekdays = 'Montag','Dienstag','Mittwoch'
$Months = 'Januar','Februar','März'
$Numbers = 'Eins','Zwei','Drei'
$Vocabulary = @{Weekdays = $Weekdays; Months = $Months; Numbers = $Numbers}
接下来,将$ButtonClickEvent
改为
$ButtonClickEvent = {
$Script:Counter++
$Label.Text = "Counter $Script:Counter"
$Labelannouncement.Text = $Listbox.SelectedItem
$Labeltest.Text = $Vocabulary[$Listbox.SelectedItem][0]
}
$Listbox.SelectedItem
(单数)获取用户感兴趣的Vocabulary的键名。$Vocabulary
在每个键下存储数组,你想显示第一个,所以只需使用第一项的索引 [0]。
P.S。完成后,不要忘记用 $Main_Form.Dispose()
希望对您有所帮助