繁体   English   中英

FlowLayoutPanel内文本框的自动宽度

[英]Automatic width of textbox inside a FlowLayoutPanel

在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
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;
    }

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

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

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM