对于希望在复选框周围添加空间的其他人,最简单的方法是使用 DataGridView 并使其看起来像 CheckedListBox。这是我的一些设计器代码:
//
// dgv1
//
this.dgv1.AllowUserToAddRows = false;
this.dgv1.AllowUserToDeleteRows = false;
this.dgv1.AllowUserToResizeColumns = false;
this.dgv1.AllowUserToResizeRows = false;
this.dgv1.BackgroundColor = System.Drawing.SystemColors.Control;
this.dgv1.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.dgv1.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.None;
this.dgv1.ColumnHeadersVisible = false;
this.dgv1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.dgvcChecked,
this.dgvcValue});
dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle3.BackColor = System.Drawing.SystemColors.Control;
dataGridViewCellStyle3.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
dataGridViewCellStyle3.ForeColor = System.Drawing.SystemColors.ControlText;
dataGridViewCellStyle3.SelectionBackColor = System.Drawing.SystemColors.Control;
dataGridViewCellStyle3.SelectionForeColor = System.Drawing.SystemColors.ControlText;
dataGridViewCellStyle3.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
this.dgv1.DefaultCellStyle = dataGridViewCellStyle3;
this.dgv1.Dock = System.Windows.Forms.DockStyle.Fill;
this.dgv1.EnableHeadersVisualStyles = false;
this.dgv1.Location = new System.Drawing.Point(7, 21);
this.dgv1.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.dgv1.Name = "dgv1";
this.dgv1.ReadOnly = true;
this.dgv1.RowHeadersVisible = false;
this.dgv1.RowTemplate.Height = 18;
this.dgv1.RowTemplate.ReadOnly = true;
this.dgv1.ShowCellErrors = false;
this.dgv1.ShowCellToolTips = false;
this.dgv1.ShowEditingIcon = false;
this.dgv1.ShowRowErrors = false;
要获取或设置 Checked 项目:
// gets or sets the checked items in dgv1 ( dgvcChecked.Index = 0, dgvcValue.Index = 1 )
public string[] pSelected {
get { return ( from DataGridViewRow r in dgv1.Rows
where r.Cells[dgvcChecked.Index].Value.Equals(true)
select r.Cells[dgvcValue.Index].Value as string ).ToArray();
}
set {
if (value != null && value.Length > 0)
foreach (DataGridViewRow r in dgv1.Rows)
r.Cells[dgvcChecked.Index].Value = value.Contains(r.Cells[dgvcValue.Index].Value as string);
}
}