假设界面上已经有了一个richTextBox1控件,并且statusstrip已经添加了一个toolStripStatusLabel1
为richTextBox1添加SelectionChanged事件,事件代码如下:
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
int row = richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart) + 1;
int start = richTextBox1.GetFirstCharIndexOfCurrentLine();
string s = richTextBox1.Text.Substring(start, richTextBox1.SelectionStart - start);
int col = GetStringLen(s) + 1;
toolStripStatusLabel1.Text = "第 " + row + " 行, 第 " + col + " 列";
}
///
/// 获取字符串s的长度,包括字母,中文,特殊符号等
///
/// 要获取长度的字符串
///
private int GetStringLen(string s)
{
if (!string.IsNullOrEmpty(s))
{
int len = s.Length;
for (int i = 0; i < s.Length; i++)
{
if (s[i] > 255)
len++;
}
return len;
}
return 0;
}