如何根据按钮的文本调整按钮的大小

发布于 2024-09-27 21:39:34 字数 254 浏览 1 评论 0原文

在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

韶华倾负 2024-10-04 21:39:34

绝对没有必要像其他发帖者所说的那样使用底层的 Graphics 对象。

如果将按钮的 AutoSize 属性设置为 true,将 AutoSizeMode 设置为 GrowAndShrink,并将 AutoEllipsis 设置为 false,则将自动调整大小以适合文本。

话虽这么说,您可能需要进行多次布局调整才能使此更改适合您的 UI。您可以调整按钮的内边距以在文本周围添加空间,并且您可能希望将按钮放置在 TableLayoutPanel (或其他内容)中,以防止它们在调整大小时重叠。

编辑:
@mastro 指出: AutoEllipsis 仅当 AutoSizefalse 时才有效(如 文档),因此可以安全地忽略它只要其他三个属性设置正确。

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, the AutoSizeMode to GrowAndShrink, and the AutoEllipsis 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 when AutoSize is false (As explained in the documentation), so it can be safely ignored as long as the other three properties are set correctly.

红玫瑰 2024-10-04 21:39:34

最好的选择是设置 AutoSize属性,如所述 ach 的答案

但是,如果 AutoSize 不适合您,则在代码中调整按钮大小非常简单。您只需设置按钮的宽度即可。诀窍是使其足够大以适合您的文本。

   using(Graphics cg =  this.CreateGraphics())
   {
       SizeF size = cg.MeasureString("Please excuse my dear aunt sally",this.button1.Font);

       // size.Width+= 3; //add some padding .net v1.1 and 1.0 only
       this.button1.Padding = 3;
       this.button1.Width = (int)size.Width;

       this.button1.Text = "Please excuse my dear aunt sally";
   }

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.

   using(Graphics cg =  this.CreateGraphics())
   {
       SizeF size = cg.MeasureString("Please excuse my dear aunt sally",this.button1.Font);

       // size.Width+= 3; //add some padding .net v1.1 and 1.0 only
       this.button1.Padding = 3;
       this.button1.Width = (int)size.Width;

       this.button1.Text = "Please excuse my dear aunt sally";
   }
踏雪无痕 2024-10-04 21:39:34

试试这个:

Button.AutoSize = true;
Button.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowOnly;
Button.TextAlign = ContentAlignment.MiddleLeft;
Button.Padding = new Padding(0, 0, 0, 0);

Try this:

Button.AutoSize = true;
Button.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowOnly;
Button.TextAlign = ContentAlignment.MiddleLeft;
Button.Padding = new Padding(0, 0, 0, 0);
长亭外,古道边 2024-10-04 21:39:34

要使 WinForms 中的按钮根据文本的大小增大和/或缩小,您需要将按钮的 AutoSize 属性设置为 True 并将 AutoSizeMode< /code> 属性为 GrowAndShrink

// C#
btn.AutoSize = true;
btn.AutoSizeMode = AutoSizeMode.GrowAndShrink;

' VB.NET
btn.AutoSize = True
btn.AutoSizeMode = AutoSizeMode.GrowAndShrink

请注意,如果 AutoSizeMode 属性设置为 GrowOnly,则 AutoSize 属性将仅允许按钮尺寸增大;通过将 AutoSizeMode 属性更改为 GrowAndShrink,按钮现在将根据其 Text 属性自动扩展或缩小宽度和高度。

另请注意,在设置如上所示的两个属性时,您可以在 Text 属性中使用换行符(Environment.NewLinevbCrLf),按钮将缩小根据需要。

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 to True and the AutoSizeMode property to GrowAndShrink.

// C#
btn.AutoSize = true;
btn.AutoSizeMode = AutoSizeMode.GrowAndShrink;

' VB.NET
btn.AutoSize = True
btn.AutoSizeMode = AutoSizeMode.GrowAndShrink

Please note that the AutoSize property will only allow the button's size to grow if the AutoSizeMode property is set to GrowOnly; by changing the AutoSizeMode property to GrowAndShrink, the button will now automatically extend or reduce in width and height based on its Text property.

Also note that in setting the two properties as shown above, you can make use of new lines (Environment.NewLine or vbCrLf) in the Text property and the button will scale down as needed.

忆离笙 2024-10-04 21:39:34

正如 Andrew Hanlon 所解释的,您可以设置 AutoSize = true

这样做时,您还可以通过将按钮放置在 FlowLayoutPanel 上来自动获得完美的按钮布局。

FlowLayoutPanelFlowDirectionLeftToRightRightToLeft 时,它们之间的水平距离将始终保持不变。您可以通过适当设置按钮的 Margin 属性来调整此距离。您可以通过增加开始新组的按钮的左边距来创建按钮组。

如果将按钮的 Dock 属性设置为 DockStyle.Fill,如果 FlowDirection< FlowLayoutPanel 的 /code> 是 TopDownBottomUp

btn.AutoSizeMode = AutoSizeMode.GrowOnly;
btn.AutoSize = true;
btn.Dock = DockStyle.Fill;

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 the FlowLayoutPanel is LeftToRight or RightToLeft. You can adjust this distance by setting the Margin 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 to DockStyle.Fill, they will even grow their width automatically in order to fit to the widest button if the FlowDirection of the FlowLayoutPanel is TopDown or BottomUp.

btn.AutoSizeMode = AutoSizeMode.GrowOnly;
btn.AutoSize = true;
btn.Dock = DockStyle.Fill;
甜`诱少女 2024-10-04 21:39:34

除了按照其他答案中的建议将 AutoSize 设置为 true 并将 AutoSizeMode 设置为 GrowAndShrink 之外,您还可以如果您设置了按钮图像,可能还需要设置 TextImageRelation 属性,以便文本不会与图像重叠。

In addition to setting the AutoSize to true and the AutoSizeModeto GrowAndShrink, as suggested in the other answers, you may also need to set the TextImageRelation property, if you have set the button image, so that the text doesn't overlap the image.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文