[英]counting number of characters per each line(rich textbox) in statusstrip in c#
我幾乎已經使用c#設計了一個記事本。 但是,我現在面臨的唯一問題是我的身份證明。
我的需要-我想顯示每行的字符數。
當用戶按下回車鍵時,它應該進入新行,現在字符數應該從1開始。
從技術上講-Col = 1,Ln = 1; //(最初)Col =每行字符數Ln =行數當用戶按下Enter鍵-Ln = 2並繼續並且Col =我們在該特定行中鍵入的字符數我嘗試了這些代碼行-
private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
int count = Convert.ToInt32(e.KeyChar);
if (Convert.ToInt32(e.KeyChar) != 13)
{
Col = richTextBox1.Text.Length;
toolStripStatusLabel1.Text = "Col:" + Col.ToString() + "," + "Ln:" + Ln;
}
if (Convert.ToInt32(e.KeyChar) == 13)
{
//richTextBox1.Clear();
Ln = Ln + 1;
toolStripStatusLabel1.Text = "Col:" + Col.ToString() + "Ln:" + Ln;
}
}
假設您使用的是Windows Forms,則可以使用以下解決方案(但是您必須訂閱SelectionChanged
事件而不是富文本框控件的KeyPress
事件):
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
int currentIndex = richTextBox1.SelectionStart;
// Get the line number of the cursor.
Ln = richTextBox1.GetLineFromCharIndex(currentIndex);
// Get the index of the first char in the specific line.
int firstLineCharIndex = richTextBox1.GetFirstCharIndexFromLine(Ln);
// Get the column number of the cursor.
Col = currentIndex - firstLineCharIndex;
// The found indices are 0 based, so add +1 to get the desired number.
toolStripStatusLabel1.Text = "Col:" + (Col + 1) + " Ln:" + (Ln + 1);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.