チェックボックスの周りにスペースを追加しようとしている他の人にとって、最も簡単な方法は、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;
チェック項目を取得または設定するには:
// 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);
}
}