第二回 C#之TextBox的一般用法

1. 属性:BackColor,borderStyle,Multiline,,Font,ForeColor,readonly

textBox的宽带是根据字体的大小自动调整的

2.让焦点用于在textBox的开始位置: this.textBox1.SelectionStart = 0;

3.选择TextBox中的某些文字:

this.textBox1.Focus();

this.textBox1.SelectionStart = 0;

this.textBox1.SelectionLength = this.textBox1.Text.Length - 1;

4.选择TextBox中的全部文字:

this.textBox1.Focus();

this.textBox1.SelectAll();

5.当在TextBox上按键了Enter时候,做什么动作.在textBox1_KeyUp事件中:

if (e.KeyCode == Keys.Enter) { this.button1.Text = "哈t哈t";}

6.TextBox中只可以输入整数,在 textBox1_KeyPress事件中

if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8))//48代表0,57代表9,8代表空格,46代表小数点

{ e.Handled = true;}

7.TextBox中只可以输入数值: 在textBox1_KeyDown事件中

if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8)&&( e.KeyChar!=46)) //48代表0,57代表9,8代表空格,46代表小数点

{ e.Handled = true;}

8.让TextBox中模拟退格操作:

this.textBox1.Focus();

SendKeys.Send("{BACKSPACE}");

9.TextBox添加文本后自动滚动显示到最后一行,自动滚动到插入符号的位置, textBox1.ScrollToCaret();

10.TextBox中换行: Environment.NewLine