首页 文章

程序化调整大小后重新调整Listview中的项目

提问于
浏览
0

我试图通过以编程方式更改此控件的高度来创建一个可以容纳可变数量的字符串并动态调整其高度的控件 .

到目前为止,我已经尝试了Checked Listbox和ListView .

列表框会一直显示水平滚动条,即使它已设置为false . ListView看起来更有前途,但它拒绝重新排列框中的项目:

public partial class Form1 : Form
{
    string[] content = 
        {
            "Lorem",
            "ipsum",
            "dolor",
            "sit",
            "amet",
            "consectetur",
            "adipiscing",
            "elit" ,
            "Integer" ,
            "commodo" ,
        };

    ListView listView1 = new ListView();

    public Form1()
    {
        InitializeComponent();

        listView1.Size = new System.Drawing.Size(300, 22);
        listView1.Font = new Font(FontFamily.GenericMonospace, 10);
        listView1.CheckBoxes = true;
        listView1.View = View.List;
        listView1.Scrollable = false;

        this.Controls.Add(listView1);

        groupBox1.Controls.Add(listView1);

        foreach (string str in content)
        {
            listView1.Items.Add(str.PadRight(12));
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        listView1.Height = 100;
    }
}

如果我将 listView1.Height = 100; 移动到类的构造函数中它会很明显地发现's where the problem lies. I can't似乎找到了这个问题的根本原因但是...我应该调用列表框的某个成员函数来重新放置它的项目吗?

Update 在摆弄了一些之后,似乎也可以通过使用设计器向列表中添加项目,设置所有锚点,捕捉到表单的边缘以及在运行时调整表单大小来重现行为 . 然后,再次,项目将不会重新对齐 . 仍然坚持如何强制重新定位列表视图中的项目 .

2 回答

  • 0

    直到现在我才意识到我的答案是真的在做什么 . 我基本上使用文本的附加标签重新实现了Checkbox . 对...

    Solution 我的解决方案最终归结为一个flowlayout面板中的几个复选框 .

  • 1

    经过一些调查和提供给我的评论后,我放弃了使用标准控件的想法 . 原因如下:

    • CheckedListBox :当编程控件的大小以编程方式更改时,会出现滚动条重新出现的错误 .

    • ListView in List mode :在过去表单的构造函数时无法重新对齐图标 .

    • ListView in SmallIcon mode :有一个错误,将ceckbox置于第一列的控件之外 .

    所以我决定使用这两个来源进行自己的控制:

    我自己做了一个CheckedLabel:

    Designer

    #region Component Designer generated code
    
    /// <summary>
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.cbCheckbox = new System.Windows.Forms.CheckBox();
        this.lblDisplay = new System.Windows.Forms.Label();
        this.SuspendLayout();
        // 
        // cbCheckbox
        // 
        this.cbCheckbox.AutoSize = true;
        this.cbCheckbox.Location = new System.Drawing.Point(3, 3);
        this.cbCheckbox.Name = "cbCheckbox";
        this.cbCheckbox.Size = new System.Drawing.Size(15, 14);
        this.cbCheckbox.TabIndex = 0;
        this.cbCheckbox.UseVisualStyleBackColor = true;
        this.cbCheckbox.CheckedChanged += new System.EventHandler(this.cbCheckbox_CheckedChanged);
        // 
        // lblDisplay
        // 
        this.lblDisplay.AutoSize = true;
        this.lblDisplay.Location = new System.Drawing.Point(24, 4);
        this.lblDisplay.Name = "lblDisplay";
        this.lblDisplay.Size = new System.Drawing.Size(35, 13);
        this.lblDisplay.TabIndex = 1;
        this.lblDisplay.Text = "label1";
        // 
        // CheckedLabel
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.AutoSize = true;
        this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
        this.Controls.Add(this.lblDisplay);
        this.Controls.Add(this.cbCheckbox);
        this.Name = "CheckedLabel";
        this.Size = new System.Drawing.Size(62, 20);
        this.ResumeLayout(false);
        this.PerformLayout();
    
    }
    
    #endregion
    

    Implementation

    [System.ComponentModel.DefaultEvent("CheckedChanged")]
    public partial class CheckedLabel : UserControl
    {
        public event EventHandler CheckedChanged;
    
        public CheckedLabel()
        {
            InitializeComponent();
        }
    
        public override string Text
        {
            get
            {
                return lblDisplay.Text;
            }
    
            set
            {
                lblDisplay.Text = value;
            }
        }
    
    
        private void cbCheckbox_CheckedChanged(object sender, EventArgs e)
        {
             // Pass the checkbox event as an ultra-shallow copy
            CheckBox b = new CheckBox();
            b.Checked = cbCheckbox.Checked;
            b.Text = lblDisplay.Text;
    
            CheckedChanged(b, e);
        }        
    }
    

    并将这些控件添加到FlowLayout面板 . 这个设置实际上允许我按照我认为合适的方式增长和缩小容器,同时自动重新对齐CheckedLabels以提供最佳匹配 .

相关问题