从变量添加文本到标签

时间:2013-01-09 18:03:57

标签: powershell label

使用此代码,该功能使用vsphere cmd-let根据列表框中的选定VM检索CPU编号。输出正在进入另一个列表框但我希望能够将结果输出到标签

function fill_updatelistbox 
{
$vmlist1 = Get-vm -Name $listBox1.SelectedItem | select NumCPU
foreach ($vm1 in $vmlist1)
{
$label1.Text($vm1.NumCPU)
}
}


$label1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 400
$System_Drawing_Point.Y = 115
$label1.Location = $System_Drawing_Point
$label1.Name = "label1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 100
$label1.Size = $System_Drawing_Size
$label1.TabIndex = 0
$label1.Text = ""

编辑:以下代码使用列表框

function fill_updatelistbox
{
$vmlist1 = Get-vm -Name $listBox1.SelectedItem | select NumCPU
foreach ($vm1 in $vmlist1)
{
[void]$listBox2.Items.Add($vm1.NumCPU)
}
}

$tabPage2.Controls.Add($panel2)
$listBox2.DataBindings.DefaultDataSourceUpdateMode = 0
$listBox2.FormattingEnabled = $True
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 1
$System_Drawing_Point.Y = 1
$listBox2.Location = $System_Drawing_Point
$listBox2.Name = "listBox2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 369
$System_Drawing_Size.Width = 160
$listBox2.Size = $System_Drawing_Size
$listBox2.TabIndex = 0
$panel2.Controls.Add

1 个答案:

答案 0 :(得分:0)

仅供参考,我没有资源来测试这个,但我会尽力帮助。首先,确保列表框是单选的,因为标签更像是单项文本框:)然后你可以尝试:

function fill_updatelistbox 
{
#Should return a single VirtualMachine object
$vm = Get-vm -Name ($listBox1.SelectedItem.toString())
$label1.Text = $vm.NumCPU.toString()
}