1、获取当前行第一个索引值
int index = textBox1.GetFirstCharIndexOfCurrentLine();
2、获取总行数
int line = textBox1.Lines.Length;
3、获取指定字符位置检索行号
int line = textBox1.GetLineFromCharIndex(index)+1;
4、获取列数
int column = textBox1.SelectionStart - index + 1;
//SelectionStart选定文本起始点
5、获取文本框内所有文字字符数
int words = textBox1.Text.Length;
6、获取文本框中选定字符数
int opt = textBox1.SelectionLength;
7、TextBox常用事件(鼠标,键盘)
8、按键条件设置
Convert.ToChar(e.KeyCode) == 13
//13表示Enter(回车)
9、追加文本
TextBox1.AppendText(“\r\n字符串”);
“\r\n” : 表示换行的意思
#region **按键代码**
if (Convert.ToChar(e.KeyCode) == 13)
{
}
if (Convert.ToChar(e.KeyCode) == 8 || Convert.ToChar(e.KeyCode) == 46)
{
}
#endregion
TextBox控件代码
private void textBox1_KeyUp(object sender, KeyEventArgs e)//键盘事件
{
//显示行,显示列
int line = 1,opt,words;
int index = textBox1.GetFirstCharIndexOfCurrentLine();//第一行索引值
line = textBox1.Lines.Length;//获取行数
int column = textBox1.SelectionStart - index + 1;//当前指定行起始点减去头索引值+1
opt = textBox1.SelectionLength;//获取选定的文本字符数
words = textBox1.Text.Length;//当前文本框总字数
toolStripStatusLabel3.Text = "第" + line + "行" + "," + "第" + column + "列";
if (textBox1.SelectionLength!=0)
{
toolStripStatusLabel2.Text = "当前字数:" + opt;
}
else
{
toolStripStatusLabel2.Text = "当前字数:" + words;
}
}
private void textBox1_MouseUp(object sender, MouseEventArgs e)//鼠标事件
{
int opt = 0,words = 0;
int index = textBox1.GetFirstCharIndexOfCurrentLine();//第一行索引值
int line = textBox1.GetLineFromCharIndex(index) + 1;//指定字符位置检索首索引值+1
int column = textBox1.SelectionStart - index + 1;//当前指定行减去头索引值+1
toolStripStatusLabel3.Text = "第" + line + "行" + "," + "第" + column + "列";
if (textBox1.SelectionLength!=0)
{
opt = textBox1.SelectionLength;//选中文本字符数
toolStripStatusLabel2.Text = "当前字数:" + opt;
}
else
{
words = textBox1.Text.Length;//当前文本框总字数
toolStripStatusLabel2.Text = "当前字数:" + words;
}
}