vb.net 如何制作固定宽度的标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21438150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:37:05  来源:igfitidea点击:

vb.net How to make Fixed width Label?

vb.netwinformsvisual-studiolabel

提问by klados

enter image description here

在此处输入图片说明

Like the image above, I want to make a certain Label with fixed width without any scrollbar. It means, actually, the width of Label is changed with the size of outer Panel, however, the height of the Label is just fit to display the whole text inside.

像上图一样,我想制作一个没有任何滚动条的固定宽度的某个标签。也就是说,实际上Label的宽度是随着外层Panel的大小而变化的,而Label的高度正好适合在里面显示整个文本。

If I set the AutoSize property to True, I can't set the width of the Label.

如果我将 AutoSize 属性设置为 True,则无法设置标签的宽度。

Thank you!

谢谢!



The important thing is the '...XYZ' part that the HEIGHT of the Label is just fit to the end of the text. No more space or no less space.

重要的是 '...XYZ' 部分,标签的高度正好适合文本的末尾。没有更多的空间或没有更少的空间。

I want to make the height of the Label to be just fit to the text inside. The width is changing to fit the width of Form size, which is intended, while the height of the Label is constant, which is NOT.

我想让标签的高度正好适合里面的文字。宽度正在变化以适应 Form size 的宽度,这是预期的,而 Label 的高度是恒定的,这不是。

采纳答案by klados

I did a trick for the solution. (or maybe this is an ordinary solution)

我为解决方案做了一个技巧。(或者这可能是一个普通的解决方案)

I set the AutoSize property to true then wrapped the Label within a FlowLayoutPanel.

我将 AutoSize 属性设置为 true,然后将 Label 包装在 FlowLayoutPanel 中。

I anchored the Flowable Panel TOP,LEFT and RIGHT as well as set the AutoSize to true.

我将 Flowable Panel 固定在顶部、左侧和右侧,并将 AutoSize 设置为 true。

回答by krowe

You need to set 'Autosize' to false and then use the 'Anchor' property to pin the right side of the label (as well as the left and top which are already pinned by default).

您需要将 'Autosize' 设置为 false,然后使用 'Anchor' 属性固定标签的右侧(以及默认情况下已固定的左侧和顶部)。

回答by Steve

The Anchorproperty from MSDN

MSDN 中的Anchor属性

Use the Anchor property to define how a control is automatically resized as its parent control is resized. Anchoring a control to its parent control ensures that the anchored edges remain in the same position relative to the edges of the parent control when the parent control is resized.

使用 Anchor 属性定义控件如何在其父控件调整大小时自动调整大小。将控件锚定到其父控件可确保在调整父控件的大小时,锚定的边缘相对于父控件的边缘保持在相同的位置。

As you explain that this label is inside a panel, then you need to set the Anchor property also for the Panel inside the Form (the object that actually resizes itself). You could also look at the similar Dockproperty

当您解释此标签位于面板内时,您还需要为表单内的面板(实际调整自身大小的对象)设置 Anchor 属性。您还可以查看类似的Dock属性