关于TextBox中的Validating事件问题 http://topic.csdn.net/t/20020601/23/771920.html 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 应当使用Validating来组证数据,这个比其它的方法有更多的好处。比如可以设置某些按钮不触发这个事件,比如取消或关闭按钮或帮助按钮等没有必要引发。再就是可以使用e.Cancel=true来取消事件,使得焦点不会离开控件造成错误。 书上也说当焦点发生变化时,Validating接受焦点 焦点是什么?? 是引发事件吗 有2个TextBox,一个是输入名字的TextBoxName,一个是输入地址的TextBoxAddress,为什么是ButtonOK这个控件的CausesValadation设置为TRUE呢 Validating事件就是当光标不在这个TextBox上了,例如鼠标从这个TextBox移去点另一个控件时,或按Tab键光标移到另一个TextBox时,就会触发Validating事件。TextChange事件是光标还在这个TextBox上, 正输入内容,文字改变时 二个人,男的。住在东边的喜欢放下放下,比如见着美女就“不要执着,放下放下”住在西边的呢,喜欢就追~东边的‘放下放下’的念着咒语,心里呢实在放不下,所以就性压抑了~晚上还经常看A片打手枪~西边的刚睡完一个漂亮女孩儿,第二天早上就又感到不满足了,开始寻找更完美的next one. 试了一下发现蛮有意思的。。Validating的时候,如果数据不合法可以设e.Cancle=true,让焦点保持在当前控件上其实基本和Leave事件差不多 有2个TextBox,一个是输入名字的TextBoxName,一个是输入地址的TextBoxAddress,为什么是ButtonOK这个控件的CausesValadation设置为TRUE呢----------------------------------------------------如果不设置为true,就不会执行验证了,你试试看就知道了,当为false时,即使数据不符合要求,也一样提交页面了 如果不设置为true,就不会执行验证了,你试试看就知道了,当为false时,即使数据不符合要求,也一样提交页面了----------------------------------------------------首先谢谢大哥回复但是上面说ButtonOK能不能提交是ButtonOK.Enable啊 如果是false就不能提交,如果是true的话就可以提交的 和Valadating有什么关系呢?? 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是干什么的.. 迷茫啊~` 求一个C#定时FTP多线程下载文件的源码多谢了 求助:Linq to SQL设计器创建的类的属性缺少Add()方法 进来有分 datagridview中左边有个黑箭头的那行为什么用什么属性可以得到 请问怎么用水晶报表制作大表格?? 如何下载网页上面的图片文件到本地? 大家知不知道有没有C#的视频教材啊??? 一个简单问题! C# 打开和关闭一个文件 c#字符串操作 内存流的释放问题 毕业以后有必要去培训机构学习.net吗
TextChange事件是光标还在这个TextBox上, 正输入内容,文字改变时
住在西边的呢,喜欢就追~东边的‘放下放下’的念着咒语,心里呢实在放不下,所以就性压抑了~晚上还经常看A片打手枪~
西边的刚睡完一个漂亮女孩儿,第二天早上就又感到不满足了,开始寻找更完美的next one.
----------------------------------------------------
如果不设置为true,就不会执行验证了,你试试看就知道了,当为false时,即使数据不符合要求,也一样提交页面了
----------------------------------------------------首先谢谢大哥回复但是上面说ButtonOK能不能提交是ButtonOK.Enable啊 如果是false就不能提交,如果是true的话就可以提交的 和Valadating有什么关系呢??
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是干什么的.. 迷茫啊~`