3
我需要根據我正在解析的文本文件生成窗體表單域。我也想自動生成驗證事件處理程序,這些處理程序基本上只會與表單字段相同,並且用於驗證字段的正則表達式將會更改。使用基於文本文件的事件創建C#表單域
我很難找出最快最簡單的方法去做這件事。
任何指向正確方向的指針都會很棒,甚至有些關鍵字可能會丟失。我只能想象這種類型的問題已經被覆蓋過,我只是不知道我在尋找什麼。
我需要根據我正在解析的文本文件生成窗體表單域。我也想自動生成驗證事件處理程序,這些處理程序基本上只會與表單字段相同,並且用於驗證字段的正則表達式將會更改。使用基於文本文件的事件創建C#表單域
我很難找出最快最簡單的方法去做這件事。
任何指向正確方向的指針都會很棒,甚至有些關鍵字可能會丟失。我只能想象這種類型的問題已經被覆蓋過,我只是不知道我在尋找什麼。
我一直在使用XML在運行時生成
我發現最簡單的方法是創建自定義的控制做類似的東西控制,以方便確認。
我還收集有關的控制細節能夠字段的值返回給Web服務
這是我如何實現這一目標
public bool IsFieldValid()
{
Error.SetError(label, "");
if (_Validate.Length == 0)
{
return true;
}
else
{
switch (_Validate)
{
case "General":
return ValidateText();
case "Int":
return ValidateInt();
case "Decimal":
return ValidateDecimal();
case "Length":
return ValidateTextLength();
default:
return true;
}
}
}
public bool ValidateText()
{
this.shapeContainer1.BackColor = this.BackColor;
if (_CanBeNull == true & this.textBox.Text.Trim() == "")
{
return true;
}
else
{
if (this.textBox.Text.Trim().Length > 0)
{
return true;
}
else
{
Error.SetError(label, CustomError);
return false;
}
}
}
public bool ValidateDecimal()
{
if (_CanBeNull == true & this.textBox.Text.Trim() == "")
{
return true;
}
else
{
try
{
this.textBox.Text = this.textBox.Text.Replace("£", "");
Decimal Val = System.Convert.ToDecimal(this.textBox.Text);
if (_MaxValue == 0)
{
return true;
}
if ((Val >= _MinValue) && (Val <= _MaxValue))
{
return true;
}
else
{
this.Error.SetError(this.label, "Value must be between " + _MinValue.ToString() + " and " + _MaxValue);
return false;
}
}
catch
{
this.Error.SetError(this.label, "Error converting value to a decimal value");
return false;
}
}
}
public bool ValidateInt()
{
if (_CanBeNull == true & this.textBox.Text.Trim() == "")
{
return true;
}
else
{
try
{
int Val = System.Convert.ToInt32(this.textBox.Text);
if (_MaxValue == 0)
{
return true;
}
if ((Val >= _MinValue) && (Val <= _MaxValue))
{
return true;
}
else
{
this.Error.SetError(label,"Value must be between " + _MinValue.ToString() + " and " + _MaxValue);
return false;
}
}
catch
{
this.Error.SetError(label,"Error converting value to a numeric value");
return false;
}
}
}
public bool ValidateTextLength()
{
if (_CanBeNull == true & this.textBox.Text.Trim() == "")
{
return true;
}
else
{
int Val = this.textBox.Text.Length;
if ((Val >= _MinValue) && (Val <= _MaxValue))
{
return true;
}
else
{
this.Error.SetError(label,"Length of text must be between " + _MinValue.ToString() + " and " + _MaxValue);
return false;
}
}
}
public String TxtReturnXML
{
get {
string S;
XmlDocument doc = new XmlDocument();
XmlNode Xe = doc.CreateNode(XmlNodeType.Element,"Value",null);
Xe.InnerText = this.textBox.Text.ToString();
S = Xe.OuterXml;
doc = null;
Xe = null;
return S;
}
}
public bool LoadedXml(XmlNode X)
{
try
{
_Validate = X.Attributes["Validate"].Value;
this.textBox.Text = X.Attributes["DefaultValue"].Value.ToString();
this.label.Text = X.Attributes["LabelText"].Value.ToString();
if (System.Convert.ToInt32(X.Attributes["Height"].Value) > 0)
{
this.Height = System.Convert.ToInt32(X.Attributes["Height"].Value) + 10;
textBox.Multiline = true;
this.textBox.Height = System.Convert.ToInt32(X.Attributes["Height"].Value);
}
if (System.Convert.ToInt32(X.Attributes["Width"].Value) > 0)
{
this.textBox.Width = System.Convert.ToInt32(X.Attributes["Width"].Value);
this.textBox.Left = (this.Width - 20) - System.Convert.ToInt32(X.Attributes["Width"].Value);
}
_CanBeNull = System.Convert.ToBoolean(X.Attributes["CanBeNull"].Value);
_PassBack = System.Convert.ToBoolean(X.Attributes["PassBack"].Value);
CustomError = X.Attributes["CustomError"].Value.ToString();
CustomWarning = X.Attributes["CustomWarning"].Value.ToString();
if (CustomWarning.Length > 0)
{
Error.SetError(label, CustomWarning);
}
_MinValue = System.Convert.ToInt32(X.Attributes["MinValue"].Value);
_MaxValue = System.Convert.ToInt32(X.Attributes["MaxValue"].Value);
}
catch {
return false;
}
_LoadedXml = X;
return true;
}
你也可以添加事件處理,以驗證在一個特定的事件(即模糊)
希望這有助於
SP
有趣的方法,雖然希望有點更通用。真的很欣賞代碼,絕對值得一看。 – tplaner 2010-06-07 19:04:55
不是最好的編碼器,但我總是嘗試和幫助 – Steven 2010-06-08 13:18:14