How to add a margin to a CheckedListBox in .NET?

99封情书 提交于 2019-12-02 18:04:35

问题


I'm writing an windforms application using .NET (actually IronPython, but that's not relevant), and I have a CheckedListBox object in my GUI.

It's working fine, it has about 20 items in a multicolumn layout. But I can't figure out how to give the thing a nice internal margin--I want to insert around 20 or 30 pixels of whitespace around the top, bottom, left, and right edges of the checkboxes.

To be clear, I want the whitespace to appear between the border of the CheckedListBox and the Checkboxes inside it, not outside the whole component.

Hopefully this is an easy answer, and I'm just missing it cause I'm new to programming in windows. If its not possible, I guess that'd be good to know too, so I don't waste anymore time with it.

(If I were doing this in Swing (Java) I would be looking to set the Insets on my component, or maybe build up a compound border with some empty space in it.)


回答1:


The native Window control doesn't support a Padding property, you cannot convince it otherwise. Not a real problem. Simply set the BorderStyle to None and put it in a Panel whose AutoScroll property is True. You'll have to set the list box size in the form's Load event because it might be rescaled. Yuck, that looks wrong. Oh well.




回答2:


For anyone else looking to add space around the checkboxes, the easiest way is to use DataGridView and make it look like CheckedListBox. Here is some of my designer code :

        // 
        // 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;

To get or set the Checked items:

    // 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);
        }
    }


来源:https://stackoverflow.com/questions/2893349/how-to-add-a-margin-to-a-checkedlistbox-in-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!