1个回答
展开全部
假设界面上已经有了一个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 + " 列";
}
/// <summary>
/// 获取字符串s的长度,包括字母,中文,特殊符号等
/// </summary>
/// <param name="s">要获取长度的字符串</param>
/// <returns>字符串的长度</returns>
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;
}
为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 + " 列";
}
/// <summary>
/// 获取字符串s的长度,包括字母,中文,特殊符号等
/// </summary>
/// <param name="s">要获取长度的字符串</param>
/// <returns>字符串的长度</returns>
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;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询