C# 根据标签内容的宽度调整标签宽度
我正在开发一个代码编辑器,希望随着数字的增加自动调整标签的宽度。例如,对于1-9 1位数,有一个特定的宽度。然后当它达到10-99 2位时,标签的宽度增加。然后再次输入100-999 3位数字,以此类推 结果应该是这样的: 这是我的密码:C# 根据标签内容的宽度调整标签宽度,c#,winforms,width,label,C#,Winforms,Width,Label,我正在开发一个代码编辑器,希望随着数字的增加自动调整标签的宽度。例如,对于1-9 1位数,有一个特定的宽度。然后当它达到10-99 2位时,标签的宽度增加。然后再次输入100-999 3位数字,以此类推 结果应该是这样的: 这是我的密码: private void timer_countline_Tick(object sender, EventArgs e) { updateNumberLabel(); } private void updateNumberLabel() {
private void timer_countline_Tick(object sender, EventArgs e)
{
updateNumberLabel();
}
private void updateNumberLabel()
{
// we get index of first visible char and number of first visible line
Point pos = new Point(0, 0);
int firstIndex = rtb.GetCharIndexFromPosition(pos);
int firstLine = rtb.GetLineFromCharIndex(firstIndex);
// now we get index of last visible char and number of last visible line
pos.X = ClientRectangle.Width;
pos.Y = ClientRectangle.Height;
int lastIndex = rtb.GetCharIndexFromPosition(pos);
int lastLine = rtb.GetLineFromCharIndex(lastIndex);
// this is point position of last visible char, we'll use its Y value for calculating numberLabel size
pos = rtb.GetPositionFromCharIndex(lastIndex);
// finally, renumber label
numberLabel.Text = "";
for (int i = firstLine; i <= lastLine + 1; i++)
numberLabel.Text += i + 1 + "\n";
}
您可以使用TextRenderer执行您想要的操作。请检查以下代码行,您应该将代码行添加到标签的TextChanged事件中:
请记住,控件的AutoSize属性必须设置为False
这用于更改控件的宽度以适应其内容的宽度
这用于更改控件的高度以适应其内容的高度
更新1:
要更改面板宽度以水平显示其中的所有内容,可以使用以下代码行:
yourPanelName.Width = yourLabelName.Left + yourLabelName.Width;
yourPanelName.Height = yourLabelName.Top + yourLabelName.Height;
int yourLabelNameWidth = TextRenderer.MeasureText(yourLabelName.Text, yourLabelName.Font).Width;
yourSplitContainerName.SplitterDistance = yourLabelName.Left + yourLabelNameWidth;
yourLabelName.Width = yourLabelName.Left + yourLabelNameWidth;
要更改面板高度以不同方式显示其中的所有内容,可以使用以下代码行:
yourPanelName.Width = yourLabelName.Left + yourLabelName.Width;
yourPanelName.Height = yourLabelName.Top + yourLabelName.Height;
int yourLabelNameWidth = TextRenderer.MeasureText(yourLabelName.Text, yourLabelName.Font).Width;
yourSplitContainerName.SplitterDistance = yourLabelName.Left + yourLabelNameWidth;
yourLabelName.Width = yourLabelName.Left + yourLabelNameWidth;
更新2:
如果使用SplitContainer控件,则必须按以下方式更改SplitContainer的属性:
FixedPanel = none
IsSplitterFixed = False
然后,您可以使用以下代码行来实现所需:
要更改SplitContainer面板宽度以水平显示其中的所有内容,可以使用以下代码行:
yourPanelName.Width = yourLabelName.Left + yourLabelName.Width;
yourPanelName.Height = yourLabelName.Top + yourLabelName.Height;
int yourLabelNameWidth = TextRenderer.MeasureText(yourLabelName.Text, yourLabelName.Font).Width;
yourSplitContainerName.SplitterDistance = yourLabelName.Left + yourLabelNameWidth;
yourLabelName.Width = yourLabelName.Left + yourLabelNameWidth;
您可以使用TextRenderer执行您想要的操作。请检查以下代码行,您应该将代码行添加到标签的TextChanged事件中:
请记住,控件的AutoSize属性必须设置为False
这用于更改控件的宽度以适应其内容的宽度
这用于更改控件的高度以适应其内容的高度
更新1:
要更改面板宽度以水平显示其中的所有内容,可以使用以下代码行:
yourPanelName.Width = yourLabelName.Left + yourLabelName.Width;
yourPanelName.Height = yourLabelName.Top + yourLabelName.Height;
int yourLabelNameWidth = TextRenderer.MeasureText(yourLabelName.Text, yourLabelName.Font).Width;
yourSplitContainerName.SplitterDistance = yourLabelName.Left + yourLabelNameWidth;
yourLabelName.Width = yourLabelName.Left + yourLabelNameWidth;
要更改面板高度以不同方式显示其中的所有内容,可以使用以下代码行:
yourPanelName.Width = yourLabelName.Left + yourLabelName.Width;
yourPanelName.Height = yourLabelName.Top + yourLabelName.Height;
int yourLabelNameWidth = TextRenderer.MeasureText(yourLabelName.Text, yourLabelName.Font).Width;
yourSplitContainerName.SplitterDistance = yourLabelName.Left + yourLabelNameWidth;
yourLabelName.Width = yourLabelName.Left + yourLabelNameWidth;
更新2:
如果使用SplitContainer控件,则必须按以下方式更改SplitContainer的属性:
FixedPanel = none
IsSplitterFixed = False
然后,您可以使用以下代码行来实现所需:
要更改SplitContainer面板宽度以水平显示其中的所有内容,可以使用以下代码行:
yourPanelName.Width = yourLabelName.Left + yourLabelName.Width;
yourPanelName.Height = yourLabelName.Top + yourLabelName.Height;
int yourLabelNameWidth = TextRenderer.MeasureText(yourLabelName.Text, yourLabelName.Font).Width;
yourSplitContainerName.SplitterDistance = yourLabelName.Left + yourLabelNameWidth;
yourLabelName.Width = yourLabelName.Left + yourLabelNameWidth;
@GrantWinney,是的,问题是每次我使用timerevent时标签都会闪烁that@Elegiac自动调整大小应该可以。没有更好的方法了。@Elegiac如果您想创建一个订单列,我认为您应该将每个条目添加为一个标签,如果计时器间隔很小,updateNumbeLabel将使标签闪烁。我的意思是你应该改变你想要显示订单列的方式。如果您想保持当前的方式,您可能需要使用SuspendLayout和ResumeLayout方法。@KingKing您能详细说明一下吗?对不起,新手,非常感谢reply@GrantWinney是的,问题是每次我这么做的时候标签都会闪烁,因为我使用timereventthat@Elegiac自动调整大小应该可以。没有更好的方法了。@Elegiac如果您想创建一个订单列,我认为您应该将每个条目添加为一个标签,如果计时器间隔很小,updateNumbeLabel将使标签闪烁。我的意思是你应该改变你想要显示订单列的方式。如果您想保持当前的方式,您可能需要使用SuspendLayout和ResumeLayout方法。@KingKing您能详细说明一下吗?抱歉,这里的新手。非常感谢您的回复。我收到了一个错误,先生:splitContainer1.Panel1.Width=numberLabel.Left+numberLabel.Width-不支持,无法显式设置splitterpanel的宽度。在splitcontainer上设置splitterdistance而不是Head如果您检查更新2部分中的最后一行代码,您将看到您在注释中提到的内容。我发现此代码有一个错误,先生:splitContainer1.Panel1.Width=numberLabel.Left+numberLabel.Width-不支持,无法显式设置splitterpanel的宽度。在splitcontainer上设置splitterdistance,如果您检查更新2部分中的最后一行代码,您将看到您在注释中提到的内容。