【问题标题】:Add checkboxes to Listbox with Powershell Windows Forms使用 Powershell Windows 窗体向列表框添加复选框
【发布时间】:2014-12-12 14:05:56
【问题描述】:

我正在根据我之前的帖子创建一个新问题:
Change the Color of Individual ListBox items in Powershell (Winforms)

我的以下代码运行良好(感谢 Micky Balladelli),但我的老板要求我添加复选框。如何调整此代码以添加一些复选框?

我在 MSDN 上的 ListBox Properties 中进行了搜索,但没有看到任何要添加复选框的属性。 也许使用 CheckedListBox 类会更好,但在这种情况下,显示我的颜色看起来很复杂。

我尝试使用 Listview 类,但这不是我所期望的。

任何帮助将不胜感激!

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

【问题讨论】:

  • 你需要使用ListBox吗?如果您只是将复选框添加到表单中,您可以使用 IF ($Checkbox.Text -eq 'locked') {$Checkbox.BackColor = [System.Drawing.Color]::yellowgreen} 例如。 (你当然需要先创建 $Checkbox 等等。)
  • 嗯,最重要的是拥有“颜色 + 复选框”,并为我的老板工作。我对某一特定课程没有任何偏好。感谢您的建议,请在下面查看我的回答。

标签: winforms powershell


【解决方案1】:

诀窍是使用 ListView 而不是 CheckedListBox。

以下包含完整的代码,并进行了相当多的更改。

function add {
 $status='logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','logged1','disconnected1','locked1','logged2','disconnected2','locked2','logged3','disconnected3','locked3'
 foreach ($s in $status)
 {
    $i =$listbox.Items.Add($s)
 }
}

$listBox_DrawItem={
param(
  [System.Object] $sender, 
  [System.Windows.Forms.DrawListViewItemEventArgs] $e
 )

 if ( $e.Item.text.contains('locked'))  
 { 
    $Color=[System.Drawing.Color]::yellowgreen       
    try
    {
      $brush = new-object System.Drawing.SolidBrush($Color)
      $e.Graphics.FillRectangle($brush, $e.Bounds)
    }
    finally
    {
      $brush.Dispose()
    }
 }
 else
 {
    $e.DrawBackground();
 }

 $e.DrawFocusRectangle();
 if ($e.Item.Checked)
 {
    [System.Windows.Forms.ControlPaint]::DrawCheckBox($e.Graphics,$e.Bounds.X, $e.Bounds.Top + 1, 15, 15, [System.Windows.Forms.ButtonState]::Checked)
 }
 else
 {
    [System.Windows.Forms.ControlPaint]::DrawCheckBox($e.Graphics,$e.Bounds.X, $e.Bounds.Top + 1, 15, 15, [System.Windows.Forms.ButtonState]::Flat)
 }

   [System.Drawing.font] $headerFont = new-object System.Drawing.font("Helvetica", 
            10, [System.Drawing.FontStyle]::Regular)

   $sf = new-object System.Drawing.StringFormat
   $sf.Alignment = [System.Drawing.StringAlignment]::Near

     $rect = New-Object System.Drawing.RectangleF
     $rect.x = $e.Bounds.X+16
     $rect.y = $e.Bounds.Top+1
     $rect.width = $e.bounds.right
     $rect.height = $e.bounds.bottom
   $e.Graphics.DrawString($e.item.Text, 
                          $headerFont, 
                          [System.Drawing.Brushes]::Black, 
                          $rect, 
                          $sf)
}     
$listBox_DrawSubItem={
param(
  [System.Object] $sender, 
  [System.Windows.Forms.DrawListViewSubItemEventArgs] $e
 )
}     

$listBox_DrawHeader={
param(
  [System.Object] $sender, 
  [System.Windows.Forms.DrawListViewColumnHeaderEventArgs] $e
 )

   [System.Drawing.font] $headerFont = new-object System.Drawing.font("Helvetica", 
            10, [System.Drawing.FontStyle]::Bold)
   $e.DrawBackground();
   $sf = new-object System.Drawing.StringFormat
   $sf.Alignment = [System.Drawing.StringAlignment]::Center

     $rect = New-Object System.Drawing.RectangleF
     $rect.x = $e.Bounds.X+16
     $rect.y = $e.Bounds.Top+1
     $rect.width = $e.bounds.right
     $rect.height = $e.bounds.bottom
     $e.Graphics.DrawString($e.Column.text,$e.Item.Font,[System.Drawing.Brushes]::Black, $rect )
   $e.Graphics.DrawString($e.Header.Text, 
                          $headerFont, 
                          [System.Drawing.Brushes]::Black, 
                          $rect, 
                          $sf)
}     



#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.Listview
$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)

$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 = "listview"
$listBox.view = [System.Windows.Forms.View]::Details
$listbox.CheckBoxes = $true
$listbox.fullrowselect = $true
$listBox.OwnerDraw = $true
$listBox.Add_DrawItem($listBox_DrawItem)
$listBox.Add_DrawSubItem($listBox_DrawSubItem)
$listBox.add_DrawColumnHeader($listBox_DrawHeader)
$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)
$listBox.Columns.Add("VMs", 300, [System.Windows.Forms.HorizontalAlignment]::Center) | out-null

$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    

【讨论】:

    猜你喜欢
    • 2019-01-31
    • 1970-01-01
    • 2013-02-06
    • 2018-03-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 2018-05-15
    • 1970-01-01
    相关资源
    最近更新 更多