Winform窗体学习笔记 第七篇 TextBox类

1. TextBox 属性:

  • CharacterCasing:这个值表示TextBox是否会改变输入的文本大小写
    • Lower,Normal,Upper
  • PasswordChar:设置密码。
  • Modifier:修饰语。
    • Public,Protected,Protected Internal,Internal,Private(默认)
  • MaxLength:指定输入文本的最大字符长度。
  • Multiline:多行显示。(bool)
  • ReadOnly:表示文本是否为只读。
  • WordWrap:指示多行文本框控件在必要时是否自动换行到下一行的开始。
  • SelectedText:在文本框中选择的文本。
  • SelectionLength:选择的字符数。
  • SelectionStart:被选中文本的开头。
  • ScrollBars:获取或设置哪些滚动条应出现在多行 TextBox 控件中。None, Vertical, Horizontal, Both
  • Lines:获取或设置文本框控件中的文本行。textBox1.Lines = new string[] { "abcd", "efg", "hijklmn" };

2. TextBox 事件:

  • TextChange:当Text内容发生变化的时候发生。
  • KeyPress:
    • KeyPressEventArgs 类 
    • private void textBoxAge_KeyPress(object sender, KeyPressEventArgs e)
    • (属性)KeyChar:获取或设置与按下的键对应的字符。
      • 8→退格  13→回车  48→0  65→A  97→a   
    • (属性)Handled:true,删除KeyPress的值,false则打印出KeyPress的值。
    • 复制代码

            private void textBoxSize_KeyPress(object sender, KeyPressEventArgs e)
            {
               // Remove all characters that are not numbers, backspace, or enter.
               if ((e.KeyChar < 48 || e.KeyChar > 57) &&
                                                      e.KeyChar != 8 && e.KeyChar != 13)
               {
                  e.Handled = true;
               }
               else if (e.KeyChar == 13)
               {
                  // Apply size if the user hits enter
                  TextBox txt = (TextBox)sender;
      
                  if (txt.Text.Length > 0)
                     ApplyTextSize(txt.Text);
                  e.Handled = true;
                  this.richTextBoxText.Focus();
               }
            }
  • Enter:接触时候出发
  • Leave:离开时候出发
  • Validating:CausesValidation属性设置为true时才会有机会触发
  • Validated:
  • → 这4个事件按照列出的顺序引发。它们统称为“焦点事件”,当控件的焦点发生改变时引发,但有两个例外。Validating 和 Validated仅在控件接收了焦点,且其CausesValidation属性设置为true时引发。接收焦点的控件引发事件的原因是有时即使焦点改变了,我们也不希望验证控件的有效性。它的一个示例是用户单击了Help按钮。
  • → private void textBoxOccupation_Validating(object sender, CancelEventArgs e)
  • → 其中sender就是通过TextBox显式转换的textBoxOccupation,可以这样定义:TextBox tb = (TextBox)sender;
  • → 然后可以像用textBoxOccupation一样的用tb。其他几个用法相似。
  • → 参考:http://blog.sina.com.cn/s/blog_6116673a0100fpeo.html
  • → 可以通过建立一个通用的Validating事件,然后通过调用,来实现其他窗口的Validating事件,实现代码的重用。

建立事件调用事件

public Form1()
      {
         InitializeComponent();

         this.buttonOK.Enabled = false;

         // Tag values for testing if the data is valid
         this.textBoxAddress.Tag = false;
         this.textBoxAge.Tag = false;
         this.textBoxName.Tag = false;
         this.textBoxOccupation.Tag = false;

         // Subscriptions to events
         // 调用textBoxEmpty_Validating方法,通过调用可以让这四个TextBox都实现此事件
         
         this.textBoxName.Validating += new CancelEventHandler(textBoxEmpty_Validating);
         this.textBoxAddress.Validating += new CancelEventHandler(textBoxEmpty_Validating);
         this.textBoxOccupation.Validating += new CancelEventHandler(textBoxOccupation_Validating);
         this.textBoxAge.Validating += new CancelEventHandler(textBoxEmpty_Validating);
      }

         // 定义方法,与Validating事件语法相同

      void textBoxEmpty_Validating(object sender, CancelEventArgs e)
      {
         // We know the sender is a TextBox, so we cast the sender object to that.
         TextBox tb = (TextBox)sender;

         // If the text is empty we set the background color of the 
         // Textbox to red to indicate a problem. We use the tag value
         // of the control to indicate if the control contains valid
         // information.
         if (tb.Text.Length == 0)
         {
            tb.BackColor = Color.Red;
            tb.Tag = false;

            // In this case we do not want to cancel further processing,
            // but if we had wanted to do this, we would have added this line:
            // e.Cancel = true;
         }
         else
         {
            tb.BackColor = System.Drawing.SystemColors.Window;
            tb.Tag = true;
         }

         // Finally, we call ValidateOK which will set the value of
         // the OK button.
         ValidateOK();
      }
  • SystemColors 类:每个属性都是Color结构,这种结构是Windows显示元素的颜色。(静态类)
    • Window:窗口工作区中背景的颜色。
  • Colors 结构:表示一种 ARGB 颜色(alpha、红色、绿色、蓝色)。
    • Red,Blue,Green。。。

 

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值