0

我正在使用 .NET(实际上是 IronPython,但这不相关)编写一个 Windforms 应用程序,并且我的 GUI 中有一个 CheckedListBox 对象。

它工作正常,它在多列布局中有大约 20 个项目。但是我不知道如何给这个东西一个很好的内部边距——我想在复选框的顶部、底部、左侧和右侧边缘插入大约 20 或 30 像素的空白。

需要明确的是,我希望空白出现CheckedListBox 的边框和其中的 Checkboxes 之间,而不是在整个组件之外。

希望这是一个简单的答案,我只是想念它,因为我是 Windows 编程新手。如果不可能,我想这也很好,所以我不会再浪费时间了。

(如果我在 Swing (Java) 中这样做,我会寻找在我的组件上设置插图,或者可能建立一个复合边框,其中有一些空白空间。)

4

2 回答 2

0

本机 Window 控件不支持 Padding 属性,否则您无法说服它。不是真正的问题。只需将 BorderStyle 设置为 None 并将其放入 AutoScroll 属性为 True 的 Panel 中。您必须在表单的 Load 事件中设置列表框大小,因为它可能会重新缩放。哎呀,这看起来不对。那好吧。

于 2010-05-23T21:08:17.420 回答
0

对于希望在复选框周围添加空间的其他人,最简单的方法是使用 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);
        }
    }
于 2016-05-09T14:33:50.120 回答