淘宝店铺网站策划书,莱芜在线头条,宁波网络推广方案公司推荐,网站建设的潜规则1、命名空间与继承
命名空间#xff1a;System.Windows.Forms
继承#xff1a;Object→MarshalByRefObject→Component→Control→TextBoxBase→TextBox[RichTextBox] TextBox常用于从用户处获取简短的文本字符串#xff0c;而RichTextBox用于显 示和输入格式化的文本(例如…1、命名空间与继承
命名空间System.Windows.Forms
继承Object→MarshalByRefObject→Component→Control→TextBoxBase→TextBox[RichTextBox] TextBox常用于从用户处获取简短的文本字符串而RichTextBox用于显 示和输入格式化的文本(例如 黑体、 下划线和斜体)它使用标准的格式化文本称为Rich Text Format(富文本格式)或RTF。 2、常用属性
方法作用Clear()从文本框控件中清除所有文本CausesValidation设置为true且该控件获得焦点引发Validating和Validated两个事件CharacterCasing这个值表示TextBox是否会改变输入的文本的大小写MaxLengthTextBox输入最大长度MultiineTextBox是否可以多行PasswordChar是否为加密字符[UseSystemPasswordChar也可以]ReadOnlyTextBox是否只读ScrollBars是否显示滚动条SelectedText在文本框种选择的文本SelectionLength在文本框种选择的字符数SelectionStart被选中文本的开头WordWrap是否自动换行 注意RichTextBox中LoadFile()和SaveFile()保存富文本框中的文本格式。 3、常用事件
Event作用Clear()从文本框控件中清除所有文本OnTextChanged(EventArgs)从文本框控件中清除所有文本Enter从文本框控件中清除所有文本Leave从文本框控件中清除所有文本Validating从文本框控件中清除所有文本Validated从文本框控件中清除所有文本键事件–KeyDown按下对应键KeyUp抬起对应键KeyPress接受按下键对应建码 C#中Validating和Validated事件 如果 CausesValidation 属性设置为 false则将取消 Validating 和 Validated 事件 焦点事件按下列顺序发生 Enter //进入控件时发生 GotFocus //在控件接收焦点时发生 Leave //输入焦点离开控件时发生 Validating //控件数据效验时发生 Validated //数据效验完成后发生 LostFocus //失去焦点时发生 Validating 发生的时候值还没有真正存入。如果验证失败设置参数e.cancel true。 Validated 发生的时候值已经存入。 不管是Validating还是Validated事件发生的时候控件都没有失去焦点。 所以如果Validating验证错误e.cancel true会导致陷入死循环焦点始终在上面无法关闭软件。可以在Validating验证满足某条件令e.cancel false强制退出。
4、示例 注意: Validating事件中e.Cancel true; 可以取消操作。如不符合要求的输入取消。 //Validating事件textBox中无法输入大于10的数private void textBox2_Validating(object sender, CancelEventArgs e){Console.WriteLine(textBox2_Validating);if (Convert.ToInt32(this.textBox2.Text) 10){e.Cancel true; }}KeyPress事件 设置为Handled为true取消KeyPress事件。 这样控件就不处理键按下。 //KeyPress事件 只允许输入0-9的键盘数字或者BackSpace退格键private void textBox2_KeyPress(object sender, KeyPressEventArgs e){if ((e.KeyChar 48 || e.KeyChar 57) e.KeyChar ! 8){e.Handled true; // Remove the characterConsole.WriteLine(删除);}else{Console.WriteLine(保留);}}RichTextBox富文本控件中设置粗体斜体下划线 //设置粗体private void button5_Click(object sender, EventArgs e){Font oldfont this.richTextBox1.SelectionFont;Font newfont;if(oldfont.Bold){newfont new Font(oldfont,oldfont.Style ~FontStyle.Bold);}else{newfont new Font(oldfont, oldfont.Style | FontStyle.Bold);}this.richTextBox1.SelectionFont newfont;this.richTextBox1.Focus();}//设置斜体private void button7_Click(object sender, EventArgs e){Font oldfont;Font newfont;oldfont this.richTextBox1.SelectionFont;if (oldfont.Italic){newfont new Font(oldfont, oldfont.Style ~FontStyle.Italic);}else{newfont new Font(oldfont, oldfont.Style | FontStyle.Italic);}this.richTextBox1.SelectionFont newfont;this.richTextBox1.Focus();}//设置下划线private void button6_Click(object sender, EventArgs e){Font oldfont;Font newfont;oldfont this.richTextBox1.SelectionFont;if (oldfont.Underline){newfont new Font(oldfont, oldfont.Style ~FontStyle.Underline);}else{newfont new Font(oldfont,oldfont.Style | FontStyle.Underline);}this.richTextBox1.SelectionFont newfont;this.richTextBox1.Focus();//设置RichTextBook中间对齐private void button8_Click(object sender, EventArgs e){//剧中if(this.richTextBox1.SelectionAlignment HorizontalAlignment.Center){this.richTextBox1.SelectionAlignment HorizontalAlignment.Left;}else{this.richTextBox1.SelectionAlignment HorizontalAlignment.Center;}this.richTextBox1.Focus();}} RichTextBox保存文本格式 private void button9_Click(object sender, EventArgs e){// Load the file into the RichTextBox.try{this.richTextBox1.LoadFile(Test.rtf);}catch (System.IO.FileNotFoundException){MessageBox.Show(No file to load yet);}}private void button10_Click(object sender, EventArgs e){// Save the text.try{this.richTextBox1.SaveFile(Test.rtf);}catch (System.Exception err){MessageBox.Show(err.Message);}}