控件的焦点事件:Control .Validating 事件和Control.Validated 事件

当通过使用键盘(Tab、Shift+Tab 等)、通过调用 SelectSelectNextControl 方法或者通过将 ContainerControl ..::.ActiveControl 属性设置为当前窗体等方式更改焦点时,焦点事件按以下顺序发生:

  1. Enter

  2. GotFocus

  3. Leave

  4. Validating

  5. Validated

  6. LostFocus

当使用鼠标或通过调用 Focus 方法更改焦点时,焦点事件将按以下顺序发生:

  1. Enter

  2. GotFocus

  3. LostFocus

  4. Leave

  5. Validating

  6. Validated

如果 CausesValidation 属性设置为 false,则将取消 ValidatingValidated 事件。

如果在 Validating 事件委托中, CancelEventArgsCancel 属性设置为 true,则正常情况下将在 Validating 事件之后发生的所有事件均被取消。

private void textBox1_Validating(object sender, 
 				System.ComponentModel.CancelEventArgs e)
{
   string errorMsg;
   if(!ValidEmailAddress(textBox1.Text, out errorMsg))
   {
      // Cancel the event and select the text to be corrected by the user.
      e.Cancel = true;
      textBox1.Select(0, textBox1.Text.Length);

      // Set the ErrorProvider error with the text to display. 
      this.errorProvider1.SetError(textBox1, errorMsg);
   }
}

private void textBox1_Validated(object sender, System.EventArgs e)
{
   // If all conditions have been met, clear the ErrorProvider of errors.
   errorProvider1.SetError(textBox1, "");
}
public bool ValidEmailAddress(string emailAddress, out string errorMessage)
{
   // Confirm that the e-mail address string is not empty.
   if(emailAddress.Length == 0)
   {
      errorMessage = "e-mail address is required.";
         return false;
   }

   // Confirm that there is an "@" and a "." in the e-mail address, and in the correct order.
   if(emailAddress.IndexOf("@") > -1)
   {
      if(emailAddress.IndexOf(".", emailAddress.IndexOf("@") ) > emailAddress.IndexOf("@") )
      {
         errorMessage = "";
         return true;
      }
   }

   errorMessage = "e-mail address must be valid e-mail address format.\n" +
      "For example 'someone@example.com' ";
      return false;
}


以上转自微软文档。

 bof_.Code.controlBaseEdit.Validated+=new EventHandler(controlBaseEdit_CodeValidated);
  bof_.Code.controlBaseEdit.Validating+=new CancelEventHandler(controlBaseEdit_CodeValidating);

            void controlBaseEdit_CodeValidated(object sender, EventArgs e)
            {
                bof_.Code.controlBaseEdit.Text = "validated";
            }

            void controlBaseEdit_CodeValidating(object sender, CancelEventArgs e)
            {
                if (IsEditing())
                {
                         if (bof_.Code.controlBaseEdit.Text=="1")
                {
                    bof_.Code.controlBaseEdit.Text = "Vadating";
                }
                   
                }
            }

1.有一个Save方法,

2.更改这一个TextBox的值为1(TextBox获得焦点)

3.点击Save键(当前焦点更改为在Save键)

4.上面事件Validating先发生,Text=="1",符合条件,TextBox值变为"Validating",执行Save事件保存TextBox值1到DB(更改焦点时发生事件,在控件正在验证时发生,微软上面例子,验证邮箱不能通过,添加错误消息)

5.接着发生Validated事件,TextBox值改为"validated"但是保存操作没有执行其更改的值。(在控件完成验证时发生,看上面微软例子,所有条件都符合时清除错误消息)

6.

2.从第2步重新开始,TextBox值改为2,(TextBox获得焦点)

3.

4.Text=="2",不符合Validating里再次更改TextBox值条件,所以Text还是“2”,保存"2"到DB

5.TextBox值更改为Validated.

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
[23-05-29 20:39:13.243] {main} <proxy-cache> requires Resin Professional. Please see http://www.caucho.com for Resin Professional information and licensing. [23-05-29 20:39:13.400] {main} [23-05-29 20:39:13.400] {main} Resin-4.0.58 (built Fri, 24 Aug 2018 01:23:14 PDT) [23-05-29 20:39:13.401] {main} [23-05-29 20:39:13.402] {main} Windows 10 10.0 amd64 [23-05-29 20:39:13.402] {main} Java(TM) SE Runtime Environment 1.8.0_191-b12, GBK, zh [23-05-29 20:39:13.402] {main} Java HotSpot(TM) 64-Bit Server VM 25.191-b12, 64, mixed mode, Oracle Corporation [23-05-29 20:39:13.402] {main} [23-05-29 20:39:13.403] {main} user.name = WR182 [23-05-29 20:39:13.541] {main} [23-05-29 20:39:13.546] {main} server listening to 127.0.0.1:6800 [23-05-29 20:39:13.549] {main} [23-05-29 20:39:13.766] {main} Table[mnode:2,D:\WEAVER\Resin\resin-data\app-0\distcache\mnode.db] validating indexes due to unclean shutdown. [23-05-29 20:39:13.816] {main} Table[data:3,D:\WEAVER\Resin\resin-data\app-0\distcache\data.db] validating indexes due to unclean shutdown. [23-05-29 20:39:13.942] {main} [23-05-29 20:39:13.943] {main} resin.home = D:\WEAVER\Resin [23-05-29 20:39:13.943] {main} resin.root = D:\WEAVER\Resin [23-05-29 20:39:13.943] {main} resin.conf = d:\WEAVER\Resin\conf\resin.xml [23-05-29 20:39:13.943] {main} [23-05-29 20:39:13.943] {main} server = 127.0.0.1:6800 (app:app-0) [23-05-29 20:39:13.943] {main} stage = production [23-05-29 20:41:38.003] {main} Found library 'resin_os' as 'd:\WEAVER\Resin\win64\resin_os.dll', but the load failed. The JVM exception was: java.lang.UnsatisfiedLinkError: no resin_os in java.library.path [23-05-29 20:42:40.782] {resin-60} WebApp[production/webapp/default/lib] active [23-05-29 20:42:40.782] {main} Host[production/host/default] active [23-05-29 20:42:40.782] {main} ServletService[id=app-0,cluster=app] active [23-05-29 20:42:40.782] {main} [23-05-29 20:42:40.782] {main} http listening to *:8080 [23-05-29 20:42:40.782] {main} https listening to *:8444 [23-05-29 20:42:40.796] {main} [23-05-29 20:42:40.796] {main} Resin[id=app-0] started in 210426ms Connected to server [23-05-29 20:43:29.233] {resin-55} WebApp[production/webapp/default/ROOT] active
最新发布
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值