如果自定义验证器为false,则停止在其执行,并专注于文本框 [英] stop execution in Custom validator if it false and focus on textbox

查看:137
本文介绍了如果自定义验证器为false,则停止在其执行,并专注于文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的先生,

我有两个文本框,分别为to_date和from_date.并且我比较两个文本框并获取值,但是它执行到我要停止的按钮的onclick旁边,因为如果to_date小于from日期,则to date变为null,并且专注于该文本框不会转到click事件按钮.

我用:

Dear sir,

i have two textboxes as to_date and from_date. and i compare both textboxes and get value but it execution goes next to onclick of button that i want to stop because if to_date is less than from date, the to date is become null as well as focus on that textboxes not goes to click event of button.

i use :

try
       {
           DateTime from = DateTime.ParseExact(txt_from_date.Text, "M/d/yyyy", theCultureInfo);
           DateTime to = DateTime.ParseExact(txt_to_date.Text, "M/d/yyyy", theCultureInfo);
           int result = DateTime.Compare(to, from);
           e.IsValid = result>0;
           if (e.IsValid == false)
           {
               txt_to_date.Text = "";
               txt_to_date.Focus();
           }
       }
       catch (Exception eq)
       {
           e.IsValid = false;
           txt_to_date.Text = "";
       }

推荐答案

我不确定这是否是您要找的东西.我进行了测试,并做了我认为您要完成的工作.过去尝试更改事件处理程序内部的UI时,我不得不做类似的事情.

I am not sure if this is what you are looking for or not. I tested and does what I believe you are trying to accomplish. I have had to do similar in the past when trying to change UI inside of a event handler.

//invokable method to set focus to textbox
void setfocus(object sender, EventArgs e)
{
    if (sender != null && sender.GetType().Equals(typeof(TextBox)))
    {
        ((TextBox)sender).Focus();
    }
}

private void button1_Click(object sender, EventArgs e)
{

    if (DateTime.Compare(DateTime.Now, DateTime.Now.AddHours(-3)) > 0)
    {
        //last date is previous to first date so we need to correct that.
        //begin invoke allows the button_click event delegate processing to finish before the method is invoked
        this.BeginInvoke(new EventHandler(setfocus), new object[] { textBox1, EventArgs.Empty });
        return; //drop out of button_click void
    }

    MessageBox.Show("How did we get here?");
}


这篇关于如果自定义验证器为false,则停止在其执行,并专注于文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆