2012-08-16 54 views
0

后,我试图让能够容纳字符串变量量,并调整它通过编程方式更改此控件的高度是高度动态控制。重新调整项目计划调整大小

到目前为止,我已经尝试了经过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;在类的构造函数会工作,所以显然这就是问题所在。我似乎无法找到这个问题的根源是什么......我应该调用列表框的某个成员函数来重新放置它的项目吗?

更新 摆弄经过一些似乎行为可以通过使用设计器将项目添加到列表中,将所有的主播也转载,捕捉到形式的边缘并在运行时调整形式。然后,再次,这些项目不会重新调整。仍然坚持如何强制重新定位列表视图中的项目。

+0

负载事件是否正在触发?如果是这样你尝试listView1.Refresh()? – Belmiris 2012-08-16 13:56:36

+0

@Belmiris是的,但问题不在于listview没有调整大小;它是完美的。列表视图中的项目保持原样:单行和控件外部。如果你能找到时间,只需将上面的代码粘贴到一个空白的窗体项目中,你就会明白我的意思了。 – Nebula 2012-08-16 14:03:36

+0

找不到解决方案 - 但使用不同的视图设置(如SmallIcon)不存在此问题。只是不要设置ImageList。 – 2012-08-16 20:10:33

回答

1

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

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

0

经过一些调查,并提供给我的意见,我用下降标准控件的想法。这里的原因:

  • CheckedListBox:具有这样当控件的大小以编程方式更改滚动条重新出现的错误。
  • 的ListView列表模式:不能重新排列图标时,过去式的构造。
  • 的ListView在SmallIcon模式:有一个地方的第一列控制之外的ceckbox的错误。

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

我自己做了一个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以提供最佳配合。