質問

flowlayoutpanelのflowlayoutpanel自動サイズに挿入されたアイテムを作成することは可能ですか?これが例です:

1 flowlayoutpanelと内部の3つのボタンを持つフォーム:

enter image description here

フォームをサイズ変更すると、コントロールは次のように見えます。

enter image description here

私が欲しいのはこれです:コントロールには、flowlayoutpanelの幅が必要です。

enter image description here

これを行う方法はありますか? FlowDirectionを変更し、アンカープロパティで遊んだが、運はありませんでした。

もちろん、flowlayoutpanel_resizeイベントのコントロールをサイズ変更できますが、約500のusercontrolsを追加したいと思います - テストしましたが、遅いです。

役に立ちましたか?

解決

この場合、1つの列でTableLayoutPanelを使用することをお勧めします。 TableLayoutPanelは、FlowLayoutPanelよりもはるかに予測可能で固体であることがわかりました。

別のオプションは、それでもFlowLayoutPanelを使用したい場合は、最初のコントロール幅を目的の幅に設定し、他のすべてのコントロールにDOCK = TOPを使用することです。

他のヒント

これを行う簡単な方法です。あなたの驚異的なエヴネントをflowlayoutpannelに結合し、含有コントロールをサイズ変更します。お気に入り:

private void myFlowLayoutPannel_SizeChanged(object sender, EventArgs e)
{
    myFlowLayoutPannel.SuspendLayout();
    foreach (Control ctrl in pnSMS.Controls)
    {
        if (ctrl is Button) ctrl.Width = pnSMS.ClientSize.Width;
    }
    myFlowLayoutPannel.ResumeLayout();
}

ここに私は私のスタックパネルのクラスを持っています:

/// <summary>
/// A stackpanel similar to the Wpf stackpanel.
/// </summary>
public class StackPanel: FlowLayoutPanel
{
    public StackPanel(): base()
    {
        InitializeComponent();
        this.ForceAutoresizeOfControls = true;
    }

    private void InitializeComponent()
    {
        this.SuspendLayout();
        //
        // StackPanel
        //
        this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
        this.WrapContents = false;
        this.ResumeLayout(false);
    }

    /// <summary>
    /// Override it just in order to hide it in design mode.
    /// </summary>
    [Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public new bool WrapContents
    {
        get { return base.WrapContents; }
        set { base.WrapContents = value; }
    }

    /// <summary>
    /// Override it just in order to set its default value.
    /// </summary>
    [DefaultValue(typeof(AutoSizeMode), "GrowAndShrink")]
    public override AutoSizeMode AutoSizeMode
    {
        get { return base.AutoSizeMode; }
        set { base.AutoSizeMode = value; }
    }

    /// <summary>
    /// Get or set a value that when is true forces the resizing of each control.
    /// If this value is false then only control that have AutoSize == true will be resized to
    /// fit the client size of this container.
    /// </summary>
    [DefaultValue(true)]
    public bool ForceAutoresizeOfControls { get; set; }

    protected override void OnSizeChanged(EventArgs e)
    {
        base.OnSizeChanged(e);
        this.SuspendLayout();
        switch (FlowDirection)
        {
            case FlowDirection.BottomUp:
            case FlowDirection.TopDown:
                foreach (Control control in this.Controls)
                    if (ForceAutoresizeOfControls || control.AutoSize)
                        control.Width = this.ClientSize.Width - control.Margin.Left - control.Margin.Right;
                break;
            case FlowDirection.LeftToRight:
            case FlowDirection.RightToLeft:
                foreach (Control control in this.Controls)
                    if (ForceAutoresizeOfControls || control.AutoSize)
                        control.Height = this.ClientSize.Height - control.Margin.Top - control.Margin.Bottom;
                break;
            default:
                break;
        }
        this.ResumeLayout();
    }

    protected override void OnLayout(LayoutEventArgs levent)
    {
        base.OnLayout(levent);

        if (levent != null && levent.AffectedControl != null)
        {
            Control control = levent.AffectedControl;
            if (ForceAutoresizeOfControls || control.AutoSize)
            {
                switch (FlowDirection)
                {
                    case FlowDirection.BottomUp:
                    case FlowDirection.TopDown:
                        control.Width = this.ClientSize.Width - control.Margin.Left - control.Margin.Right;
                        break;
                    case FlowDirection.LeftToRight:
                    case FlowDirection.RightToLeft:
                        control.Height = this.ClientSize.Height - control.Margin.Top - control.Margin.Bottom;
                        break;
                    default:
                        break;
                }
            }
        }
    }
}

FlowLayoutPanel によれば、特定の方法でコントロールを配置します MSDN:

...垂直フローの方向については、flowlayoutpanelコントロールが暗黙の列の幅を計算します 列で最も広い子コントロールから. 。この列の他のすべてのコントロール アンカー また ドックプロパティは、この暗黙の列に適合するように整列または伸びます。動作は、水平フロー方向に対して同様の方法で機能します。

理想的ではありませんが、1つの子供のコントロールがコンテナと同じ幅に設定され、残りのコントロールがに設定されている限り、これをネイティブに行うことができます。 Dock.

私は提案します...ボタンのアンカーで遊んでみてください...それをとして設定してみてください

Button1.Anchor = (AnchoreStyle.Left or AnchoreStyle.Right)

またはプロパティに設定してください...

そして、それをflowlayoutpanelの代わりにパネルの内側に置いてください...;)

aは必要ありません FlowLayoutPanel ここ。

あなたはあなたが正常に望むことをすることができるはずです Panel コントロール。フォームで伸びるように4つの側面すべてに固定し、ボタンを追加して、すべてをドックに設定します。

仕事が終わった。

他の回答が述べたように、パネル自体はボタンを処理するのに十分です。私のために働くコードのビット:

public class ButtonWindow : Panel
{
    public ButtonWindow()
    {
        Dock = DockStyle.Fill;
        AutoScroll = true;

        for (int i = 0; i < 500; i++) 
        {
           Button button = new Button() { Height = 100, Dock = DockStyle.Top };
           Controls.Add(button);
        }
    }
}

良い1日を。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top