FlowLayoutPanel内文本框的自动宽度

时间:2014-05-22 16:33:37

标签: c# .net vb.net winforms

在FlowLayoutPanel内部,我有一个标签和一个文本框。当我更改FlowLayoutPanel的宽度时,我还希望文本框宽度发生变化。这可能吗?

控件放置如下:

[FLOWLAYOUTPANEL [LABEL] [TEXTBOX]]

编辑:基于Jay Stratemeyer和Hans Passant的代码和建议,我能够有一个标签和一个文本框,可以调整其宽度并自动换行。这是代码:

    Private Sub FlowLayoutPanel1_Resize(sender As Object, e As System.EventArgs) Handles FlowLayoutPanel1.Resize
        Dim new_width As Integer = FlowLayoutPanel1.ClientSize.Width - LabelControl1.Width - LabelControl1.Margin.Left - LabelControl1.Margin.Right - TextBox1.Margin.Left - TextBox1.Margin.Right

        If new_width > Me.TextBox1.MinimumSize.Width Then
            Me.TextBox1.Width = new_width
        End If
    End Sub

2 个答案:

答案 0 :(得分:1)

public Form1()
    {
        InitializeComponent();
        this.Load +=new EventHandler(Form1_Load);
    }

    public int MyFlowPanelOriginalSize { get; set; }
    public int MyFlowPanelNewSize { get; set; }
    public int DifferenceInSizeOfPanel { get; set; }

    private void Form1_Load(object sender, EventArgs e)
    {
        MyFlowPanelOriginalSize = MyFlowPanel.Width;
        MyFlowPanel.Resize += new EventHandler(MyFlowPanel_Resize);
        DifferenceInSizeOfPanel = 0;
    }



    void MyFlowPanel_Resize(object sender, EventArgs e)
    {
        MyFlowPanelNewSize = MyFlowPanel.Width;
        DifferenceInSizeOfPanel = MyFlowPanelNewSize - MyFlowPanelOriginalSize;
        var TextBoxDifference = MyTextBox.Width + DifferenceInSizeOfPanel;
        MyTextBox.Width = TextBoxDifference;
        MyFlowPanelOriginalSize = MyFlowPanel.Width;
    }

答案 1 :(得分:1)

是的,这是TableLayoutPanel的默认行为,列设置为父级的50%。

您可以将标签列设置为固定大小(或自动 - 基于标签文本),将文本框设置为100%。这样,任何调整TableLayoutPanel的大小都会触发TextBox的大小调整,而不是其他任何内容。

enter image description here