我正在C#中開發一個文本編輯器,並且我正在嘗試進行一次行計數。Richtextbox行數
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";
}
}
}
它工作正常,雖然你寫它們添加的行數,但如果你刪除一個,如果刪除或添加更多的行只會更新。
我想讓它瞬間。如果刪除一個,計數應立即減少。
'richTextBox.Lines.Count()'有什麼問題? – derape