http://topic.csdn.net/t/20020601/23/771920.html

解决方案 »

  1.   

    应当使用Validating来组证数据,这个比其它的方法有更多的好处。比如可以设置某些按钮不触发这个事件,比如取消或关闭按钮或帮助按钮等没有必要引发。再就是可以使用e.Cancel=true来取消事件,使得焦点不会离开控件造成错误。
      

  2.   

    书上也说当焦点发生变化时,Validating接受焦点   焦点是什么??  是引发事件吗
      

  3.   

    有2个TextBox,一个是输入名字的TextBoxName,一个是输入地址的TextBoxAddress,为什么是ButtonOK这个控件的CausesValadation设置为TRUE呢
      

  4.   

    Validating事件就是当光标不在这个TextBox上了,例如鼠标从这个TextBox移去点另一个控件时,或按Tab键光标移到另一个TextBox时,就会触发Validating事件。
    TextChange事件是光标还在这个TextBox上, 正输入内容,文字改变时
      

  5.   

    二个人,男的。住在东边的喜欢放下放下,比如见着美女就“不要执着,放下放下”
    住在西边的呢,喜欢就追~东边的‘放下放下’的念着咒语,心里呢实在放不下,所以就性压抑了~晚上还经常看A片打手枪~
    西边的刚睡完一个漂亮女孩儿,第二天早上就又感到不满足了,开始寻找更完美的next one.
      

  6.   

    试了一下发现蛮有意思的。。Validating的时候,如果数据不合法可以设e.Cancle=true,让焦点保持在当前控件上其实基本和Leave事件差不多
      

  7.   

    有2个TextBox,一个是输入名字的TextBoxName,一个是输入地址的TextBoxAddress,为什么是ButtonOK这个控件的CausesValadation设置为TRUE呢
    ----------------------------------------------------
    如果不设置为true,就不会执行验证了,你试试看就知道了,当为false时,即使数据不符合要求,也一样提交页面了
      

  8.   

    如果不设置为true,就不会执行验证了,你试试看就知道了,当为false时,即使数据不符合要求,也一样提交页面了
    ----------------------------------------------------首先谢谢大哥回复但是上面说ButtonOK能不能提交是ButtonOK.Enable啊 如果是false就不能提交,如果是true的话就可以提交的 和Valadating有什么关系呢??
      

  9.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace TextBoxTest
    {
        public partial class TextBoxTest : Form
        {
            public TextBoxTest()
            {
                InitializeComponent();
                this.buttonOK.Enabled = false;            this.textBoxAddress.Tag = false;
                this.textBoxAge.Tag = false;
                this.textBoxName.Tag = false;            this.textBoxName.Validating += new CancelEventHandler(this.textBoxEmpty_Validating);
                this.textBoxAddress.Validating += new CancelEventHandler(this.textBoxEmpty_Validating);
                this.textBoxName.TextChanged += new System.EventHandler(this.textBox_TextChanged);
                this.textBoxAddress.TextChanged += new System.EventHandler(this.textBox_TextChanged);
                this.textBoxAge.TextChanged += new System.EventHandler(this.textBox_TextChanged);        }        private void buttonOK_Click(object sender, EventArgs e)
            {
                string output;
                output = "Name:" + this.textBoxName.Text + "\r\n";
                output += "Address:" + this.textBoxAddress.Text + "\r\n";
                output += "Occupation:" + (string)(this.checkBoxProgrammer.Checked ? "Programmer" : "Not a  Programmer") + "\r\n";
                output += "Sex:" + (string)(this.radioButtonFemale.Checked? "Female":"Male") + "\r\n";
                output += "Age:" + this.textBoxAge.Text + "\r\n";
                this.textBoxOutput.Text = output;
            }        private void buttonHelp_Click(object sender, EventArgs e)
            {
                string output;
                output = "Name =Your name\r\n";
                output +="Address = Your address\r\n";
                output += "Occupation = Check 'if u r a Programmer'";
                output += "Sex = Your sex\r\n";
                output += "Age = Your age\r\n";
                this.textBoxOutput.Text = output;
            }        private void textBoxEmpty_Validating(object sender,CancelEventArgs e)
            {
                TextBox tb = (TextBox)sender;
                if(tb.Text.Length == 0)
                {
                    tb.BackColor = Color.Blue;
                    tb.Tag = false;            }
               else
                {
                    tb.Tag = true;
                      tb.BackColor = System.Drawing.SystemColors.Window;            }
                ValidateOK();
            
            }        private void textBoxAge_KeyPress(object sender, KeyPressEventArgs e)
            {
                if ((e.KeyChar <48 || e.KeyChar >57)&& e.KeyChar != 8)
                    e.Handled = true;
            }
            private void textBox_TextChanged(object sender, System.EventArgs e)
            {
                TextBox tb = (TextBox)sender;
                if (tb.Text.Length == 0 )
                {
                    tb.BackColor = Color.Red;
                    tb.Tag = false;            }
                else 
                {
                    tb.BackColor = System.Drawing.SystemColors.Window;
                    tb.Tag = true;
                }
                ValidateOK();        }
               
            private void ValidateOK()
            {
                this.buttonOK.Enabled = ((bool)(this.textBoxAddress.Tag) &&(bool)(this.textBoxName.Tag) &&(bool)(this.textBoxAge.Tag));
            }
        }
    Validating这这个程序里是做什么用的??  书上说什么有效性,这个change事件完全可以做,多个Validating是干什么的.. 迷茫啊~`