C# 如何将按钮文本拆分为两列
我有一个按钮,我想把文本和数字自动分开。 例如:文本=编程,数字=1354.25 然后,结果可以与按钮宽度相匹配。文本将在最左边,数字将在最右边C# 如何将按钮文本拆分为两列,c#,winforms,C#,Winforms,我有一个按钮,我想把文本和数字自动分开。 例如:文本=编程,数字=1354.25 然后,结果可以与按钮宽度相匹配。文本将在最左边,数字将在最右边 Result="Programming 1354.25". Result="Program 54.25". Result="C# 514754.25". 我不想添加空格,因为文本和数字中的字符数不同 var result = "Programming 1
Result="Programming 1354.25".
Result="Program 54.25".
Result="C# 514754.25".
我不想添加空格,因为文本和数字中的字符数不同
var result = "Programming 1354.25";
var pieces = result.Split(' ');
Debug.Write(pieces[pieces.Length - 1]);
这段代码基本上将结果拆分为字符串数组,然后输出该数组中的最后一个索引
这段代码基本上将结果拆分为字符串数组,然后输出该数组中的最后一个索引 如果您提到的结果是所需的结果,您可以计算生成恒定长度字符串所需的空格数,即(假设
Text
和number
都是string
s):
但是,如果您不使用固定大小的字体,这些字体将无法正确对齐。例如,
i
字符占用的水平空间小于W
字符。在这种情况下,您必须处理按钮的绘制事件,并分别绘制其中一个或两个文本。如果您提到的结果是所需的结果,您可以计算生成恒定长度字符串所需的空格数,即(假设文本
和数字
都是字符串
s):
但是,如果您不使用固定大小的字体,这些字体将无法正确对齐。例如,i
字符占用的水平空间小于W
字符。在这种情况下,您必须处理按钮的Paint
事件,并分别绘制其中一个或两个文本。我假设您有单词代码>按钮数据的KeyValuePair
项的数组。您有名称为button1
,button2
等的按钮
您需要做的第一步是找到应该在按钮上绘制的文本的最大宽度。没有空格,只有数据。您应该为此使用方法。您可以从OnPaint
处理程序参数获取Graphics
实例
之后,您可以计算应添加到其他按钮的空间量,以获得大致相同的绘制文本宽度
protected override void OnPaint (PaintEventArgs e)
{
base.OnPaint(e);
var words = new Dictionary<string, decimal> {
{ "Programming", 1345.25M },
{ "Program", 54.25M },
{ "C#", 342325.25M }
}.ToArray();
var g = e.Graphics;
var font = button1.Font;
var maxWidth = words.Max(x => g.MeasureString($"{x.Key}{x.Value}", font).Width);
SetButtonText(g, maxWidth, button1, words[0]);
SetButtonText(g, maxWidth, button2, words[1]);
SetButtonText(g, maxWidth, button3, words[2]);
}
private void SetButtonText(Graphics g,
float maxWidth, Button button, KeyValuePair<string, decimal> data)
{
var minSpacesCount = 5;
var spaceWidth = g.MeasureString(" ", button.Font).Width;
var initialTextWidth = g.MeasureString($"{data.Key}{data.Value}", button.Font).Width;
var spacesToAdd = minSpacesCount + (int)((maxWidth - initialTextWidth) / spaceWidth);
button.Text = $"{data.Key}{new String(' ', spacesToAdd)}{data.Value}";
}
protected override void OnPaint(PaintEventArgs e)
{
基础漆(e);
新字典{
{“编程”,1345.25百万},
{“计划”,5425万},
{“C#”,342325.25百万}
}.ToArray();
var g=e.图形;
var font=button1.font;
var maxWidth=words.Max(x=>g.MeasureString($“{x.Key}{x.Value}”,font.Width);
SetButtonText(g,最大宽度,按钮1,单词[0]);
SetButtonText(g,最大宽度,按钮2,单词[1]);
SetButtonText(g,最大宽度,按钮3,单词[2]);
}
私有void SetButtonText(图g,
浮点最大宽度、按钮按钮、KeyValuePair数据)
{
var MinSpaceScont=5;
var spaceWidth=g.MeasureString(“,button.Font).Width;
var initialTextWidth=g.MeasureString($“{data.Key}{data.Value}”,button.Font).Width;
var spacesToAdd=minspacescont+(int)((maxWidth-initialTextWidth)/spaceWidth);
Text=$“{data.Key}{newstring('',spacesToAdd)}{data.Value}”;
}
结果:
请注意,如果每个按钮上有不同的字体,这也不是问题。仅在计算最大宽度时,您需要使用每个按钮的字体,而不是使用第一个按钮的字体。我假设您的按钮数据有关键字对项的数组。您的按钮有名称按钮1
,按钮2
等
您需要做的第一步是找到应该在按钮上绘制的文本的最大宽度。没有空格,只有数据。您应该为此使用方法。您可以从OnPaint
处理程序参数获取Graphics
实例
之后,您可以计算应添加到其他按钮的空间量,以获得大致相同的绘制文本宽度
protected override void OnPaint (PaintEventArgs e)
{
base.OnPaint(e);
var words = new Dictionary<string, decimal> {
{ "Programming", 1345.25M },
{ "Program", 54.25M },
{ "C#", 342325.25M }
}.ToArray();
var g = e.Graphics;
var font = button1.Font;
var maxWidth = words.Max(x => g.MeasureString($"{x.Key}{x.Value}", font).Width);
SetButtonText(g, maxWidth, button1, words[0]);
SetButtonText(g, maxWidth, button2, words[1]);
SetButtonText(g, maxWidth, button3, words[2]);
}
private void SetButtonText(Graphics g,
float maxWidth, Button button, KeyValuePair<string, decimal> data)
{
var minSpacesCount = 5;
var spaceWidth = g.MeasureString(" ", button.Font).Width;
var initialTextWidth = g.MeasureString($"{data.Key}{data.Value}", button.Font).Width;
var spacesToAdd = minSpacesCount + (int)((maxWidth - initialTextWidth) / spaceWidth);
button.Text = $"{data.Key}{new String(' ', spacesToAdd)}{data.Value}";
}
protected override void OnPaint(PaintEventArgs e)
{
基础漆(e);
新字典{
{“编程”,1345.25百万},
{“计划”,5425万},
{“C#”,342325.25百万}
}.ToArray();
var g=e.图形;
var font=button1.font;
var maxWidth=words.Max(x=>g.MeasureString($“{x.Key}{x.Value}”,font.Width);
SetButtonText(g,最大宽度,按钮1,单词[0]);
SetButtonText(g,最大宽度,按钮2,单词[1]);
SetButtonText(g,最大宽度,按钮3,单词[2]);
}
私有void SetButtonText(图g,
浮点最大宽度、按钮按钮、KeyValuePair数据)
{
var MinSpaceScont=5;
var spaceWidth=g.MeasureString(“,button.Font).Width;
var initialTextWidth=g.MeasureString($“{data.Key}{data.Value}”,button.Font).Width;
var spacesToAdd=minspacescont+(int)((maxWidth-initialTextWidth)/spaceWidth);
Text=$“{data.Key}{newstring('',spacesToAdd)}{data.Value}”;
}
结果:
请注意,如果每个按钮上有不同的字体,这也不是问题。仅在计算最大宽度时,您需要使用每个按钮的字体,而不是第一个按钮的字体。WinForms?WPF?ASP.NET?如何实现此功能?button.Text=“Text”+numberofspaces+数字。numberofspaces必须是文本和数字之间的空白字符数。它是Winforms@XXX如果您想让我们回答您的问题,请先回答我们的问题。它是WinForms?WinForms?WPF?ASP.NET?如何实现此?按钮。Text=“Text”+numberofspaces+数字。numberofspaces必须是文本和数字之间的空白字符数。它是Winforms@XXX如果你想让我们回答你的问题,那就从回答我们的问题开始。是WinForms吗?