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;と、明らかに問題がどこにあるかが明らかになります。しかし、この問題の根本的な原因が何かを見つけることができないようです...リストボックスのメンバー関数を呼び出して、アイテムを再配置する必要がありますか?

更新 もう少しいじった後、デザイナーを使用してリストにアイテムを追加し、すべてのアンカーを設定し、フォームの端にスナップし、実行時にフォームのサイズを変更することでも、動作を再現できるようです。その後、再び、アイテムは再配置されません。ただし、リストビュー内のアイテムを強制的に再配置する方法にまだこだわっています。

4

2 に答える 2

1

今になって初めて、私の答えが実際に何をしているのかがわかります。基本的に、テキストに追加のラベルを使用してチェックボックスを再実装しました。右...

ソリューション 私のソリューションは、最終的にフローレイアウト パネルのいくつかのチェックボックスに要約されます。

于 2012-08-20T11:07:44.890 に答える
0

さらに調査し、私に提供されたコメントの後、標準コントロールを使用するという考えを取り下げました。理由は次のとおりです。

  • CheckedListBox : コントロールのサイズがプログラムによって変更されると、スクロールバーが再表示されるというバグがあります。
  • リスト モードの ListView : フォームのコンストラクタを過ぎると、アイコンを再配置できません。
  • SmallIcon モードの ListView : ceckbox が最初の列のコントロールの外側に配置されるバグがあります。

そこで、次の 2 つのソースを使用して独自のコントロールを作成することにしました。

自分自身を 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 を自動的に再調整して最適なフィットを提供しながら、コンテナーを適切に拡大および縮小することができます。

于 2012-08-20T07:15:54.427 に答える