简体   繁体   English

按文本内容自动调整面板

[英]Autosizing panel by text content

I have a WinForms-Application where I want to add UserControls dynamicly docking to the top:我有一个 WinForms 应用程序,我想在其中添加动态停靠到顶部的 UserControls:

this.Controls.Clear();
this.Controls.Add(myCustomControl(){Title="first", content="first text", Dock=DockStyle.Top});
this.Controls.Add(myCustomControl(){Title="second", content="very long text, where......", Dock=DockStyle.Top});

now myCostumControl [YELLOW] is a userControl with the following content:现在myCostumControl [YELLOW] 是具有以下内容的 userControl:

TopTitle [PINK]: A Label, docked to the top
BottomContent [GREEN]: A Panel, Fills out the rest of the Control below the TopTitle (Dockstyle Fill)
TextContent [BLUE]: A multiline Textbox, docked (fill) within the Panel.

So it looks like this:所以它看起来像这样:

在此处输入图片说明

Now what I need to achieve is that the Height from myCustomControl is according to the Text-Content of the "TextContent" - TextBox, so I can stack multiple Controls.现在我需要实现的是,来自 myCustomControl 的高度是根据“TextContent”-TextBox 的 Text-Content,所以我可以堆叠多个控件。 So if there is only a "Hello World" in it, the height should be small, if I put the Windows EULA in it it should be very long.所以如果里面只有一个“Hello World”,高度应该很小,如果我把Windows EULA放在里面,它应该很长。

I already tried messing around with all "AutoSize"-properties I could get my hands on, but the Textbox either disappeared completely or it hat no effect.我已经尝试过弄乱我可以使用的所有“AutoSize”属性,但是文本框要么完全消失,要么没有效果。

I also tried resizing the Textbox on Change:我还尝试在更改时调整文本框的大小:

Size size = TextRenderer.MeasureText(txtContent.Text, txtContent.Font);
txtContent.Height = size.Height; 

No success, either也没有成功

To make your composite control auto-size, perform these settings:要使复合控件自动调整大小,请执行以下设置:

  • Add a Label to user control and set AutoSize of label to false and set it's height to a suitable height and set its Dock to top.为用户控件添加一个Label并将标签的AutoSize设置为 false 并将其高度设置为合适的高度并将其Dock设置为顶部。
  • Add a TextBox to user control and set its Dock to Fill .TextBox添加到用户控件并将其Dock设置为Fill
  • override SetBoundsCore and calculate the preferred size of control:覆盖SetBoundsCore并计算控件的首选大小:

     protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) { var flags = TextFormatFlags.WordBreak | TextFormatFlags.NoPrefix; var proposedSize = new Size(width, int.MaxValue); var size = TextRenderer.MeasureText(textBox1.Text, textBox1.Font, proposedSize, flags); height = Math.Max(size.Height, textBox1.Font.Height) + label1.Height + 5; base.SetBoundsCore(x, y, width, height, specified); }
  • Handle TextChanged event of the TextBox to refresh size of control when content text changes:处理TextBox TextChanged事件以在内容文本更改时刷新TextBox大小:

     void textBox1_TextChanged(object sender, EventArgs e) { SetBoundsCore(Left, Top, Width, Height, BoundsSpecified.Size); }

Here is the result:结果如下:

在此处输入图片说明

If you want myCustomControl to be autosized, then obvioulsy, you cannot use fill docking for any child controls as docking set the size of the child according to parent size and you want parent size to adjust according to child size.如果您希望myCustomControl自动调整大小,那么显然,您不能对任何子控件使用填充停靠,因为停靠根据父级大小设置子级的大小,并且您希望父级大小根据子级大小进行调整。

So you should either use a table layout or a flow layout for the child.因此,您应该为孩子使用表格布局或流布局。 If you use a table, then, you have to use auto-size for the rows that should adapt.如果您使用表格,则必须对应该适应的行使用自动调整大小。

Then the whole layout control could be set to auto-size and should be docked at top (or maybe anchored).然后整个布局控件可以设置为自动调整大小,并且应该停靠在顶部(或者可能锚定)。

An you can the parent to display a vertical scrollbar if the layout control does not fit in visible area.如果布局控件不适合可见区域,您可以让父级显示垂直滚动条。

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

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