请教Button的属性 CausesValidation ? CausesValidation MS:指示次组件是否触发验证事件。CS有验证控件么? 请教 请教.. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 CS什么意思?Winform?由于winform程序无需与服务器往返,验证什么可以直接验证了,无需验证控件了 CausesValidation 属性规定当按钮控件被点击时是否验证页面。当按钮被点击时,页面验证默认为执行。 晕 , 这是Winform程序呀, 在B/S中,asp.net中存在验证控件,表示的意思:单击按钮时,是否触发验证。但是在默认情况下都是触发的,一般不需要特别设置。只是如果存在多个按钮的情况下,才会有用。 C/S架构当没有自带的验证控件,验证逻辑都要自己写这个属性的作用只是用作是一个开启是否验证某种逻辑的开关。没有通过就则焦点停留在控件。给你一个相应的源代码,你可以看一下:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace Test{ public partial class Form1 : Form { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.myLabel = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.myTextBox1 = new System.Windows.Forms.TextBox(); this.myTextBox2 = new System.Windows.Forms.TextBox(); this.myButtonAdd = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // myLabel // this.myLabel.AutoSize = true; this.myLabel.Location = new System.Drawing.Point(70, 107); this.myLabel.Name = "myLabel"; this.myLabel.Size = new System.Drawing.Size(71, 12); this.myLabel.TabIndex = 0; this.myLabel.Text = "Description"; // // button1 // this.button1.Location = new System.Drawing.Point(71, 136); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 1; this.button1.Text = "Enabled"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // myTextBox1 // this.myTextBox1.Location = new System.Drawing.Point(72, 24); this.myTextBox1.Name = "myTextBox1"; this.myTextBox1.Size = new System.Drawing.Size(220, 21); this.myTextBox1.TabIndex = 2; // // myTextBox2 // this.myTextBox2.Location = new System.Drawing.Point(72, 69); this.myTextBox2.Name = "myTextBox2"; this.myTextBox2.Size = new System.Drawing.Size(220, 21); this.myTextBox2.TabIndex = 2; // // myButtonAdd // this.myButtonAdd.Location = new System.Drawing.Point(152, 136); this.myButtonAdd.Name = "myButtonAdd"; this.myButtonAdd.Size = new System.Drawing.Size(90, 23); this.myButtonAdd.TabIndex = 1; this.myButtonAdd.Text = "AddTwoValue"; this.myButtonAdd.UseVisualStyleBackColor = true; this.myButtonAdd.Click += new System.EventHandler(this.myButtonAdd_Click); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(25, 28); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(41, 12); this.label1.TabIndex = 0; this.label1.Text = "Value1"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(25, 73); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(41, 12); this.label2.TabIndex = 0; this.label2.Text = "Value2"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(329, 199); this.Controls.Add(this.myTextBox2); this.Controls.Add(this.myTextBox1); this.Controls.Add(this.myButtonAdd); this.Controls.Add(this.button1); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.myLabel); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Label myLabel; private System.Windows.Forms.Button button1; private System.Windows.Forms.TextBox myTextBox1; private System.Windows.Forms.TextBox myTextBox2; private Label label1; private Label label2; private System.Windows.Forms.Button myButtonAdd; public Form1() { InitializeComponent(); AddHandlers(); } private void AddHandlers() { // Add the Validating and Validated handlers for textboxes. myTextBox1.Validating += new System.ComponentModel.CancelEventHandler(myTextBox1_Validating); myTextBox1.Validated += new System.EventHandler(myTextBox1_Validated); myTextBox1.CausesValidationChanged += new System.EventHandler(myTextBox1_CausesValidationChanged); myTextBox2.Validating += new System.ComponentModel.CancelEventHandler(myTextBox2_Validating); myTextBox2.Validated += new System.EventHandler(myTextBox2_Validated); myTextBox2.CausesValidationChanged += new System.EventHandler(myTextBox2_CausesValidationChanged); if (myTextBox1.CausesValidation == true && myTextBox2.CausesValidation == true) { button1.Text = "Disable Validation"; myLabel.Text = "Validation Enabled"; this.Focus(); } } private void myTextBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e) { if (!CheckIfTextBoxNumeric(myTextBox1)) { myLabel.Text = "Has to be numeric"; e.Cancel = true; } } private void myTextBox1_Validated(object sender, System.EventArgs e) { myLabel.Text = "Validated first control"; } private void myTextBox1_CausesValidationChanged(object sender, System.EventArgs e) { myLabel.Text = "CausesValidation property was changed for First Textbox"; } private void myTextBox2_Validating(object sender, System.ComponentModel.CancelEventArgs e) { if (!CheckIfTextBoxNumeric(myTextBox2)) { myLabel.Text = "Has to be numeric"; e.Cancel = true; } } private void myTextBox2_Validated(object sender, System.EventArgs e) { myLabel.Text = "Validated second control"; } private void myTextBox2_CausesValidationChanged(object sender, System.EventArgs e) { myLabel.Text = "CausesValidation property was changed for Second Textbox"; } private bool CheckIfTextBoxNumeric(TextBox myTextBox1) { bool isValid = true; if (myTextBox1.Text == "") { isValid = false; } else { for (int i = 0; i < myTextBox1.Text.Length; i++) { if (!(System.Char.IsNumber(myTextBox1.Text[i]))) { myTextBox1.Text = ""; isValid = false; break; } } } return isValid; } private void myButtonAdd_Click(object sender, System.EventArgs e) { try { int result = Convert.ToInt32(myTextBox1.Text) + Convert.ToInt32(myTextBox2.Text); myLabel.Text = result.ToString(); } catch (Exception myException) { myLabel.Text = "Exception : " + myException.Message; } } private void button1_Click(object sender, System.EventArgs e) { if (myTextBox1.CausesValidation == false && myTextBox2.CausesValidation == false) { myTextBox1.CausesValidation = true; myTextBox2.CausesValidation = true; button1.Text = "Disable Validation"; myLabel.Text = "Validation Enabled"; } else if (myTextBox1.CausesValidation == true && myTextBox2.CausesValidation == true) { myTextBox1.CausesValidation = false; myTextBox2.CausesValidation = false; button1.Text = "Enable Validation"; myLabel.Text = "Validation Disabled"; } } }} 怎么理解yield return。。。 关于正则表达式,奇怪啊! 关闭不了窗体的疑问 急需解决:100分求助网页抓取问题-----~~~~ 跪求一个C# 的EXE加壳工具,今天已经是第三天了 还没有找到。。。 LOG4NET的优缺点是什么啊 怎么合并父子窗体的工具条 ? 动态分析.DLL的类库层次结构 求助 在方法中需要获取一个实体异步返回的值怎么获取? C#从mysql取数据并存入动态数组中 api函数声明是在哪找的? C# 中ppt 编程
Winform?由于winform程序无需与服务器往返,验证什么可以直接验证了,无需验证控件了
晕 , 这是Winform程序呀,
这个属性的作用只是用作是一个开启是否验证某种逻辑的开关。没有通过就则焦点停留在控件。
给你一个相应的源代码,你可以看一下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace Test
{
public partial class Form1 : Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows Form Designer generated code /// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.myLabel = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.myTextBox1 = new System.Windows.Forms.TextBox();
this.myTextBox2 = new System.Windows.Forms.TextBox();
this.myButtonAdd = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// myLabel
//
this.myLabel.AutoSize = true;
this.myLabel.Location = new System.Drawing.Point(70, 107);
this.myLabel.Name = "myLabel";
this.myLabel.Size = new System.Drawing.Size(71, 12);
this.myLabel.TabIndex = 0;
this.myLabel.Text = "Description";
//
// button1
//
this.button1.Location = new System.Drawing.Point(71, 136);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "Enabled";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// myTextBox1
//
this.myTextBox1.Location = new System.Drawing.Point(72, 24);
this.myTextBox1.Name = "myTextBox1";
this.myTextBox1.Size = new System.Drawing.Size(220, 21);
this.myTextBox1.TabIndex = 2;
//
// myTextBox2
//
this.myTextBox2.Location = new System.Drawing.Point(72, 69);
this.myTextBox2.Name = "myTextBox2";
this.myTextBox2.Size = new System.Drawing.Size(220, 21);
this.myTextBox2.TabIndex = 2;
//
// myButtonAdd
//
this.myButtonAdd.Location = new System.Drawing.Point(152, 136);
this.myButtonAdd.Name = "myButtonAdd";
this.myButtonAdd.Size = new System.Drawing.Size(90, 23);
this.myButtonAdd.TabIndex = 1;
this.myButtonAdd.Text = "AddTwoValue";
this.myButtonAdd.UseVisualStyleBackColor = true;
this.myButtonAdd.Click += new System.EventHandler(this.myButtonAdd_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(25, 28);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 12);
this.label1.TabIndex = 0;
this.label1.Text = "Value1";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(25, 73);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(41, 12);
this.label2.TabIndex = 0;
this.label2.Text = "Value2";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(329, 199);
this.Controls.Add(this.myTextBox2);
this.Controls.Add(this.myTextBox1);
this.Controls.Add(this.myButtonAdd);
this.Controls.Add(this.button1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.myLabel);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.Label myLabel;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox myTextBox1;
private System.Windows.Forms.TextBox myTextBox2;
private Label label1;
private Label label2;
private System.Windows.Forms.Button myButtonAdd; public Form1()
{
InitializeComponent();
AddHandlers();
}
private void AddHandlers()
{
// Add the Validating and Validated handlers for textboxes.
myTextBox1.Validating += new System.ComponentModel.CancelEventHandler(myTextBox1_Validating);
myTextBox1.Validated += new System.EventHandler(myTextBox1_Validated);
myTextBox1.CausesValidationChanged += new System.EventHandler(myTextBox1_CausesValidationChanged); myTextBox2.Validating += new System.ComponentModel.CancelEventHandler(myTextBox2_Validating);
myTextBox2.Validated += new System.EventHandler(myTextBox2_Validated);
myTextBox2.CausesValidationChanged += new System.EventHandler(myTextBox2_CausesValidationChanged);
if (myTextBox1.CausesValidation == true && myTextBox2.CausesValidation == true)
{
button1.Text = "Disable Validation";
myLabel.Text = "Validation Enabled";
this.Focus();
}
} private void myTextBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
if (!CheckIfTextBoxNumeric(myTextBox1))
{
myLabel.Text = "Has to be numeric";
e.Cancel = true;
}
}
private void myTextBox1_Validated(object sender, System.EventArgs e)
{
myLabel.Text = "Validated first control";
} private void myTextBox1_CausesValidationChanged(object sender, System.EventArgs e)
{
myLabel.Text = "CausesValidation property was changed for First Textbox";
}
private void myTextBox2_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
if (!CheckIfTextBoxNumeric(myTextBox2))
{
myLabel.Text = "Has to be numeric";
e.Cancel = true;
}
} private void myTextBox2_Validated(object sender, System.EventArgs e)
{
myLabel.Text = "Validated second control";
} private void myTextBox2_CausesValidationChanged(object sender, System.EventArgs e)
{
myLabel.Text = "CausesValidation property was changed for Second Textbox";
} private bool CheckIfTextBoxNumeric(TextBox myTextBox1)
{
bool isValid = true;
if (myTextBox1.Text == "")
{
isValid = false;
}
else
{
for (int i = 0; i < myTextBox1.Text.Length; i++)
{
if (!(System.Char.IsNumber(myTextBox1.Text[i])))
{
myTextBox1.Text = "";
isValid = false;
break;
}
}
}
return isValid;
}
private void myButtonAdd_Click(object sender, System.EventArgs e)
{
try
{
int result = Convert.ToInt32(myTextBox1.Text) + Convert.ToInt32(myTextBox2.Text);
myLabel.Text = result.ToString();
}
catch (Exception myException)
{
myLabel.Text = "Exception : " + myException.Message;
}
}
private void button1_Click(object sender, System.EventArgs e)
{
if (myTextBox1.CausesValidation == false && myTextBox2.CausesValidation == false)
{
myTextBox1.CausesValidation = true;
myTextBox2.CausesValidation = true;
button1.Text = "Disable Validation";
myLabel.Text = "Validation Enabled";
}
else if (myTextBox1.CausesValidation == true && myTextBox2.CausesValidation == true)
{
myTextBox1.CausesValidation = false;
myTextBox2.CausesValidation = false;
button1.Text = "Enable Validation";
myLabel.Text = "Validation Disabled";
}
}
}
}