C# textBox用法总结 [持续更新]

来源:互联网 发布:淘宝店铺招牌素材 编辑:程序博客网 时间:2024/06/15 21:28

textBox用法总结:

1 去掉边框 【注:this = textBox】this.BorderStyle = System.Windows.Forms.BorderStyle.None;
2 设置字体 this.Font = new System.Drawing.Font(this.Font.Name, 11);
3 设置光标this.Focus();//光标移动到当前textBox中this.SelectAll(); //选中所有内容this.SelectionStart = this.Text.Length;///设置光标在末尾 
4 设置大小this.Size = new System.Drawing.Size(30, 25);
5 设置最大字符个数this.MaxLength = 3; //最大为3个
6 添加控件,设置控件位置Label  lb =  new Label();this.Controls.Add(lb);//添加一个Labellb.Location = new System.Drawing.Point(10,10);
7 设置字体居中this.TextAlign = HorizontalAlignment.Center;
8 控制输入内容 --- 重写textBox事件protected override void OnKeyPress(KeyPressEventArgs e){    //阻止键盘输入e.KeyChar.ToString()按键字符    e.Handled = true;    if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == (char)8))    {     //只能输入0 -9     e.Handled = false;     return ;    }}

9 设置多行 Multiline属性  True 允许多行  false 单行
10 单行编辑模式下,设置密码显示字符  设置PassworldChar属性为 *
11 设置滚动边框 ScrollBars 属性,默认为 None  Vertical竖直  Horizontal水平
<p>12 设置下拉框始终在最下面</p><p align="left">   textBox.AppendText(“hello”);//赋值</p><p align="left">   textBox.Focus();</p>

13 下拉框现在光标处   textbox.Focus();//获取焦点   textbox.Select(textbox.length,0);//移动到最后   textbox.ScrollToCaret();//滚动到光标处



1 0
原创粉丝点击