代码之家  ›  专栏  ›  技术社区  ›  Xanatos

如何在.NET中向选中的列表框添加边距?

  •  0
  • Xanatos  · 技术社区  · 16 年前

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

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

    为了清晰起见,我希望空白处出现 之间 checkedListBox的边框及其内部的复选框,而不是整个组件的外部。

    希望这是一个简单的答案,我只是错过了,因为我刚开始在Windows中编程。如果不可能的话,我想这也很好知道,所以我不会再浪费时间了。

    (如果我在Swing(Java)中这样做的话,我会想在我的组件上设置插图,或者在它里面建立一个有一些空格的复合边框。)

    2 回复  |  直到 10 年前
        1
  •  0
  •   Hans Passant    16 年前

    本机窗口控件不支持padding属性,否则无法说服它。不是真正的问题。只需将borderStyle设置为none,并将其放入AutoScroll属性为true的面板中。您必须在窗体的加载事件中设置列表框的大小,因为它可能被重新缩放。哎呀,看起来不对。哦,好吧。

        2
  •  0
  •   Slai    10 年前

    对于希望在复选框周围添加空间的其他人,最简单的方法是使用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);
            }
        }
    
    推荐文章