using System;
using System.Windows.Forms;
using System.Drawing;
namespace GrapeCity.Win.MultiRow.SampleCode
{
class CellValidatorDemo:Form
{
private GcMultiRow gcMultiRow1 = new GcMultiRow();
private FlowLayoutPanel panel = new FlowLayoutPanel();
public CellValidatorDemo()
{
this.Text = "CellValidator Demo";
this.Size = new Size(660, 300);
// Initial flow layout panel and add to form.
this.panel.Dock = DockStyle.Left;
this.panel.Size = new Size(200, 200);
this.panel.FlowDirection = FlowDirection.TopDown;
this.panel.WrapContents = false;
this.panel.Padding = new Padding(5);
this.panel.AutoSize = true;
this.Controls.Add(panel);
// Add MultiRow to form
this.gcMultiRow1.Dock = DockStyle.Left;
this.gcMultiRow1.Width = 400;
this.Controls.Add(this.gcMultiRow1);
label.Height = 50;
label.Dock = DockStyle.Bottom;
label.BackColor = SystemColors.Info;
label.Text = "Please Click one button to add one CellValidator and set one CellValidateAction.";
this.Controls.Add(label);
this.Load += new EventHandler(Form1_Load);
InitButton();
this.StartPosition = FormStartPosition.CenterScreen;
}
private void Form1_Load(object sender, EventArgs e)
{
gcMultiRow1.Template = Template.CreateGridTemplate(1);
gcMultiRow1.RowCount = 5;
}
#region Button Event Handlers
void setRangeValidator_Click(object sender, EventArgs e)
{
Template template = Template.CreateGridTemplate(1);
//Create one RangeValidator to vaidate whether the committed value is between one range.
RangeValidator rangValidator = new RangeValidator();
//You should set the RequiredType first, then set other Properties.
rangValidator.RequiredType = typeof(int);
rangValidator.MinValue = 5;
rangValidator.MaxValue = 10;
rangValidator.NullIsValid = false;
LineNotify lineNotifyAction = new LineNotify();
//When you ends edit mode, the action will be done.(press Enter key commonly)
lineNotifyAction.DoActionReason = ValidateReasons.EndEdit;
lineNotifyAction.LineColor = Color.Red;
rangValidator.Actions.Add(lineNotifyAction);
template.Row.Cells[0].Validators.Add(rangValidator);
this.gcMultiRow1.Template = template;
label.Text = "The RangeValidator's MinValue is 5, MaxValue is 10, input 4(not between the range) in one Cell, press Enter key to trigger validation(DoActionReason is EndEdit), one red under line will be shown.";
}
void setRequiredTypeValidator_Click(object sender, EventArgs e)
{
Template template = Template.CreateGridTemplate(1);
RequiredTypeValidator requiredTypeValidator = new RequiredTypeValidator();
requiredTypeValidator.RequiredType = typeof(decimal);
TipNotify tipNotify = new TipNotify();
tipNotify.ToolTipIcon = ToolTipIcon.Warning;
tipNotify.ToolTipSpan = 6000;
tipNotify.ToolTipTitle = "Requird Type Error";
tipNotify.ToolTipText = "The original value is not Decimal type";
//When you escape edit mode, the acion will be done(press ESC key commonly)
tipNotify.DoActionReason = ValidateReasons.CancelEdit;
requiredTypeValidator.Actions.Add(tipNotify);
//When cancel edit, this value will be validated.
template.Row.Cells[0].Value = "I'm string type";
template.Row.Cells[0].Validators.Add(requiredTypeValidator);
this.gcMultiRow1.Template = template;
label.Text = "The RequiredTypeValidator's RequiredType is decimal type,by default, the cell's value is 'I'm string type', if you enter edit mode and input any value, then press ESC to escape edit mode to trigger validation(DoActionReason is CancelEdit), the old default value will be validated, it is not a Decimal type value, so validation failed.";
}
void setIncludeListValidator_Click(object sender, EventArgs e)
{
Template template = Template.CreateGridTemplate(1);
IncludeListValidator includeListValidator = new IncludeListValidator();
includeListValidator.Candidates = new string[] { null, "2", "3", "4", "5" };
IconNotify iconNotify = new IconNotify();
iconNotify.IconAlignment = ContentAlignment.MiddleCenter;
iconNotify.IconTip = "The editing value is not within the specified candidate list!";
//When CurrentCell move to another Cell, the action will be done.
iconNotify.DoActionReason = ValidateReasons.CellValidating;
includeListValidator.Actions.Add(iconNotify);
template.Row.Cells[0].Validators.Add(includeListValidator);
this.gcMultiRow1.Template = template;
label.Text = "The IncludeListValidator.Candidates list is 'null,2,3,4,5', please input 1 in edit mode, then move CurrentCell to another Cell to trigger validation(DoActionReason is CellValidating), one error icon will pop up to notify you validation failed.";
}
void setRegularExpressionValidator_Click(object sender, EventArgs e)
{
Template template = Template.CreateGridTemplate(1);
RegularExpressionValidator regularExpressionValidator = new RegularExpressionValidator();
//Define a regular expression for currency values.
regularExpressionValidator.Expression = @"^-?\d+(\.\d{2})?$";
regularExpressionValidator.RegexOptions = System.Text.RegularExpressions.RegexOptions.CultureInvariant;
FocusProcess focusProcess = new FocusProcess();
focusProcess.PreventFocusLeave = true;
//When CurrentCell move to another Row, the action will be done.
focusProcess.DoActionReason = ValidateReasons.RowValidating;
regularExpressionValidator.Actions.Add(focusProcess);
template.Row.Cells[0].Validators.Add(regularExpressionValidator);
this.gcMultiRow1.Template = template;
label .Text = @"The RegularExpressionValidator.Expression is '^-?\d+(\.\d{2})?$', which expresses one currency value, input '-!2.05', then move CurrentCell to another Row to trigger validation(DoActionReasion is RowValidating), You will find the focus cannot move out, input '-12.05',the focus can move to another Row.";
}
void setPairCharValidator_Click(object sender, EventArgs e)
{
Template template = Template.CreateGridTemplate(1);
PairCharValidator pairCharValidator = new PairCharValidator();
pairCharValidator.PairChars.Add(new PairChar('(', ')'));
CellStyleNotify cellStyleNotify = new CellStyleNotify();
cellStyleNotify.InvalidCellStyle.ForeColor = Color.Red;
cellStyleNotify.InvalidCellStyle.Font = new Font(DefaultFont, FontStyle.Bold);
cellStyleNotify.DoActionReason = ValidateReasons.EditedFormattedValueChanged;
pairCharValidator.Actions.Add(cellStyleNotify);
template.Row.Cells[0].Validators.Add(pairCharValidator);
this.gcMultiRow1.Template = template;
label.Text = "The PairCharValidator.PairChars is '( )', in edit mode, if you input '2*(3+4' to trigger validation, validation failed, the font will change, if input '2*(3+4)' to trigger validation, the validation success.";
}
void setCompareValueValidator_Click(object sender, EventArgs e)
{
Template template = Template.CreateGridTemplate(1);
CompareValueValidator compareValueValidator = new CompareValueValidator();
//You should set the RequiredType first, then set other Properties.
compareValueValidator.RequiredType = typeof(int);
compareValueValidator.ComparedValue = 10;
compareValueValidator.DifferenceValue = 0;
compareValueValidator.ComparedOperator = ValidateComparisonOperator.GreaterThan;
SoundNotify soundNotify = new SoundNotify();
soundNotify.SoundType = SystemSoundType.Asterisk;
//When cancel the whole Row editing, the old value will be restored.
soundNotify.DoActionReason = ValidateReasons.CancelRow;
compareValueValidator.Actions.Add(soundNotify);
//When do the SoundNotify action, this value 9 will be validated.
template.Row.Cells[0].Value = 9;
template.Row.Cells[0].Validators.Add(compareValueValidator);
this.gcMultiRow1.Template = template;
label.Text = "The CompareValueValidator.ComparedValue is 10, DifferenceValue is 0, ComparedOperator is GreaterThan, input some value to Cell, press ENTER to commit value(not validate), then press ESC to trigger validation(DoActionReason is CancelRow), the old value 9 is restored and validated, validation failed, one Sound will be played";
}
void setMyRequiredFieldValidator_Click(object sender,EventArgs e)
{
Template template = Template.CreateGridTemplate(1);
MyRequiredFieldValidator myValidator = new MyRequiredFieldValidator();
MyIconNotifyAction myAction = new MyIconNotifyAction();
myAction.DoActionReason = ValidateReasons.EndEdit;
myValidator.Actions.Add(myAction);
template.Row.Cells[0].Validators.Add(myValidator);
this.gcMultiRow1.Template = template;
label.Text = "Use the customized MyRequiredFieldValidator and MyIconNotifyAction, input 'aaa', press ENTER key to commit value, one error icon will show and one system sound will also play, hover the error icon, the customized text will show.";
}
class MyRequiredFieldValidator : RequiredFieldValidator
{
protected override bool Validate(ValidateContext context)
{
if (context.EditedFormattedValue != null)
{
string value = context.EditedFormattedValue.ToString();
if (value != "NULL")
{
context.ValidateInfo = "The input value is not NULL";
return false;
}
else
{
return true;
}
}
return base.Validate(context);
}
}
class MyIconNotifyAction : IconNotify
{
protected override void DoAction(ValidateActionContext context)
{
base.DoAction(context);
//When the error icon pop up, one system sound will play at the same time.
if (!context.IsValid)
{
System.Media.SystemSounds.Asterisk.Play();
}
}
}
#endregion
#region Initialize Buttons
private void InitButton()
{
AddButton(setRangeValidator, "Click to add one RangeValidator", new EventHandler(setRangeValidator_Click));
AddButton(setRequiredTypeValidator, "Click to add one RequiredTypeValidator", new EventHandler(setRequiredTypeValidator_Click));
AddButton(setIncludeListValidator, "Click to add one IncludeListValidator", new EventHandler(setIncludeListValidator_Click));
AddButton(setRegularExpressionValidator, "Click to add one RegularExpressionValidator", new EventHandler(setRegularExpressionValidator_Click));
AddButton(setPairCharValidator, "Click to add one PairCharValidator", new EventHandler(setPairCharValidator_Click));
AddButton(setCompareValueValidator, "Click to add one CompareValueValidator", new EventHandler(setCompareValueValidator_Click));
AddButton(setMyRequiredFieldValidator, "Click to add one customized CellValidator", new EventHandler(setMyRequiredFieldValidator_Click));
}
private void AddButton(Button button, string text, EventHandler eventHandler)
{
this.panel.Controls.Add(button);
button.Text = text;
button.AutoSize = true;
button.Click += eventHandler;
}
Button setRangeValidator = new Button();
Button setIncludeListValidator = new Button();
Button setRequiredTypeValidator = new Button();
Button setPairCharValidator = new Button();
Button setRegularExpressionValidator = new Button();
Button setCompareValueValidator = new Button();
Button setMyRequiredFieldValidator = new Button();
Label label = new Label();
#endregion
[STAThreadAttribute()]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new CellValidatorDemo());
}
}
}
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports GrapeCity.Win.MultiRow
Class CellValidatorDemo
Inherits Form
Private gcMultiRow1 As New GcMultiRow()
Private panel As New FlowLayoutPanel()
Public Sub New()
Me.Text = "CellValidator Demo"
Me.Size = New Size(660, 300)
' Initial flow layout panel and add to form.
Me.panel.Dock = DockStyle.Left
Me.panel.Size = New Size(200, 200)
Me.panel.FlowDirection = FlowDirection.TopDown
Me.panel.WrapContents = False
Me.panel.Padding = New Padding(5)
Me.panel.AutoSize = True
Me.Controls.Add(panel)
' Add MultiRow to form
Me.gcMultiRow1.Dock = DockStyle.Left
Me.gcMultiRow1.Width = 400
Me.Controls.Add(Me.gcMultiRow1)
label.Height = 50
label.Dock = DockStyle.Bottom
label.BackColor = SystemColors.Info
label.Text = "Please Click one button to add one CellValidator and set one CellValidateAction."
Me.Controls.Add(label)
InitButton()
Me.StartPosition = FormStartPosition.CenterScreen
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
gcMultiRow1.Template = Template.CreateGridTemplate(1)
gcMultiRow1.RowCount = 5
End Sub
#Region "Button Event Handlers"
Private Sub setRangeValidator_Click(ByVal sender As Object, ByVal e As EventArgs) Handles setRangeValidator.Click
Dim template As Template = template.CreateGridTemplate(1)
'Create one RangeValidator to vaidate whether the committed value is between one range.
Dim rangValidator As New RangeValidator()
'You should set the RequiredType first, then set other Properties.
rangValidator.RequiredType = GetType(Integer)
rangValidator.MinValue = 5
rangValidator.MaxValue = 10
rangValidator.NullIsValid = False
Dim lineNotifyAction As New LineNotify()
'When you ends edit mode, the action will be done.(press Enter key commonly)
lineNotifyAction.DoActionReason = ValidateReasons.EndEdit
lineNotifyAction.LineColor = Color.Red
rangValidator.Actions.Add(lineNotifyAction)
template.Row.Cells(0).Validators.Add(rangValidator)
Me.gcMultiRow1.Template = template
label.Text = "The RangeValidator's MinValue is 5, MaxValue is 10, input 4(not between the range) in one Cell, press Enter key to trigger validation(DoActionReason is EndEdit), one red under line will be shown."
End Sub
Private Sub setRequiredTypeValidator_Click(ByVal sender As Object, ByVal e As EventArgs) Handles setRequiredTypeValidator.Click
Dim template As Template = template.CreateGridTemplate(1)
Dim requiredTypeValidator As New RequiredTypeValidator()
requiredTypeValidator.RequiredType = GetType(Decimal)
Dim tipNotify As New TipNotify()
tipNotify.ToolTipIcon = ToolTipIcon.Warning
tipNotify.ToolTipSpan = 6000
tipNotify.ToolTipTitle = "Requird Type Error"
tipNotify.ToolTipText = "The original value is not Decimal type"
'When you escape edit mode, the acion will be done(press ESC key commonly)
tipNotify.DoActionReason = ValidateReasons.CancelEdit
requiredTypeValidator.Actions.Add(tipNotify)
'When cancel edit, this value will be validated.
template.Row.Cells(0).Value = "I'm string type"
template.Row.Cells(0).Validators.Add(requiredTypeValidator)
Me.gcMultiRow1.Template = template
label.Text = "The RequiredTypeValidator's RequiredType is decimal type,by default, the cell's value is 'I'm string type', if you enter edit mode and input any value, then press ESC to escape edit mode to trigger validation(DoActionReason is CancelEdit), the old default value will be validated, it is not a Decimal type value, so validation failed."
End Sub
Private Sub setIncludeListValidator_Click(ByVal sender As Object, ByVal e As EventArgs) Handles setIncludeListValidator.Click
Dim template As Template = template.CreateGridTemplate(1)
Dim includeListValidator As New IncludeListValidator()
includeListValidator.Candidates = New String() {Nothing, "2", "3", "4", "5"}
Dim iconNotify As New IconNotify()
iconNotify.IconAlignment = ContentAlignment.MiddleCenter
iconNotify.IconTip = "The editing value is not within the specified candidate list!"
'When CurrentCell move to another Cell, the action will be done.
iconNotify.DoActionReason = ValidateReasons.CellValidating
includeListValidator.Actions.Add(iconNotify)
template.Row.Cells(0).Validators.Add(includeListValidator)
Me.gcMultiRow1.Template = template
label.Text = "The IncludeListValidator.Candidates list is 'null,2,3,4,5', please input 1 in edit mode, then move CurrentCell to another Cell to trigger validation(DoActionReason is CellValidating), one error icon will pop up to notify you validation failed."
End Sub
Private Sub setRegularExpressionValidator_Click(ByVal sender As Object, ByVal e As EventArgs) Handles setRegularExpressionValidator.Click
Dim template As Template = template.CreateGridTemplate(1)
Dim regularExpressionValidator As New RegularExpressionValidator()
'Define a regular expression for currency values.
regularExpressionValidator.Expression = "^-?\d+(\.\d{2})?$"
regularExpressionValidator.RegexOptions = System.Text.RegularExpressions.RegexOptions.CultureInvariant
Dim focusProcess As New FocusProcess()
focusProcess.PreventFocusLeave = True
'When CurrentCell move to another Row, the action will be done.
focusProcess.DoActionReason = ValidateReasons.RowValidating
regularExpressionValidator.Actions.Add(focusProcess)
template.Row.Cells(0).Validators.Add(regularExpressionValidator)
Me.gcMultiRow1.Template = template
label.Text = "The RegularExpressionValidator.Expression is '^-?\d+(\.\d{2})?$', which expresses one currency value, input '-!2.05', then move CurrentCell to another Row to trigger validation(DoActionReasion is RowValidating), You will find the focus cannot move out, input '-12.05',the focus can move to another Row."
End Sub
Private Sub setPairCharValidator_Click(ByVal sender As Object, ByVal e As EventArgs) Handles setPairCharValidator.Click
Dim template As Template = template.CreateGridTemplate(1)
Dim pairCharValidator As New PairCharValidator()
pairCharValidator.PairChars.Add(New PairChar("("c, ")"c))
Dim cellStyleNotify As New CellStyleNotify()
cellStyleNotify.InvalidCellStyle.ForeColor = Color.Red
cellStyleNotify.InvalidCellStyle.Font = New Font(DefaultFont, FontStyle.Bold)
cellStyleNotify.DoActionReason = ValidateReasons.EditedFormattedValueChanged
pairCharValidator.Actions.Add(cellStyleNotify)
template.Row.Cells(0).Validators.Add(pairCharValidator)
Me.gcMultiRow1.Template = template
label.Text = "The PairCharValidator.PairChars is '( )', in edit mode, if you input '2*(3+4' to trigger validation, validation failed, the font will change, if input '2*(3+4)' to trigger validation, the validation success."
End Sub
Private Sub setCompareValueValidator_Click(ByVal sender As Object, ByVal e As EventArgs) Handles setCompareValueValidator.Click
Dim template As Template = template.CreateGridTemplate(1)
Dim compareValueValidator As New CompareValueValidator()
'You should set the RequiredType first, then set other Properties.
compareValueValidator.RequiredType = GetType(Integer)
compareValueValidator.ComparedValue = 10
compareValueValidator.DifferenceValue = 0
compareValueValidator.ComparedOperator = ValidateComparisonOperator.GreaterThan
Dim soundNotify As New SoundNotify()
soundNotify.SoundType = SystemSoundType.Asterisk
'When cancel the whole Row editing, the old value will be restored.
soundNotify.DoActionReason = ValidateReasons.CancelRow
compareValueValidator.Actions.Add(soundNotify)
'When do the SoundNotify action, this value 9 will be validated.
template.Row.Cells(0).Value = 9
template.Row.Cells(0).Validators.Add(compareValueValidator)
Me.gcMultiRow1.Template = template
label.Text = "The CompareValueValidator.ComparedValue is 10, DifferenceValue is 0, ComparedOperator is GreaterThan, input some value to Cell, press ENTER to commit value(not validate), then press ESC to trigger validation(DoActionReason is CancelRow), the old value 9 is restored and validated, validation failed, one Sound will be played"
End Sub
Private Sub setMyRequiredFieldValidator_Click(ByVal sender As Object, ByVal e As EventArgs) Handles setMyRequiredFieldValidator.Click
Dim template As Template = template.CreateGridTemplate(1)
Dim myValidator As New MyRequiredFieldValidator()
Dim myAction As New MyIconNotifyAction()
myAction.DoActionReason = ValidateReasons.EndEdit
myValidator.Actions.Add(myAction)
template.Row.Cells(0).Validators.Add(myValidator)
Me.gcMultiRow1.Template = template
label.Text = "Use the customized MyRequiredFieldValidator and MyIconNotifyAction, input 'aaa', press ENTER key to commit value, one error icon will show and one system sound will also play, hover the error icon, the customized text will show."
End Sub
Private Class MyRequiredFieldValidator
Inherits RequiredFieldValidator
Protected Overloads Overrides Function Validate(ByVal context As ValidateContext) As Boolean
If context.EditedFormattedValue <> Nothing Then
Dim value As String = context.EditedFormattedValue.ToString()
If value <> "NULL" Then
context.ValidateInfo = "The input value is not NULL"
Return False
Else
Return True
End If
End If
Return MyBase.Validate(context)
End Function
End Class
Private Class MyIconNotifyAction
Inherits IconNotify
Protected Overloads Overrides Sub DoAction(ByVal context As ValidateActionContext)
MyBase.DoAction(context)
'When the error icon pop up, one system sound will play at the same time.
If Not context.IsValid Then
System.Media.SystemSounds.Asterisk.Play()
End If
End Sub
End Class
#End Region
#Region "Initialize Buttons"
Private Sub InitButton()
AddButton(setRangeValidator, "Click to add one RangeValidator")
AddButton(setRequiredTypeValidator, "Click to add one RequiredTypeValidator")
AddButton(setIncludeListValidator, "Click to add one IncludeListValidator")
AddButton(setRegularExpressionValidator, "Click to add one RegularExpressionValidator")
AddButton(setPairCharValidator, "Click to add one PairCharValidator")
AddButton(setCompareValueValidator, "Click to add one CompareValueValidator")
AddButton(setMyRequiredFieldValidator, "Click to add one customized CellValidator")
End Sub
Private Sub AddButton(ByVal button As Button, ByVal text As String)
Me.panel.Controls.Add(button)
button.Text = text
button.AutoSize = True
End Sub
Friend WithEvents setRangeValidator As New Button()
Friend WithEvents setIncludeListValidator As New Button()
Friend WithEvents setRequiredTypeValidator As New Button()
Friend WithEvents setPairCharValidator As New Button()
Friend WithEvents setRegularExpressionValidator As New Button()
Friend WithEvents setCompareValueValidator As New Button()
Friend WithEvents setMyRequiredFieldValidator As New Button()
Private label As New Label()
#End Region
<STAThreadAttribute()> _
Public Shared Sub Main()
Application.EnableVisualStyles()
Application.Run(New CellValidatorDemo())
End Sub
End Class