在 Powershell (Winforms) 中更改单个列表框项目的颜色 [英] Change the Color of Individual ListBox items in Powershell (Winforms)
问题描述
我正在努力为我的问题寻找解决方案,但没办法,我需要你的帮助.
I am trying to find a solution to my problem but no way, I need your help please.
我的目标:我想将列表框中包含字符串locked"的所有行都涂成绿色.下面的代码正在运行,但我需要修改它.
My goal : I want to colour in green all the lines of my listbox containing the string "locked". The below code is working but I need to modify that.
我想删除添加按钮,因此当我运行脚本时,表单会打开,所有内容都会自动填充为绿色.在这种情况下,变量应该是这样的:
I want to remove the add button so when I run the script, the form is opened and everything is automatically filled in green. In that case, the variable should be something like that :
$status='logged1','disconnected1','locked1','logged2','disconnected2','locked2','logged3','disconnected3','locked3'
$status='logged1','disconnected1','locked1','logged2','disconnected2','locked2','logged3','disconnected3','locked3'
$status='logged','disconnected','locked'
function add {
$index=Get-Random -max 3
$state = "$Index-$($Status[$index])"
$listbox.Items.Add($state)
}
$listBox_DrawItem={
param(
[System.Object] $sender,
[System.Windows.Forms.DrawItemEventArgs] $e
)
#Suppose Sender de type Listbox
if ($Sender.Items.Count -eq 0) {return}
#Suppose item de type String
$lbItem=$Sender.Items[$e.Index]
if ( $lbItem -match 'locked$')
{
$Color=[System.Drawing.Color]::yellowgreen
try
{
$brush = new-object System.Drawing.SolidBrush($Color)
$e.Graphics.FillRectangle($brush, $e.Bounds)
}
finally
{
$brush.Dispose()
}
}
$e.Graphics.DrawString($lbItem, $e.Font, [System.Drawing.SystemBrushes]::ControlText, (new-object System.Drawing.PointF($e.Bounds.X, $e.Bounds.Y)))
}
#Generated Form Function
function GenerateForm {
#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
#endregion
#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$add = New-Object System.Windows.Forms.Button
$listbox = New-Object System.Windows.Forms.ListBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects
#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.
$handler_form1_Load=
{
#TODO: Place custom script here
}
$handler_btnRechercher_Click=
{
add
#TODO: Place custom script here
}
$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
$form1.WindowState = $InitialFormWindowState
}
#----------------------------------------------
#region Generated Form Code
$form1.BackColor = [System.Drawing.Color]::FromArgb(255,240,240,240)
$form1.Text = "Move VM"
$form1.Name = "form1"
$form1.AutoScaleMode = 3
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.AutoScroll = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 357
$System_Drawing_Size.Height = 486
$form1.ClientSize = $System_Drawing_Size
$form1.add_Load($handler_form1_Load)
$add.TabIndex = 8
$add.Name = "add"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 130
$System_Drawing_Size.Height = 23
$add.Size = $System_Drawing_Size
$add.UseVisualStyleBackColor = $True
$add.Text = "add"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 104
$System_Drawing_Point.Y = 448
$add.Location = $System_Drawing_Point
$add.DataBindings.DefaultDataSourceUpdateMode = 0
$add.add_Click($handler_btnRechercher_Click)
$form1.Controls.Add($add)
$listbox.FormattingEnabled = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 330
$System_Drawing_Size.Height = 407
$listbox.Size = $System_Drawing_Size
$listbox.DataBindings.DefaultDataSourceUpdateMode = 0
$listbox.Name = "listbox"
$listBox.DrawMode = [System.Windows.Forms.DrawMode]::OwnerDrawFixed
$listBox.Add_DrawItem($listBox_DrawItem)
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 21
$listbox.Location = $System_Drawing_Point
$listbox.TabIndex = 4
$listbox.add_Click($action_si_click_sur_VMKO)
$form1.Controls.Add($listbox)
#endregion Generated Form Code
#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null
} #End Function
#Call the Function
GenerateForm
推荐答案
类似的事情?
function add {
$status='logged1','disconnected1','locked1','logged2','disconnected2','locked2','logged3','disconnected3','locked3'
foreach ($s in $status)
{
$listbox.Items.Add($s)
}
}
$listBox_DrawItem={
param(
[System.Object] $sender,
[System.Windows.Forms.DrawItemEventArgs] $e
)
#Suppose Sender de type Listbox
if ($Sender.Items.Count -eq 0) {return}
#Suppose item de type String
$lbItem=$Sender.Items[$e.Index]
if ( $lbItem.contains('locked'))
{
$Color=[System.Drawing.Color]::yellowgreen
try
{
$brush = new-object System.Drawing.SolidBrush($Color)
$e.Graphics.FillRectangle($brush, $e.Bounds)
}
finally
{
$brush.Dispose()
}
}
$e.Graphics.DrawString($lbItem, $e.Font, [System.Drawing.SystemBrushes]::ControlText, (new-object System.Drawing.PointF($e.Bounds.X, $e.Bounds.Y)))
}
#Generated Form Function
function GenerateForm {
#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
#endregion
#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$add = New-Object System.Windows.Forms.Button
$listbox = New-Object System.Windows.Forms.ListBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects
#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.
$handler_form1_Load=
{
#TODO: Place custom script here
}
$handler_btnRechercher_Click=
{
add
#TODO: Place custom script here
}
$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
$form1.WindowState = $InitialFormWindowState
}
#----------------------------------------------
#region Generated Form Code
$form1.BackColor = [System.Drawing.Color]::FromArgb(255,240,240,240)
$form1.Text = "Move VM"
$form1.Name = "form1"
$form1.AutoScaleMode = 3
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.AutoScroll = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 357
$System_Drawing_Size.Height = 486
$form1.ClientSize = $System_Drawing_Size
$form1.add_Load($handler_form1_Load)
$listbox.FormattingEnabled = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 330
$System_Drawing_Size.Height = 407
$listbox.Size = $System_Drawing_Size
$listbox.DataBindings.DefaultDataSourceUpdateMode = 0
$listbox.Name = "listbox"
$listBox.DrawMode = [System.Windows.Forms.DrawMode]::OwnerDrawFixed
$listBox.Add_DrawItem($listBox_DrawItem)
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 21
$listbox.Location = $System_Drawing_Point
$listbox.TabIndex = 4
$listbox.add_Click($action_si_click_sur_VMKO)
$form1.Controls.Add($listbox)
#endregion Generated Form Code
#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
add
$form1.ShowDialog()| Out-Null
} #End Function
#Call the Function
GenerateForm
这篇关于在 Powershell (Winforms) 中更改单个列表框项目的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!