如何根据按钮的文本调整按钮的大小
在使用 C# + Winforms 翻译应用程序的过程中,我需要根据语言更改按钮的文本。
我的问题如下:
假设我想翻译“大家好!”中的按钮。到“Bonjour tout le monde”!
正如您所猜测的,如果我输入英文文本或法文文本,按钮的大小将不一样...我的问题很“简单”,我如何才能动态调整按钮的大小,以便文本适合其内容按钮?
到目前为止我得到了类似的东西!
【大家好!】
【大家好】
In the process of translating an application with C# + Winforms, I need to change a button's text depending on the language.
My problem is the following :
Let's say I want to translate a button from "Hi all!" to "Bonjour tout le monde" !
As you can guess, the button's size won't be the same if I enter english text or french one... My question is "simple", how can I manage to resize the button on the fly so the text fits its content in the button ?
So far I got something like that !
[Hi all!]
[Bonjour]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
绝对没有必要像其他发帖者所说的那样使用底层的 Graphics 对象。
如果将按钮的
AutoSize
属性设置为 true,将AutoSizeMode
设置为GrowAndShrink
,并将AutoEllipsis
设置为 false,则将自动调整大小以适合文本。话虽这么说,您可能需要进行多次布局调整才能使此更改适合您的 UI。您可以调整按钮的内边距以在文本周围添加空间,并且您可能希望将按钮放置在
TableLayoutPanel
(或其他内容)中,以防止它们在调整大小时重叠。编辑:
@mastro 指出:
AutoEllipsis
仅当AutoSize
为false
时才有效(如 文档),因此可以安全地忽略它只要其他三个属性设置正确。There's absolutely no need to use the underlying
Graphics
object as the other posters have said.If you set the button's
AutoSize
property to true, theAutoSizeMode
toGrowAndShrink
, and theAutoEllipsis
to false, it will resize automatically to fit the text.That being said, you may need to make several layout adjustments to make this change fit into your UI. You can adjust the button's padding to add space around the text, and you may want to place your buttons in a
TableLayoutPanel
(or something) to stop them from overlapping when they resize.Edit:
@mastro pointed out that:
AutoEllipsis
is only valid whenAutoSize
isfalse
(As explained in the documentation), so it can be safely ignored as long as the other three properties are set correctly.最好的选择是设置 AutoSize属性,如所述 ach 的答案
但是,如果 AutoSize 不适合您,则在代码中调整按钮大小非常简单。您只需设置按钮的宽度即可。诀窍是使其足够大以适合您的文本。
Your best bet is to set the AutoSize property as described ach's answer
However if AutoSize isn't working for you, resizing the button in code is easy enough. You can just need to set the button's width. The trick is making it big enough to fit your text.
试试这个:
Try this:
要使 WinForms 中的按钮根据文本的大小增大和/或缩小,您需要将按钮的
AutoSize
属性设置为True
并将AutoSizeMode< /code> 属性为
GrowAndShrink
。请注意,如果
AutoSizeMode
属性设置为GrowOnly
,则AutoSize
属性将仅允许按钮尺寸增大;通过将AutoSizeMode
属性更改为GrowAndShrink
,按钮现在将根据其Text
属性自动扩展或缩小宽度和高度。另请注意,在设置如上所示的两个属性时,您可以在 Text 属性中使用换行符(
Environment.NewLine
或vbCrLf
),按钮将缩小根据需要。To enable a Button in WinForms grow and/or shrink depending on the size of the Text, you need to set the button's
AutoSize
property toTrue
and theAutoSizeMode
property toGrowAndShrink
.Please note that the
AutoSize
property will only allow the button's size to grow if theAutoSizeMode
property is set toGrowOnly
; by changing theAutoSizeMode
property toGrowAndShrink
, the button will now automatically extend or reduce in width and height based on itsText
property.Also note that in setting the two properties as shown above, you can make use of new lines (
Environment.NewLine
orvbCrLf
) in the Text property and the button will scale down as needed.正如 Andrew Hanlon 所解释的,您可以设置
AutoSize = true
。这样做时,您还可以通过将按钮放置在 FlowLayoutPanel 上来自动获得完美的按钮布局。
当
FlowLayoutPanel
的FlowDirection
为LeftToRight
或RightToLeft
时,它们之间的水平距离将始终保持不变。您可以通过适当设置按钮的Margin
属性来调整此距离。您可以通过增加开始新组的按钮的左边距来创建按钮组。如果将按钮的
Dock
属性设置为DockStyle.Fill
,如果FlowDirection<
FlowLayoutPanel
的 /code> 是TopDown
或BottomUp
。As Andrew Hanlon explains, you can set
AutoSize = true
.When doing so, you can also attain a perfect layout of the buttons automatically by placing them on a
FlowLayoutPanel
.The horizontal distance between them will always stay the same when the
FlowDirection
of theFlowLayoutPanel
isLeftToRight
orRightToLeft
. You can adjust this distance by setting theMargin
property of the buttons appropriately. You can create groups of buttons by increasing the left margin of buttons beginning a new group.If you set the
Dock
property of the buttons toDockStyle.Fill
, they will even grow their width automatically in order to fit to the widest button if theFlowDirection
of theFlowLayoutPanel
isTopDown
orBottomUp
.除了按照其他答案中的建议将
AutoSize
设置为true
并将AutoSizeMode
设置为GrowAndShrink
之外,您还可以如果您设置了按钮图像,可能还需要设置TextImageRelation
属性,以便文本不会与图像重叠。In addition to setting the
AutoSize
totrue
and theAutoSizeMode
toGrowAndShrink
, as suggested in the other answers, you may also need to set theTextImageRelation
property, if you have set the button image, so that the text doesn't overlap the image.