【问题标题】:Realign items in Listview after programmatic resize编程调整大小后重新对齐 Listview 中的项目
【发布时间】:2012-08-16 13:41:12
【问题描述】:

我正在尝试制作一个可以容纳可变数量的字符串并通过以编程方式更改此控件的高度来动态调整其高度的控件。

到目前为止,我已经尝试过 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; 移动到类的构造函数中,它将起作用,很明显这就是问题所在。我似乎找不到这个问题的根本原因是什么......我应该调用列表框的一些成员函数来重新放置它的项目吗?

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

【问题讨论】:

  • 加载事件是否触发?如果是这样,您是否尝试过 listView1.Refresh()?
  • @Belmiris 是的,但问题不在于列表视图没有调整大小;它完美地做到了这一点。然而,列表视图中的项目保持原样:单行并且在控件之外。如果你能找到时间,只需将上面的代码复制粘贴到一个空的 windows 窗体项目中,你就会明白我的意思了。
  • 未找到解决方案 - 但使用不同的视图设置(如 SmallIcon)没有此问题。只是不要设置 ImageList。
  • 我一直在阅读更多的 MSDN,实际上,似乎没有解决方案。编写我自己的放置例程是行不通的,因为在列表模式下无法更改位置。 SmallIcon 本来是一个选项,它不是因为 SmallIcon 中有一个错误,它将复选框放在第一列的控件之外。我最好写我自己的清单。如果我有让我满意的东西,我会发布答案。谢谢!

标签: c# winforms listview dynamic resize


【解决方案1】:

直到现在我才意识到我的答案真正在做什么。我基本上使用文本的附加标签重新实现了复选框。对……

解决方案 我的解决方案最终归结为流程布局面板中的几个复选框。

【讨论】:

    【解决方案2】:

    经过更多调查和提供给我的 cmets,我放弃了使用标准控件的想法。原因如下:

    • CheckedListBox:有一个错误,即当控件的大小以编程方式更改时,滚动条会重新出现。
    • ListView 处于列表模式:通过表单的构造函数时无法重新对齐图标。
    • SmallIcon 模式下的ListView:有一个错误,将复选框置于第一列的控件之外。

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

    我给自己做了一个 CheckedLabel:

    设计师

    #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
    

    实施

    [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 以提供最佳拟合。

    【讨论】:

      猜你喜欢
      • 2015-07-02
      • 1970-01-01
      • 2016-04-27
      • 1970-01-01
      • 2012-10-29
      • 2013-10-07
      • 1970-01-01
      • 1970-01-01
      • 2014-03-25
      相关资源
      最近更新 更多