Richtextbox行数 [英] Richtextbox lines count
本文介绍了Richtextbox行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用C#开发文本编辑器,并且试图进行行计数.
I'm developing a text editor in C#, and I'm trying to make a line count.
private void updateNumberLabel()
{
Point pos = new Point(0, 0);
int firstIndex = Document.GetCharIndexFromPosition(pos);
int firstLine = Document.GetLineFromCharIndex(firstIndex);
pos.X = ClientRectangle.Width;
pos.Y = ClientRectangle.Height;
int lastIndex = Document.GetCharIndexFromPosition(pos);
int lastLine = Document.GetLineFromCharIndex(lastIndex);
int actualLine = Document.GetLineFromCharIndex(actualPos);
pos = Document.GetPositionFromCharIndex(lastIndex);
if (lastLine != actualLine)
{
numberLabel.Text = "";
for (int i = firstLine; i <= lastLine + 1; i++)
{
numberLabel.Text += i + 1 + "\n";
}
}
}
它工作正常,并在您编写行时增加了行数,但是如果删除了一行,则只有在删除或再添加一行时才会更新.
It works fine and adds the number of lines while you write them, but if you delete one, it will only update if you delete or add one more line.
我要使其瞬时.如果删除一个,该计数应立即减少.
I want make it instantaneous. If you delete one, the count shall be decreased instantaneously.
推荐答案
也许这太简单了,但那又如何呢?
Maybe this is too easy, but what about that:
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
var lineCount = richTextBox.Lines.Count();
numberLabel.Text = lineCount.ToString();
}
确保将其分配给TextChanged
事件.
如果这不是您所需要的,请添加更多信息,以达到您的目标.
If this is not what you need, please add some more information what you are trying to achieve.
这篇关于Richtextbox行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文