DotNet 4.8.1 build of DotNetBar
This commit is contained in:
366
PROMS/DotNetBar Source Code/Validator/CompareValidator.cs
Normal file
366
PROMS/DotNetBar Source Code/Validator/CompareValidator.cs
Normal file
@@ -0,0 +1,366 @@
|
||||
#if FRAMEWORK20
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace DevComponents.DotNetBar.Validator
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents compare validator for SuperValidator control used to compare two input fields or input fields to specified value.
|
||||
/// </summary>
|
||||
public class CompareValidator : ValidatorBase
|
||||
{
|
||||
#region Constructors
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the RequiredFieldValidator class.
|
||||
/// </summary>
|
||||
public CompareValidator()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the RequiredFieldValidator class.
|
||||
/// </summary>
|
||||
public CompareValidator(string errorMessage)
|
||||
{
|
||||
this.ErrorMessage = errorMessage;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the RequiredFieldValidator class.
|
||||
/// </summary>
|
||||
public CompareValidator(string errorMessage, string optionalValidationGroup)
|
||||
{
|
||||
this.ErrorMessage = errorMessage;
|
||||
this.OptionalValidationGroup = optionalValidationGroup;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the RequiredFieldValidator class.
|
||||
/// </summary>
|
||||
public CompareValidator(string errorMessage, string optionalValidationGroup, Control controlToCompare, eValidationCompareOperator compareOperator)
|
||||
{
|
||||
this.ErrorMessage = errorMessage;
|
||||
this.OptionalValidationGroup = optionalValidationGroup;
|
||||
this.ControlToCompare = controlToCompare;
|
||||
this.Operator = compareOperator;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the RequiredFieldValidator class.
|
||||
/// </summary>
|
||||
public CompareValidator(string errorMessage, Control controlToCompare, eValidationCompareOperator compareOperator)
|
||||
{
|
||||
this.ErrorMessage = errorMessage;
|
||||
this.ControlToCompare = controlToCompare;
|
||||
this.Operator = compareOperator;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the RequiredFieldValidator class.
|
||||
/// </summary>
|
||||
public CompareValidator(string errorMessage, string optionalValidationGroup, object valueToCompare, eValidationCompareOperator compareOperator)
|
||||
{
|
||||
this.ErrorMessage = errorMessage;
|
||||
this.OptionalValidationGroup = optionalValidationGroup;
|
||||
this.ValueToCompare = valueToCompare;
|
||||
this.Operator = compareOperator;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the RequiredFieldValidator class.
|
||||
/// </summary>
|
||||
public CompareValidator(string errorMessage, object valueToCompare, eValidationCompareOperator compareOperator)
|
||||
{
|
||||
this.ErrorMessage = errorMessage;
|
||||
this.ValueToCompare = valueToCompare;
|
||||
this.Operator = compareOperator;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the RequiredFieldValidator class.
|
||||
/// </summary>
|
||||
public CompareValidator(string errorMessage, string optionalValidationGroup, Control controlToCompare, object valueToCompare, eValidationCompareOperator compareOperator)
|
||||
{
|
||||
this.ErrorMessage = errorMessage;
|
||||
this.OptionalValidationGroup = optionalValidationGroup;
|
||||
this.ControlToCompare = controlToCompare;
|
||||
this.Operator = compareOperator;
|
||||
this.ValueToCompare = valueToCompare;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Implementation
|
||||
public override bool Validate(System.Windows.Forms.Control input)
|
||||
{
|
||||
object leftValue = GetControlValue(input);
|
||||
object rightValue = null;
|
||||
if (_ControlToCompare != null)
|
||||
rightValue = GetControlValue(_ControlToCompare);
|
||||
else
|
||||
rightValue = _ValueToCompare;
|
||||
|
||||
this.LastValidationResult = Compare(leftValue, rightValue, _Operator);
|
||||
return LastValidationResult;
|
||||
}
|
||||
|
||||
private Control _ControlToCompare = null;
|
||||
/// <summary>
|
||||
/// Gets or sets the control to compare validated control to.
|
||||
/// </summary>
|
||||
[DefaultValue(null), Category("Behavior"), Description("Indicates control to compare validated control to.")]
|
||||
public Control ControlToCompare
|
||||
{
|
||||
get { return _ControlToCompare; }
|
||||
set { _ControlToCompare = value; }
|
||||
}
|
||||
|
||||
private string _ControlToCompareValuePropertyName="";
|
||||
/// <summary>
|
||||
/// Gets or sets the Value property name for the ControlToCompare control.
|
||||
/// </summary>
|
||||
[DefaultValue(""), Category("Behavior"), Description("Indicates Value property name for the ControlToCompare control.")]
|
||||
public string ControlToCompareValuePropertyName
|
||||
{
|
||||
get { return _ControlToCompareValuePropertyName; }
|
||||
set
|
||||
{
|
||||
_ControlToCompareValuePropertyName = value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private object _ValueToCompare;
|
||||
/// <summary>
|
||||
/// Gets or sets the value to compare to the validation control.
|
||||
/// </summary>
|
||||
[DefaultValue((string)null), Category("Behavior"), Description("Indicates value to compare to the validation control."), TypeConverter(typeof(StringConverter))]
|
||||
public object ValueToCompare
|
||||
{
|
||||
get { return _ValueToCompare; }
|
||||
set { _ValueToCompare = value; }
|
||||
}
|
||||
|
||||
private eValidationCompareOperator _Operator = eValidationCompareOperator.Equal;
|
||||
/// <summary>
|
||||
/// Gets or sets the operator used for comparison.
|
||||
/// </summary>
|
||||
[DefaultValue(eValidationCompareOperator.Equal), Category("Behavior"), Description("Indicates operator used for comparison")]
|
||||
public eValidationCompareOperator Operator
|
||||
{
|
||||
get { return _Operator; }
|
||||
set { _Operator = value; }
|
||||
}
|
||||
|
||||
internal static bool Compare(object leftValue, object rightValue, eValidationCompareOperator compareOperator)
|
||||
{
|
||||
if (compareOperator == eValidationCompareOperator.DataTypeCheck)
|
||||
{
|
||||
if (leftValue != null && rightValue != null)
|
||||
return leftValue.GetType().Equals(rightValue.GetType());
|
||||
else
|
||||
return leftValue == rightValue;
|
||||
}
|
||||
if (leftValue == null && rightValue == null) return true;
|
||||
if (leftValue == null || rightValue == null) return false;
|
||||
|
||||
if (!leftValue.GetType().Equals(rightValue.GetType()) && rightValue is string)
|
||||
{
|
||||
object temp = null;
|
||||
if (ConvertRightToLeftType(leftValue, rightValue, out temp))
|
||||
rightValue = temp;
|
||||
}
|
||||
|
||||
if (!leftValue.GetType().Equals(rightValue.GetType()))
|
||||
return false;
|
||||
|
||||
int compareResult = 0;
|
||||
|
||||
if (leftValue is string)
|
||||
compareResult = ((string)leftValue).CompareTo(rightValue);
|
||||
else if (leftValue is int)
|
||||
compareResult = ((int)leftValue).CompareTo(rightValue);
|
||||
else if (leftValue is double)
|
||||
compareResult = ((double)leftValue).CompareTo(rightValue);
|
||||
else if (leftValue is long)
|
||||
compareResult = ((long)leftValue).CompareTo(rightValue);
|
||||
else if (leftValue is DateTime)
|
||||
compareResult = ((DateTime)leftValue).CompareTo(rightValue);
|
||||
else if (leftValue is decimal)
|
||||
compareResult = ((decimal)leftValue).CompareTo(rightValue);
|
||||
else if (leftValue is Single)
|
||||
compareResult = ((Single)leftValue).CompareTo(rightValue);
|
||||
else if (leftValue is bool)
|
||||
compareResult = ((bool)leftValue).CompareTo(rightValue);
|
||||
else if (leftValue is TimeSpan)
|
||||
compareResult = ((TimeSpan)leftValue).CompareTo(rightValue);
|
||||
else if (leftValue is byte)
|
||||
compareResult = ((byte)leftValue).CompareTo(rightValue);
|
||||
else if (leftValue is char)
|
||||
compareResult = ((char)leftValue).CompareTo(rightValue);
|
||||
else if (leftValue is Guid)
|
||||
compareResult = ((Guid)leftValue).CompareTo(rightValue);
|
||||
else
|
||||
return true;
|
||||
|
||||
switch (compareOperator)
|
||||
{
|
||||
case eValidationCompareOperator.Equal:
|
||||
return (compareResult == 0);
|
||||
|
||||
case eValidationCompareOperator.NotEqual:
|
||||
return (compareResult != 0);
|
||||
|
||||
case eValidationCompareOperator.GreaterThan:
|
||||
return (compareResult > 0);
|
||||
|
||||
case eValidationCompareOperator.GreaterThanEqual:
|
||||
return (compareResult >= 0);
|
||||
|
||||
case eValidationCompareOperator.LessThan:
|
||||
return (compareResult < 0);
|
||||
|
||||
case eValidationCompareOperator.LessThanEqual:
|
||||
return (compareResult <= 0);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
internal static bool ConvertRightToLeftType(object leftValue, object rightValue, out object temp)
|
||||
{
|
||||
temp = null;
|
||||
if (rightValue == null || leftValue == null || !(rightValue is string)) return false;
|
||||
if (rightValue.GetType().Equals(leftValue.GetType()))
|
||||
{
|
||||
temp = rightValue;
|
||||
return true;
|
||||
}
|
||||
|
||||
Type type = leftValue.GetType();
|
||||
string right = (string)rightValue;
|
||||
|
||||
if (type == typeof(int))
|
||||
{
|
||||
int result=0;
|
||||
if (int.TryParse(right, out result))
|
||||
{
|
||||
temp = result;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if (type == typeof(long))
|
||||
{
|
||||
long result = 0;
|
||||
if (long.TryParse(right, out result))
|
||||
{
|
||||
temp = result;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if (type == typeof(double))
|
||||
{
|
||||
double result = 0;
|
||||
if (double.TryParse(right, out result))
|
||||
{
|
||||
temp = result;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if (type == typeof(Single))
|
||||
{
|
||||
Single result = 0;
|
||||
if (Single.TryParse(right, out result))
|
||||
{
|
||||
temp = result;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if (type == typeof(DateTime))
|
||||
{
|
||||
DateTime result;
|
||||
if (DateTime.TryParse(right, out result))
|
||||
{
|
||||
temp = result;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if (type == typeof(TimeSpan))
|
||||
{
|
||||
TimeSpan result;
|
||||
if (TimeSpan.TryParse(right, out result))
|
||||
{
|
||||
temp = result;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if (type == typeof(bool))
|
||||
{
|
||||
bool result;
|
||||
if (bool.TryParse(right, out result))
|
||||
{
|
||||
temp = result;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if (type == typeof(byte))
|
||||
{
|
||||
byte result;
|
||||
if (byte.TryParse(right, out result))
|
||||
{
|
||||
temp = result;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if (type == typeof(char))
|
||||
{
|
||||
char result;
|
||||
if (char.TryParse(right, out result))
|
||||
{
|
||||
temp = result;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
/// <summary>
|
||||
/// Specifies the validation comparison operators used by the CompareValidator.
|
||||
/// </summary>
|
||||
public enum eValidationCompareOperator
|
||||
{
|
||||
/// <summary>
|
||||
/// A comparison for equality.
|
||||
/// </summary>
|
||||
Equal,
|
||||
/// <summary>
|
||||
/// A comparison for inequality.
|
||||
/// </summary>
|
||||
NotEqual,
|
||||
/// <summary>
|
||||
/// A comparison for greater than.
|
||||
/// </summary>
|
||||
GreaterThan,
|
||||
/// <summary>
|
||||
/// A comparison for greater than or equal to.
|
||||
/// </summary>
|
||||
GreaterThanEqual,
|
||||
/// <summary>
|
||||
/// A comparison for less than.
|
||||
/// </summary>
|
||||
LessThan,
|
||||
/// <summary>
|
||||
/// A comparison for less than or equal to.
|
||||
/// </summary>
|
||||
LessThanEqual,
|
||||
/// <summary>
|
||||
/// A comparison for data type only.
|
||||
/// </summary>
|
||||
DataTypeCheck
|
||||
}
|
||||
}
|
||||
#endif
|
83
PROMS/DotNetBar Source Code/Validator/CustomValidator.cs
Normal file
83
PROMS/DotNetBar Source Code/Validator/CustomValidator.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
#if FRAMEWORK20
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace DevComponents.DotNetBar.Validator
|
||||
{
|
||||
public class CustomValidator : ValidatorBase
|
||||
{
|
||||
#region Events
|
||||
public event ValidateValueEventHandler ValidateValue;
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the CustomValidator class.
|
||||
/// </summary>
|
||||
public CustomValidator()
|
||||
{
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Implementation
|
||||
public override bool Validate(System.Windows.Forms.Control input)
|
||||
{
|
||||
ValidateValueEventArgs e = new ValidateValueEventArgs(input);
|
||||
OnValidateValue(e);
|
||||
|
||||
if (this.SuperValidator != null)
|
||||
this.SuperValidator.InvokeCustomValidatorValidateValue(this, e);
|
||||
|
||||
this.LastValidationResult = e.IsValid;
|
||||
return LastValidationResult;
|
||||
}
|
||||
protected virtual void OnValidateValue(ValidateValueEventArgs e)
|
||||
{
|
||||
ValidateValueEventHandler h = ValidateValue;
|
||||
if (h != null) h(this, e);
|
||||
}
|
||||
|
||||
private object _Tag;
|
||||
/// <summary>
|
||||
/// Gets or sets custom data associated with the validator
|
||||
/// </summary>
|
||||
[DefaultValue((string)null), Localizable(false), TypeConverter(typeof(StringConverter)), Category("Data"), Description("Custom data associated with the validator")]
|
||||
public object Tag
|
||||
{
|
||||
get { return _Tag; }
|
||||
set { _Tag = value; }
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
/// <summary>
|
||||
/// Defines delegate for CustomValidator ValidateValue event.
|
||||
/// </summary>
|
||||
/// <param name="sender">Sender</param>
|
||||
/// <param name="e">Event arguments</param>
|
||||
public delegate void ValidateValueEventHandler(object sender, ValidateValueEventArgs e);
|
||||
|
||||
public class ValidateValueEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the reference to the control to validate.
|
||||
/// </summary>
|
||||
public readonly Control ControlToValidate;
|
||||
/// <summary>
|
||||
/// Gets or sets whether control's value is valid.
|
||||
/// </summary>
|
||||
public bool IsValid = false;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the ValidateValueEventArgs class.
|
||||
/// </summary>
|
||||
/// <param name="controlToValidate">Control to validate.</param>
|
||||
public ValidateValueEventArgs(Control controlToValidate)
|
||||
{
|
||||
ControlToValidate = controlToValidate;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
@@ -0,0 +1,65 @@
|
||||
#if FRAMEWORK20
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace DevComponents.DotNetBar.Validator
|
||||
{
|
||||
internal class ErrorProviderWrapper : IErrorProvider
|
||||
{
|
||||
#region Constructors
|
||||
private ErrorProvider _ErrorProvider = null;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the ErrorProviderWrapper class.
|
||||
/// </summary>
|
||||
/// <param name="errorProvider"></param>
|
||||
public ErrorProviderWrapper(ErrorProvider errorProvider)
|
||||
{
|
||||
_ErrorProvider = errorProvider;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the ErrorProviderWrapper class.
|
||||
/// </summary>
|
||||
/// <param name="errorProvider"></param>
|
||||
/// <param name="iconPadding"></param>
|
||||
public ErrorProviderWrapper(ErrorProvider errorProvider, int iconPadding)
|
||||
{
|
||||
_ErrorProvider = errorProvider;
|
||||
_IconPadding = iconPadding;
|
||||
}
|
||||
|
||||
private int _IconPadding = 0;
|
||||
public int IconPadding
|
||||
{
|
||||
get { return _IconPadding; }
|
||||
set
|
||||
{
|
||||
_IconPadding = value;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region IErrorProvider Members
|
||||
public void SetError(Control control, string value)
|
||||
{
|
||||
_ErrorProvider.SetError(control, value);
|
||||
if (_IconPadding != 0)
|
||||
_ErrorProvider.SetIconPadding(control, _IconPadding);
|
||||
}
|
||||
|
||||
public void ClearError(Control control)
|
||||
{
|
||||
_ErrorProvider.SetError(control, null);
|
||||
if (_IconPadding != 0)
|
||||
_ErrorProvider.SetIconPadding(control, 0);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
#endif
|
288
PROMS/DotNetBar Source Code/Validator/HighlightPanel.cs
Normal file
288
PROMS/DotNetBar Source Code/Validator/HighlightPanel.cs
Normal file
@@ -0,0 +1,288 @@
|
||||
#if FRAMEWORK20
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
using System.Drawing;
|
||||
|
||||
namespace DevComponents.DotNetBar.Validator
|
||||
{
|
||||
internal class HighlightPanel : Control
|
||||
{
|
||||
#region Private Variables
|
||||
private Dictionary<Control, eHighlightColor> _Highlights = null;
|
||||
private List<HighlightRegion> _HighlightRegions = new List<HighlightRegion>();
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the HighlightPanel class.
|
||||
/// </summary>
|
||||
/// <param name="highlights"></param>
|
||||
public HighlightPanel(Dictionary<Control, eHighlightColor> highlights)
|
||||
{
|
||||
_Highlights = highlights;
|
||||
|
||||
this.SetStyle(ControlStyles.UserPaint, true);
|
||||
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
|
||||
this.SetStyle(ControlStyles.Opaque, true);
|
||||
this.SetStyle(ControlStyles.ResizeRedraw, true);
|
||||
this.SetStyle(DisplayHelp.DoubleBufferFlag, true);
|
||||
this.SetStyle(ControlStyles.Selectable, false);
|
||||
//this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Implementation
|
||||
protected override void OnPaint(PaintEventArgs e)
|
||||
{
|
||||
Graphics g = e.Graphics;
|
||||
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
|
||||
|
||||
//g.FillRectangle(Brushes.Yellow, this.ClientRectangle);
|
||||
//return;
|
||||
|
||||
foreach (HighlightRegion highlightRegion in _HighlightRegions)
|
||||
{
|
||||
Color[] colors = GetHighlightColors(highlightRegion.HighlightColor);
|
||||
Rectangle r = highlightRegion.Bounds;
|
||||
Color back = highlightRegion.BackColor;
|
||||
|
||||
r.Inflate(1, 1);
|
||||
DisplayHelp.FillRectangle(g, r, back);
|
||||
r.Inflate(-1, -1);
|
||||
|
||||
DisplayHelp.FillRoundedRectangle(g, r, 2, colors[0]);
|
||||
r.Inflate(-2, -2);
|
||||
DisplayHelp.DrawRectangle(g, colors[2], r);
|
||||
r.Inflate(1, 1);
|
||||
DisplayHelp.DrawRoundedRectangle(g, colors[1], r, 2);
|
||||
}
|
||||
|
||||
base.OnPaint(e);
|
||||
}
|
||||
|
||||
private Color[] GetHighlightColors(eHighlightColor color)
|
||||
{
|
||||
Color[] colors = new Color[3];
|
||||
if (color == eHighlightColor.Blue)
|
||||
{
|
||||
colors[0] = ColorScheme.GetColor(172, 0x6A9CD4);
|
||||
colors[1] = ColorScheme.GetColor(0x6A9CD4);
|
||||
colors[2] = ColorScheme.GetColor(0x5D7EA4);
|
||||
}
|
||||
else if (color == eHighlightColor.Orange)
|
||||
{
|
||||
colors[0] = ColorScheme.GetColor(172, 0xFF9C00);
|
||||
colors[1] = ColorScheme.GetColor(0xFF9C00);
|
||||
colors[2] = ColorScheme.GetColor(0xCC6600);
|
||||
}
|
||||
else if (color == eHighlightColor.Green)
|
||||
{
|
||||
colors[0] = ColorScheme.GetColor(172, 0x71B171);
|
||||
colors[1] = ColorScheme.GetColor(0x71B171);
|
||||
colors[2] = ColorScheme.GetColor(0x339933);
|
||||
}
|
||||
else if (color == eHighlightColor.Custom)
|
||||
{
|
||||
if (_CustomHighlightColors == null || _CustomHighlightColors.Length < 3)
|
||||
{
|
||||
colors[0] = Color.Red;
|
||||
colors[1] = Color.Red;
|
||||
colors[2] = Color.Red;
|
||||
}
|
||||
else
|
||||
{
|
||||
colors[0] = _CustomHighlightColors[0];
|
||||
colors[1] = _CustomHighlightColors[1];
|
||||
colors[2] = _CustomHighlightColors[2];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
colors[0] = ColorScheme.GetColor(172, 0xC63030);
|
||||
colors[1] = ColorScheme.GetColor(0xC63030);
|
||||
colors[2] = ColorScheme.GetColor(0x990000);
|
||||
}
|
||||
return colors;
|
||||
}
|
||||
|
||||
protected override void OnVisibleChanged(EventArgs e)
|
||||
{
|
||||
if (this.Visible && !_UpdatingRegion) UpdateRegion();
|
||||
base.OnVisibleChanged(e);
|
||||
}
|
||||
|
||||
protected override void OnHandleCreated(EventArgs e)
|
||||
{
|
||||
if (!_RegionInitialized) UpdateRegion();
|
||||
base.OnHandleCreated(e);
|
||||
}
|
||||
private bool _RegionInitialized = false;
|
||||
private bool _UpdatingRegion = false;
|
||||
internal void UpdateRegion()
|
||||
{
|
||||
if (_UpdatingRegion || !this.IsHandleCreated) return;
|
||||
|
||||
try
|
||||
{
|
||||
_UpdatingRegion = true;
|
||||
this.Region = null;
|
||||
_HighlightRegions.Clear();
|
||||
if (_Highlights == null) return;
|
||||
if (_Highlights.Count == 0 && _FocusHighlightControl == null)
|
||||
{
|
||||
this.Visible = false;
|
||||
return;
|
||||
}
|
||||
|
||||
bool processFocusControl = true;
|
||||
Region region = null;
|
||||
foreach (KeyValuePair<Control, eHighlightColor> item in _Highlights)
|
||||
{
|
||||
if (item.Value == eHighlightColor.None || !GetIsVisible(item.Key)) continue;
|
||||
if (item.Key == _FocusHighlightControl) processFocusControl = false;
|
||||
|
||||
Rectangle r = GetControlRect(item.Key);
|
||||
if (r.IsEmpty) continue;
|
||||
r.Inflate(2, 2);
|
||||
_HighlightRegions.Add(new HighlightRegion(r, GetBackColor(item.Key.Parent), item.Value));
|
||||
if (region == null)
|
||||
region = new Region(r);
|
||||
else
|
||||
region.Union(r);
|
||||
r.Inflate(-3, -3);
|
||||
region.Exclude(r);
|
||||
}
|
||||
|
||||
if (processFocusControl && _FocusHighlightControl != null && _FocusHighlightControl.Visible)
|
||||
{
|
||||
Rectangle r = GetControlRect(_FocusHighlightControl);
|
||||
if (!r.IsEmpty)
|
||||
{
|
||||
r.Inflate(2, 2);
|
||||
_HighlightRegions.Add(new HighlightRegion(r, GetBackColor(_FocusHighlightControl.Parent), _FocusHighlightColor));
|
||||
if (region == null)
|
||||
region = new Region(r);
|
||||
else
|
||||
region.Union(r);
|
||||
r.Inflate(-3, -3);
|
||||
region.Exclude(r);
|
||||
}
|
||||
}
|
||||
|
||||
this.Region = region;
|
||||
if (region == null)
|
||||
this.Visible = false;
|
||||
else if (!this.Visible)
|
||||
{
|
||||
this.Visible = true;
|
||||
this.BringToFront();
|
||||
}
|
||||
this.Invalidate();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_UpdatingRegion = false;
|
||||
_RegionInitialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
private bool GetIsVisible(Control control)
|
||||
{
|
||||
if (!control.Visible) return false;
|
||||
if (control.Parent == null || !control.IsHandleCreated) return control.Visible;
|
||||
|
||||
WinApi.RECT rect = new WinApi.RECT();
|
||||
WinApi.GetWindowRect(control.Handle, ref rect);
|
||||
Point pp = control.Parent.PointToClient(new Point(rect.Left + 3, rect.Top + 3));
|
||||
//IntPtr handle = NativeFunctions.ChildWindowFromPoint(control.Parent.Handle, new NativeFunctions.POINT(pp.X, pp.Y));
|
||||
IntPtr handle = NativeFunctions.ChildWindowFromPointEx(control.Parent.Handle, new NativeFunctions.POINT(pp.X, pp.Y), (uint)NativeFunctions.WindowFromPointFlags.CWP_SKIPINVISIBLE);
|
||||
if (handle == IntPtr.Zero) return control.Visible;
|
||||
|
||||
Control c = Control.FromHandle(handle);
|
||||
if (c != null && c != control && c != this && c != control.Parent)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return control.Visible;
|
||||
}
|
||||
private Color GetBackColor(Control control)
|
||||
{
|
||||
Color backColor = control.BackColor;
|
||||
if (backColor.IsEmpty || backColor == Color.Transparent)
|
||||
backColor = SystemColors.Control;
|
||||
else if (backColor.A < 255)
|
||||
backColor = Color.FromArgb(255, backColor);
|
||||
return backColor;
|
||||
}
|
||||
protected override void OnResize(EventArgs e)
|
||||
{
|
||||
UpdateRegion();
|
||||
base.OnResize(e);
|
||||
}
|
||||
|
||||
private Rectangle GetControlRect(Control c)
|
||||
{
|
||||
if (!c.IsHandleCreated) return Rectangle.Empty;
|
||||
|
||||
WinApi.RECT rect = new WinApi.RECT();
|
||||
WinApi.GetWindowRect(c.Handle, ref rect);
|
||||
Point p = this.PointToClient(rect.Location);
|
||||
return new Rectangle(p, rect.Size);
|
||||
|
||||
//Point p = c.PointToScreen(Point.Empty);
|
||||
//p = this.PointToClient(p);
|
||||
//return new Rectangle(p, c.Size);
|
||||
}
|
||||
|
||||
private struct HighlightRegion
|
||||
{
|
||||
public Rectangle Bounds;
|
||||
public Color BackColor;
|
||||
public eHighlightColor HighlightColor;
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the HighlightRegion structure.
|
||||
/// </summary>
|
||||
/// <param name="bounds"></param>
|
||||
/// <param name="backColor"></param>
|
||||
/// <param name="highlightColor"></param>
|
||||
public HighlightRegion(Rectangle bounds, Color backColor, eHighlightColor highlightColor)
|
||||
{
|
||||
Bounds = bounds;
|
||||
BackColor = backColor;
|
||||
HighlightColor = highlightColor;
|
||||
}
|
||||
}
|
||||
|
||||
private Control _FocusHighlightControl;
|
||||
public Control FocusHighlightControl
|
||||
{
|
||||
get { return _FocusHighlightControl; }
|
||||
set { _FocusHighlightControl = value; }
|
||||
}
|
||||
|
||||
private eHighlightColor _FocusHighlightColor = eHighlightColor.Blue;
|
||||
public eHighlightColor FocusHighlightColor
|
||||
{
|
||||
get { return _FocusHighlightColor; }
|
||||
set { _FocusHighlightColor = value; }
|
||||
}
|
||||
|
||||
private Color[] _CustomHighlightColors = null;
|
||||
/// <summary>
|
||||
/// Gets or sets the array of colors used to render custom highlight color. Control expects 3 colors in array to be specified which define the highlight border.
|
||||
/// </summary>
|
||||
public Color[] CustomHighlightColors
|
||||
{
|
||||
get { return _CustomHighlightColors; }
|
||||
set
|
||||
{
|
||||
_CustomHighlightColors = value;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
#endif
|
599
PROMS/DotNetBar Source Code/Validator/Highlighter.cs
Normal file
599
PROMS/DotNetBar Source Code/Validator/Highlighter.cs
Normal file
@@ -0,0 +1,599 @@
|
||||
#if FRAMEWORK20
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Windows.Forms;
|
||||
using System.Drawing;
|
||||
|
||||
namespace DevComponents.DotNetBar.Validator
|
||||
{
|
||||
[ToolboxBitmap(typeof(Highlighter), "Validator.Highlighter.ico"), ToolboxItem(true), ProvideProperty("HighlightColor", typeof(Control)),
|
||||
ProvideProperty("HighlightOnFocus", typeof(Control)),
|
||||
System.Runtime.InteropServices.ComVisible(false), Designer("DevComponents.DotNetBar.Design.HighlighterDesigner, DevComponents.DotNetBar.Design, Version=14.1.0.37, Culture=neutral, PublicKeyToken=90f470f34c89ccaf")]
|
||||
public class Highlighter : Component, IExtenderProvider, IErrorProvider
|
||||
{
|
||||
#region Private Variables
|
||||
private Dictionary<Control, eHighlightColor> _Highlights = new Dictionary<Control, eHighlightColor>();
|
||||
private Dictionary<Control, bool> _HighlightOnFocus = new Dictionary<Control, bool>();
|
||||
#endregion
|
||||
|
||||
#region Implementation
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (_ContainerControl != null)
|
||||
{
|
||||
_ContainerControl.SizeChanged -= ContainerControlSizeChanged;
|
||||
_ContainerControl.HandleCreated -= ContainerControlHandleCreated;
|
||||
}
|
||||
|
||||
if (_HighlightPanel != null && _HighlightPanel.Parent == null && !_HighlightPanel.IsDisposed)
|
||||
{
|
||||
_HighlightPanel.Dispose();
|
||||
_HighlightPanel = null;
|
||||
}
|
||||
else
|
||||
_HighlightPanel = null;
|
||||
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves whether control is highlighted when it receives input focus.
|
||||
/// </summary>
|
||||
[DefaultValue(false), Localizable(true), Description("Indicates whether control is highlighted when it receives input focus.")]
|
||||
public bool GetHighlightOnFocus(Control c)
|
||||
{
|
||||
if (_HighlightOnFocus.ContainsKey(c))
|
||||
{
|
||||
return _HighlightOnFocus[c];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/// <summary>
|
||||
/// Sets whether control is highlighted when it receives input focus.
|
||||
/// </summary>
|
||||
/// <param name="c">Reference to supported control.</param>
|
||||
/// <param name="highlight">Indicates whether to highlight control on focus.</param>
|
||||
public void SetHighlightOnFocus(Control c, bool highlight)
|
||||
{
|
||||
if (c == null) throw new NullReferenceException();
|
||||
|
||||
if (_HighlightOnFocus.ContainsKey(c))
|
||||
{
|
||||
if (!highlight)
|
||||
{
|
||||
RemoveHighlightOnFocus(_HighlightOnFocus, c);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if(highlight)
|
||||
AddHighlightOnFocus(_HighlightOnFocus, c);
|
||||
}
|
||||
private void AddHighlightOnFocus(Dictionary<Control, bool> highlightOnFocus, Control c)
|
||||
{
|
||||
c.Enter += ControlHighlightEnter;
|
||||
c.Leave += ControlHighlightLeave;
|
||||
c.VisibleChanged += ControlHighlightVisibleChanged;
|
||||
highlightOnFocus.Add(c, true);
|
||||
}
|
||||
|
||||
void ControlHighlightVisibleChanged(object sender, EventArgs e)
|
||||
{
|
||||
if(_HighlightPanel!=null && _HighlightPanel.FocusHighlightControl == sender)
|
||||
UpdateHighlighterRegion();
|
||||
}
|
||||
void ControlHighlightLeave(object sender, EventArgs e)
|
||||
{
|
||||
if (_HighlightPanel != null) _HighlightPanel.FocusHighlightControl = null;
|
||||
UpdateHighlighterRegion();
|
||||
}
|
||||
void ControlHighlightEnter(object sender, EventArgs e)
|
||||
{
|
||||
if (_HighlightPanel != null)
|
||||
{
|
||||
if (!_HighlightPanel.Visible) _HighlightPanel.Visible = true;
|
||||
_HighlightPanel.BringToFront();
|
||||
_HighlightPanel.FocusHighlightControl = (Control)sender;
|
||||
}
|
||||
UpdateHighlighterRegion();
|
||||
}
|
||||
private void RemoveHighlightOnFocus(Dictionary<Control, bool> highlightOnFocus, Control c)
|
||||
{
|
||||
c.Enter -= ControlHighlightEnter;
|
||||
c.Leave -= ControlHighlightLeave;
|
||||
c.VisibleChanged -= ControlHighlightVisibleChanged;
|
||||
highlightOnFocus.Remove(c);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the highlight color that is applied to the control.
|
||||
/// </summary>
|
||||
[DefaultValue(eHighlightColor.None), Localizable(true), Description("Indicates the highlight color that is applied to the control.")]
|
||||
public eHighlightColor GetHighlightColor(Control c)
|
||||
{
|
||||
if (_Highlights.ContainsKey(c))
|
||||
{
|
||||
return _Highlights[c];
|
||||
}
|
||||
return eHighlightColor.None;
|
||||
}
|
||||
/// <summary>
|
||||
/// Sets the highlight color for the control.
|
||||
/// </summary>
|
||||
/// <param name="c">Reference to supported control.</param>
|
||||
/// <param name="highlightColor">Highlight color.</param>
|
||||
public void SetHighlightColor(Control c, eHighlightColor highlightColor)
|
||||
{
|
||||
if (_Highlights.ContainsKey(c))
|
||||
{
|
||||
if (highlightColor == eHighlightColor.None)
|
||||
{
|
||||
RemoveHighlight(_Highlights, c);
|
||||
}
|
||||
else
|
||||
{
|
||||
eHighlightColor color = _Highlights[c];
|
||||
RemoveHighlight(_Highlights, c);
|
||||
AddHighlight(_Highlights, c, highlightColor);
|
||||
}
|
||||
}
|
||||
else if (highlightColor != eHighlightColor.None)
|
||||
{
|
||||
AddHighlight(_Highlights, c, highlightColor);
|
||||
}
|
||||
}
|
||||
|
||||
private Dictionary<TabControl, int> _TabControl1 = new Dictionary<TabControl, int>();
|
||||
private Dictionary<SuperTabControl, int> _SuperTabControl1 = new Dictionary<SuperTabControl, int>();
|
||||
private Dictionary<System.Windows.Forms.TabControl, int> _TabControl2 = new Dictionary<System.Windows.Forms.TabControl, int>();
|
||||
private Dictionary<Panel, int> _ParentPanel = new Dictionary<Panel, int>();
|
||||
|
||||
private void AddHighlight(Dictionary<Control, eHighlightColor> highlights, Control c, eHighlightColor highlightColor)
|
||||
{
|
||||
highlights.Add(c, highlightColor);
|
||||
c.LocationChanged += new EventHandler(ControlLocationChanged);
|
||||
c.SizeChanged += new EventHandler(ControlSizeChanged);
|
||||
c.VisibleChanged += new EventHandler(ControlVisibleChanged);
|
||||
if (_HighlightPanel != null)
|
||||
{
|
||||
if (!_HighlightPanel.Visible) _HighlightPanel.Visible = true;
|
||||
_HighlightPanel.BringToFront();
|
||||
}
|
||||
|
||||
if(c.Parent == null)
|
||||
c.ParentChanged += ControlParentChanged;
|
||||
else
|
||||
AddTabControlHandlers(c);
|
||||
|
||||
UpdateHighlighterRegion();
|
||||
}
|
||||
|
||||
private void ControlParentChanged(object sender, EventArgs e)
|
||||
{
|
||||
Control c = (Control)sender;
|
||||
c.ParentChanged -= ControlParentChanged;
|
||||
AddTabControlHandlers(c);
|
||||
}
|
||||
|
||||
private void AddTabControlHandlers(Control c)
|
||||
{
|
||||
TabControl tab1 = GetParentControl(c, typeof(TabControl)) as TabControl;
|
||||
|
||||
if (tab1 != null)
|
||||
{
|
||||
if (_TabControl1.ContainsKey(tab1))
|
||||
_TabControl1[tab1] = _TabControl1[tab1] + 1;
|
||||
else
|
||||
{
|
||||
_TabControl1.Add(tab1, 1);
|
||||
tab1.SelectedTabChanged += TabControl1SelectedTabChanged;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
SuperTabControl tab = GetParentControl(c, typeof(SuperTabControl)) as SuperTabControl;
|
||||
|
||||
if (tab != null)
|
||||
{
|
||||
if (_SuperTabControl1.ContainsKey(tab))
|
||||
_SuperTabControl1[tab] = _SuperTabControl1[tab] + 1;
|
||||
else
|
||||
{
|
||||
_SuperTabControl1.Add(tab, 1);
|
||||
tab.SelectedTabChanged += SuperTabControl1SelectedTabChanged;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
System.Windows.Forms.TabControl tab2 =
|
||||
GetParentControl(c, typeof (System.Windows.Forms.TabControl)) as System.Windows.Forms.TabControl;
|
||||
|
||||
if (tab2 != null)
|
||||
{
|
||||
if (_TabControl2.ContainsKey(tab2))
|
||||
_TabControl2[tab2] = _TabControl2[tab2] + 1;
|
||||
else
|
||||
{
|
||||
_TabControl2.Add(tab2, 1);
|
||||
tab2.SelectedIndexChanged += WinFormsTabSelectedIndexChanged;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Panel parentPanel = GetParentControl(c, typeof (Panel)) as Panel;
|
||||
|
||||
if (parentPanel != null)
|
||||
{
|
||||
if (_ParentPanel.ContainsKey(parentPanel))
|
||||
_ParentPanel[parentPanel] = _ParentPanel[parentPanel] + 1;
|
||||
else
|
||||
{
|
||||
_ParentPanel.Add(parentPanel, 1);
|
||||
parentPanel.Resize += ParentPanelResized;
|
||||
parentPanel.LocationChanged += ParentPanelLocationChanged;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ParentPanelLocationChanged(object sender, EventArgs e)
|
||||
{
|
||||
UpdateHighlights();
|
||||
}
|
||||
private void ParentPanelResized(object sender, EventArgs e)
|
||||
{
|
||||
UpdateHighlights();
|
||||
}
|
||||
private void WinFormsTabSelectedIndexChanged(object sender, EventArgs e)
|
||||
{
|
||||
UpdateHighlighterRegion();
|
||||
}
|
||||
|
||||
private void TabControl1SelectedTabChanged(object sender, TabStripTabChangedEventArgs e)
|
||||
{
|
||||
UpdateHighlighterRegion();
|
||||
}
|
||||
|
||||
private void SuperTabControl1SelectedTabChanged(object sender, SuperTabStripSelectedTabChangedEventArgs e)
|
||||
{
|
||||
UpdateHighlighterRegion();
|
||||
}
|
||||
|
||||
private Control GetParentControl(Control c, Type parentType)
|
||||
{
|
||||
Control parent = c.Parent;
|
||||
while (parent != null)
|
||||
{
|
||||
if (parentType.IsAssignableFrom(parent.GetType())) return parent;
|
||||
parent = parent.Parent;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
void ControlVisibleChanged(object sender, EventArgs e)
|
||||
{
|
||||
UpdateHighlighterRegion();
|
||||
}
|
||||
|
||||
private void ControlSizeChanged(object sender, EventArgs e)
|
||||
{
|
||||
UpdateHighlighterRegion();
|
||||
}
|
||||
private void ControlLocationChanged(object sender, EventArgs e)
|
||||
{
|
||||
UpdateHighlighterRegion();
|
||||
}
|
||||
|
||||
private void UpdateHighlighterRegion()
|
||||
{
|
||||
if (_HighlightPanel != null) _HighlightPanel.UpdateRegion();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the highlighted controls border. Usually call to this method is not needed but under
|
||||
/// certain scenarios where highlighter does not automatically detects the change in visibility of
|
||||
/// the highlighted control call to this method is necessary.
|
||||
/// </summary>
|
||||
public void UpdateHighlights()
|
||||
{
|
||||
UpdateHighlighterRegion();
|
||||
}
|
||||
|
||||
private void RemoveHighlight(Dictionary<Control, eHighlightColor> highlights, Control c)
|
||||
{
|
||||
highlights.Remove(c);
|
||||
c.LocationChanged -= new EventHandler(ControlLocationChanged);
|
||||
c.SizeChanged -= new EventHandler(ControlSizeChanged);
|
||||
c.VisibleChanged -= new EventHandler(ControlVisibleChanged);
|
||||
|
||||
TabControl tab1 = GetParentControl(c, typeof(TabControl)) as TabControl;
|
||||
if (tab1 != null)
|
||||
{
|
||||
if (_TabControl1.ContainsKey(tab1))
|
||||
{
|
||||
if (_TabControl1[tab1] == 1)
|
||||
{
|
||||
_TabControl1.Remove(tab1);
|
||||
tab1.SelectedTabChanged -= TabControl1SelectedTabChanged;
|
||||
}
|
||||
else
|
||||
_TabControl1[tab1] = _TabControl1[tab1] - 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
SuperTabControl tab = GetParentControl(c, typeof(SuperTabControl)) as SuperTabControl;
|
||||
|
||||
if (tab != null)
|
||||
{
|
||||
if (_SuperTabControl1.ContainsKey(tab))
|
||||
{
|
||||
if (_SuperTabControl1[tab] == 1)
|
||||
{
|
||||
_SuperTabControl1.Remove(tab);
|
||||
tab.SelectedTabChanged -= SuperTabControl1SelectedTabChanged;
|
||||
}
|
||||
else
|
||||
_SuperTabControl1[tab] = _SuperTabControl1[tab] - 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
System.Windows.Forms.TabControl tab2 =
|
||||
GetParentControl(c, typeof (System.Windows.Forms.TabControl)) as System.Windows.Forms.TabControl;
|
||||
|
||||
if (tab2 != null)
|
||||
{
|
||||
if (_TabControl2.ContainsKey(tab2))
|
||||
{
|
||||
if (_TabControl2[tab2] == 1)
|
||||
{
|
||||
_TabControl2.Remove(tab2);
|
||||
tab2.SelectedIndexChanged -= WinFormsTabSelectedIndexChanged;
|
||||
}
|
||||
else
|
||||
_TabControl2[tab2] = _TabControl2[tab2] - 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Panel parentPanel = GetParentControl(c, typeof (Panel)) as Panel;
|
||||
|
||||
if (parentPanel != null)
|
||||
{
|
||||
if (_ParentPanel.ContainsKey(parentPanel))
|
||||
{
|
||||
if (_ParentPanel[parentPanel] == 1)
|
||||
{
|
||||
_ParentPanel.Remove(parentPanel);
|
||||
parentPanel.LocationChanged -= ParentPanelLocationChanged;
|
||||
parentPanel.SizeChanged -= ParentPanelResized;
|
||||
}
|
||||
else
|
||||
_ParentPanel[parentPanel] = _ParentPanel[parentPanel] - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
UpdateHighlighterRegion();
|
||||
}
|
||||
|
||||
internal Dictionary<Control, eHighlightColor> Highlights
|
||||
{
|
||||
get
|
||||
{
|
||||
return _Highlights;
|
||||
}
|
||||
}
|
||||
|
||||
private eHighlightColor _FocusHighlightColor = eHighlightColor.Blue;
|
||||
/// <summary>
|
||||
/// Indicates the highlight focus color.
|
||||
/// </summary>
|
||||
[DefaultValue(eHighlightColor.Blue), Category("Appearance"), Description("Indicates the highlight focus color."), Localizable(true)]
|
||||
public eHighlightColor FocusHighlightColor
|
||||
{
|
||||
get { return _FocusHighlightColor; }
|
||||
set
|
||||
{
|
||||
_FocusHighlightColor = value;
|
||||
if (_HighlightPanel != null)
|
||||
{
|
||||
_HighlightPanel.FocusHighlightColor = value;
|
||||
UpdateHighlighterRegion();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private HighlightPanel _HighlightPanel = null;
|
||||
private Control _ContainerControl = null;
|
||||
/// <summary>
|
||||
/// Gets or sets the container control highlighter is bound to. The container control must be set in order for highlighter to work.
|
||||
/// Container control should always be a form.
|
||||
/// </summary>
|
||||
[DefaultValue(null), Description("Indicates container control highlighter is bound to. Should be set to parent form."), Category("Behavior")]
|
||||
public Control ContainerControl
|
||||
{
|
||||
get
|
||||
{
|
||||
return _ContainerControl;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (this.DesignMode)
|
||||
{
|
||||
_ContainerControl = value;
|
||||
return;
|
||||
}
|
||||
|
||||
if (_ContainerControl != value)
|
||||
{
|
||||
if (_ContainerControl != null)
|
||||
{
|
||||
_ContainerControl.SizeChanged -= ContainerControlSizeChanged;
|
||||
_ContainerControl.HandleCreated -= ContainerControlHandleCreated;
|
||||
if (_HighlightPanel != null && _HighlightPanel.Parent == _ContainerControl)
|
||||
_ContainerControl.Controls.Remove(_HighlightPanel);
|
||||
}
|
||||
|
||||
_ContainerControl = value;
|
||||
|
||||
if (_ContainerControl != null)
|
||||
{
|
||||
if (_HighlightPanel == null)
|
||||
{
|
||||
_HighlightPanel = new HighlightPanel(_Highlights);
|
||||
_HighlightPanel.FocusHighlightColor = _FocusHighlightColor;
|
||||
_HighlightPanel.Margin = new System.Windows.Forms.Padding(0);
|
||||
_HighlightPanel.Padding = new System.Windows.Forms.Padding(0);
|
||||
_HighlightPanel.CustomHighlightColors = _CustomHighlightColors;
|
||||
_HighlightPanel.Visible = false;
|
||||
}
|
||||
_ContainerControl.SizeChanged += ContainerControlSizeChanged;
|
||||
_ContainerControl.HandleCreated += ContainerControlHandleCreated;
|
||||
_ContainerControl.Controls.Add(_HighlightPanel);
|
||||
UpdateHighlightPanelBounds();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ContainerControlHandleCreated(object sender, EventArgs e)
|
||||
{
|
||||
if (_Highlights.Count > 0 && _HighlightPanel != null && !_HighlightPanel.Visible)
|
||||
_HighlightPanel.Visible = true;
|
||||
}
|
||||
|
||||
private void UpdateHighlightPanelBounds()
|
||||
{
|
||||
Rectangle bounds = new Rectangle(0, 0, _ContainerControl.ClientRectangle.Width, _ContainerControl.ClientRectangle.Height);
|
||||
if (_HighlightPanel.Parent is Form)
|
||||
{
|
||||
Form form = _HighlightPanel.Parent as Form;
|
||||
if (form.AutoSize)
|
||||
{
|
||||
bounds.X += form.Padding.Left;
|
||||
bounds.Y += form.Padding.Top;
|
||||
bounds.Width -= form.Padding.Horizontal;
|
||||
bounds.Height -= form.Padding.Vertical;
|
||||
}
|
||||
}
|
||||
if(_HighlightPanel.Bounds.Equals(bounds))
|
||||
_HighlightPanel.UpdateRegion();
|
||||
else
|
||||
_HighlightPanel.Bounds = bounds;
|
||||
//_HighlightPanel.UpdateRegion();
|
||||
_HighlightPanel.BringToFront();
|
||||
}
|
||||
|
||||
private Timer _DelayTimer = null;
|
||||
private void ContainerControlSizeChanged(object sender, EventArgs e)
|
||||
{
|
||||
if (!BarFunctions.IsVista)
|
||||
{
|
||||
Form form = sender as Form;
|
||||
if (form != null)
|
||||
{
|
||||
if (_DelayTimer == null)
|
||||
{
|
||||
_DelayTimer = new Timer();
|
||||
_DelayTimer.Interval = 100;
|
||||
_DelayTimer.Tick += new EventHandler(DelayTimerTick);
|
||||
_DelayTimer.Start();
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
UpdateHighlightPanelBounds();
|
||||
}
|
||||
|
||||
void DelayTimerTick(object sender, EventArgs e)
|
||||
{
|
||||
Timer timer = _DelayTimer;
|
||||
_DelayTimer = null;
|
||||
timer.Tick -= new EventHandler(DelayTimerTick);
|
||||
timer.Stop();
|
||||
timer.Dispose();
|
||||
UpdateHighlightPanelBounds();
|
||||
}
|
||||
|
||||
private Color[] _CustomHighlightColors = null;
|
||||
/// <summary>
|
||||
/// Gets or sets the array of colors used to render custom highlight color. Control expects 3 colors in array to be specified which define the highlight border.
|
||||
/// </summary>
|
||||
[DefaultValue(null), Category("Appearance"), Description("Array of colors used to render custom highlight color. Control expects 3 colors in array to be specified which define the highlight border.")]
|
||||
public Color[] CustomHighlightColors
|
||||
{
|
||||
get { return _CustomHighlightColors; }
|
||||
set
|
||||
{
|
||||
_CustomHighlightColors = value;
|
||||
if (_HighlightPanel != null)
|
||||
{
|
||||
_HighlightPanel.CustomHighlightColors = _CustomHighlightColors;
|
||||
_HighlightPanel.Invalidate();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region IExtenderProvider Members
|
||||
|
||||
public bool CanExtend(object extendee)
|
||||
{
|
||||
return (extendee is Control);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Licensing
|
||||
#if !TRIAL
|
||||
private string _LicenseKey = "";
|
||||
[Browsable(false), DefaultValue("")]
|
||||
public string LicenseKey
|
||||
{
|
||||
get { return _LicenseKey; }
|
||||
set
|
||||
{
|
||||
if (NativeFunctions.ValidateLicenseKey(value))
|
||||
return;
|
||||
_LicenseKey = (!NativeFunctions.CheckLicenseKey(value) ? "9dsjkhds7" : value);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endregion
|
||||
|
||||
#region IErrorProvider Members
|
||||
|
||||
void IErrorProvider.SetError(Control control, string value)
|
||||
{
|
||||
this.SetHighlightColor(control, eHighlightColor.Red);
|
||||
}
|
||||
|
||||
void IErrorProvider.ClearError(Control control)
|
||||
{
|
||||
this.SetHighlightColor(control, eHighlightColor.None);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
/// <summary>
|
||||
/// Defines highlight colors provided by Highlighter control.
|
||||
/// </summary>
|
||||
public enum eHighlightColor
|
||||
{
|
||||
None,
|
||||
Red,
|
||||
Blue,
|
||||
Green,
|
||||
Orange,
|
||||
Custom
|
||||
}
|
||||
}
|
||||
#endif
|
BIN
PROMS/DotNetBar Source Code/Validator/Highlighter.ico
Normal file
BIN
PROMS/DotNetBar Source Code/Validator/Highlighter.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.2 KiB |
27
PROMS/DotNetBar Source Code/Validator/IErrorProvider.cs
Normal file
27
PROMS/DotNetBar Source Code/Validator/IErrorProvider.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
#if FRAMEWORK20
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace DevComponents.DotNetBar.Validator
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines an interface that can be implemented by custom error providers to be used with SuperValidator.
|
||||
/// </summary>
|
||||
public interface IErrorProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets the error state on the control.
|
||||
/// </summary>
|
||||
/// <param name="control">Control for which error state is being set.</param>
|
||||
/// <param name="value">The error message from validator.</param>
|
||||
void SetError(Control control, string value);
|
||||
/// <summary>
|
||||
/// Clears the error state for the control.
|
||||
/// </summary>
|
||||
/// <param name="control">Control to clear error state for.</param>
|
||||
void ClearError(Control control);
|
||||
}
|
||||
}
|
||||
#endif
|
110
PROMS/DotNetBar Source Code/Validator/RangeValidator.cs
Normal file
110
PROMS/DotNetBar Source Code/Validator/RangeValidator.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
#if FRAMEWORK20
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace DevComponents.DotNetBar.Validator
|
||||
{
|
||||
public class RangeValidator : ValidatorBase
|
||||
{
|
||||
#region Constructors
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the RangeValidator class.
|
||||
/// </summary>
|
||||
public RangeValidator()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the RangeValidator class.
|
||||
/// </summary>
|
||||
public RangeValidator(string errorMessage, object minimumValue, object maximumValue)
|
||||
{
|
||||
this.ErrorMessage = errorMessage;
|
||||
this.MinimumValue = minimumValue;
|
||||
this.MaximumValue = maximumValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the RangeValidator class.
|
||||
/// </summary>
|
||||
public RangeValidator(string errorMessage, string optionalValidationGroup, object minimumValue, object maximumValue)
|
||||
{
|
||||
this.ErrorMessage = errorMessage;
|
||||
this.OptionalValidationGroup = optionalValidationGroup;
|
||||
this.MinimumValue = minimumValue;
|
||||
this.MaximumValue = maximumValue;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Implementation
|
||||
public override bool Validate(System.Windows.Forms.Control input)
|
||||
{
|
||||
if (_MaximumValue == null && _MinimumValue == null) return true;
|
||||
object value = GetControlValue(input);
|
||||
|
||||
if (value == null)
|
||||
{
|
||||
if (_MinimumValue == null) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
if(value == "" && _IsEmptyStringValid)
|
||||
return true;
|
||||
|
||||
bool isMinimumValid = true;
|
||||
if (_MinimumValue != null)
|
||||
{
|
||||
isMinimumValid = CompareValidator.Compare(value, _MinimumValue, eValidationCompareOperator.GreaterThanEqual);
|
||||
if (!isMinimumValid) return false;
|
||||
}
|
||||
bool isMaximumValid = true;
|
||||
if (_MaximumValue != null)
|
||||
{
|
||||
isMaximumValid = CompareValidator.Compare(value, _MaximumValue, eValidationCompareOperator.LessThanEqual);
|
||||
}
|
||||
|
||||
this.LastValidationResult = isMinimumValid & isMaximumValid;
|
||||
return this.LastValidationResult;
|
||||
}
|
||||
|
||||
private object _MaximumValue = null;
|
||||
/// <summary>
|
||||
/// Gets or sets the maximum value control may have.
|
||||
/// </summary>
|
||||
[DefaultValue((string)null), Category("Behavior"), Description("Indicates maximum value control may have."), TypeConverter(typeof(StringConverter))]
|
||||
public object MaximumValue
|
||||
{
|
||||
get { return _MaximumValue; }
|
||||
set { _MaximumValue = value; }
|
||||
}
|
||||
|
||||
private object _MinimumValue = null;
|
||||
/// <summary>
|
||||
/// Gets or sets the minimum value control may have.
|
||||
/// </summary>
|
||||
[DefaultValue((string)null), Category("Behavior"), Description("Indicates minimum value control may have."), TypeConverter(typeof(StringConverter))]
|
||||
public object MinimumValue
|
||||
{
|
||||
get { return _MinimumValue; }
|
||||
set { _MinimumValue = value; }
|
||||
}
|
||||
|
||||
private bool _IsEmptyStringValid = false;
|
||||
/// <summary>
|
||||
/// Indicates whether empty string of zero length is considered valid input.
|
||||
/// </summary>
|
||||
[DefaultValue(false), Category("Behavior"), Description("Indicates whether empty string of zero length is considered valid input.")]
|
||||
public bool IsEmptyStringValid
|
||||
{
|
||||
get { return _IsEmptyStringValid; }
|
||||
set
|
||||
{
|
||||
_IsEmptyStringValid = value;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
#endif
|
@@ -0,0 +1,113 @@
|
||||
#if FRAMEWORK20
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.ComponentModel;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace DevComponents.DotNetBar.Validator
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the regular expression validator used with SuperValidator control.
|
||||
/// </summary>
|
||||
public class RegularExpressionValidator : ValidatorBase
|
||||
{
|
||||
#region Implementation
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the RegularExpressionValidator class.
|
||||
/// </summary>
|
||||
public RegularExpressionValidator()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the RegularExpressionValidator class.
|
||||
/// </summary>
|
||||
/// <param name="validationExpression"></param>
|
||||
public RegularExpressionValidator(string errorMessage, string validationExpression)
|
||||
{
|
||||
_ValidationExpression = validationExpression;
|
||||
this.ErrorMessage = errorMessage;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the RegularExpressionValidator class.
|
||||
/// </summary>
|
||||
/// <param name="validationExpression"></param>
|
||||
public RegularExpressionValidator(string errorMessage, string validationExpression, bool emptyValueIsValid)
|
||||
{
|
||||
_ValidationExpression = validationExpression;
|
||||
this.ErrorMessage = errorMessage;
|
||||
this.EmptyValueIsValid = emptyValueIsValid;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the RegularExpressionValidator class.
|
||||
/// </summary>
|
||||
/// <param name="validationExpression"></param>
|
||||
public RegularExpressionValidator(string errorMessage, string validationExpression, string optionalValidationGroup, bool emptyValueIsValid)
|
||||
{
|
||||
_ValidationExpression = validationExpression;
|
||||
this.ErrorMessage = errorMessage;
|
||||
this.OptionalValidationGroup = optionalValidationGroup;
|
||||
}
|
||||
|
||||
private string _ValidationExpression = "";
|
||||
/// <summary>
|
||||
/// Gets or sets regular expression used to validate controls value.
|
||||
/// </summary>
|
||||
[DefaultValue(""), Category("Behavior"), Description("Indicates regular expression used to validate controls value."), Editor("DevComponents.DotNetBar.Design.ValidationExpressionEditor, DevComponents.DotNetBar.Design, Version=14.1.0.37, Culture=neutral, PublicKeyToken=90f470f34c89ccaf", typeof(System.Drawing.Design.UITypeEditor))]
|
||||
public string ValidationExpression
|
||||
{
|
||||
get { return _ValidationExpression; }
|
||||
set
|
||||
{
|
||||
_ValidationExpression = value;
|
||||
}
|
||||
}
|
||||
|
||||
private bool _EmptyValueIsValid = false;
|
||||
/// <summary>
|
||||
/// Gets or sets whether empty value is considered valid value by the validator. Default value is false.
|
||||
/// </summary>
|
||||
[DefaultValue(false), Category("Behavior"), Description("Indicates whether empty value is considered valid value by the validator")]
|
||||
public bool EmptyValueIsValid
|
||||
{
|
||||
get { return _EmptyValueIsValid; }
|
||||
set
|
||||
{
|
||||
_EmptyValueIsValid = value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public override bool Validate(System.Windows.Forms.Control input)
|
||||
{
|
||||
if (string.IsNullOrEmpty(_ValidationExpression))
|
||||
{
|
||||
this.LastValidationResult = true;
|
||||
return true;
|
||||
}
|
||||
object value = GetControlValue(input);
|
||||
string textValue = "";
|
||||
|
||||
if (value != null)
|
||||
{
|
||||
textValue = value.ToString();
|
||||
}
|
||||
|
||||
if (_EmptyValueIsValid && string.IsNullOrEmpty(textValue))
|
||||
{
|
||||
this.LastValidationResult = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
Regex regularExpression = new Regex(_ValidationExpression);
|
||||
|
||||
this.LastValidationResult = regularExpression.IsMatch(textValue);
|
||||
return LastValidationResult;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
#endif
|
158
PROMS/DotNetBar Source Code/Validator/RequiredFieldValidator.cs
Normal file
158
PROMS/DotNetBar Source Code/Validator/RequiredFieldValidator.cs
Normal file
@@ -0,0 +1,158 @@
|
||||
#if FRAMEWORK20
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace DevComponents.DotNetBar.Validator
|
||||
{
|
||||
/// <summary>
|
||||
/// Describes required field validator used with SuperValidator control.
|
||||
/// </summary>
|
||||
[TypeConverter("DevComponents.DotNetBar.Design.RequiredFieldValidatorConverter, DevComponents.DotNetBar.Design, Version=14.1.0.37, Culture=neutral, PublicKeyToken=90f470f34c89ccaf"), DesignTimeVisible(false), ToolboxItem(false), Localizable(true)]
|
||||
public class RequiredFieldValidator : ValidatorBase
|
||||
{
|
||||
#region Events
|
||||
/// <summary>
|
||||
/// Occurs when controls value needs to be evaluated to check whether it is empty. You can use this event to perform custom evaluation.
|
||||
/// </summary>
|
||||
public event EvaluateIsEmptyEventHandler EvaluateIsEmpty;
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the RequiredFieldValidator class.
|
||||
/// </summary>
|
||||
public RequiredFieldValidator()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the RequiredFieldValidator class.
|
||||
/// </summary>
|
||||
public RequiredFieldValidator(string errorMessage)
|
||||
{
|
||||
this.ErrorMessage = errorMessage;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the RequiredFieldValidator class.
|
||||
/// </summary>
|
||||
public RequiredFieldValidator(string errorMessage, string optionalValidationGroup)
|
||||
{
|
||||
this.ErrorMessage = errorMessage;
|
||||
this.OptionalValidationGroup = optionalValidationGroup;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Implementation
|
||||
public override bool Validate(System.Windows.Forms.Control input)
|
||||
{
|
||||
object value = GetControlValue(input);
|
||||
this.LastValidationResult = !IsEmpty(input, value);
|
||||
return LastValidationResult;
|
||||
}
|
||||
|
||||
protected virtual bool IsEmpty(System.Windows.Forms.Control input, object value)
|
||||
{
|
||||
EvaluateIsEmptyEventArgs args = new EvaluateIsEmptyEventArgs(input, value, this);
|
||||
OnEvaluateIsEmpty(args);
|
||||
if (args.IsEmptySet) return args.IsEmpty;
|
||||
|
||||
if (value == null) return true;
|
||||
if (value is string)
|
||||
{
|
||||
if (_IsEmptyStringValid)
|
||||
return value == null;
|
||||
else
|
||||
return string.IsNullOrEmpty((string)value);
|
||||
}
|
||||
else if ((input is ComboBox || input is DevComponents.DotNetBar.Controls.ComboTree) && value is int)
|
||||
return ((int)value) < 0;
|
||||
|
||||
return false;
|
||||
}
|
||||
/// <summary>
|
||||
/// Raises EvaluateIsEmpty event.
|
||||
/// </summary>
|
||||
/// <param name="args">Event Arguments</param>
|
||||
protected virtual void OnEvaluateIsEmpty(EvaluateIsEmptyEventArgs args)
|
||||
{
|
||||
EvaluateIsEmptyEventHandler h = EvaluateIsEmpty;
|
||||
if (h != null) h(this, args);
|
||||
}
|
||||
|
||||
private bool _IsEmptyStringValid = false;
|
||||
/// <summary>
|
||||
/// Indicates whether empty string of zero length is considered valid input.
|
||||
/// </summary>
|
||||
[DefaultValue(false), Category("Behavior"), Description("Indicates whether empty string of zero length is considered valid input.")]
|
||||
public bool IsEmptyStringValid
|
||||
{
|
||||
get { return _IsEmptyStringValid; }
|
||||
set
|
||||
{
|
||||
_IsEmptyStringValid = value;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
#region EvaluateIsEmptyEventArgs
|
||||
public class EvaluateIsEmptyEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the ValidatorGetValueEventArgs class.
|
||||
/// </summary>
|
||||
/// <param name="control"></param>
|
||||
/// <param name="validator"></param>
|
||||
public EvaluateIsEmptyEventArgs(Control control, object value, ValidatorBase validator)
|
||||
{
|
||||
Control = control;
|
||||
Validator = validator;
|
||||
Value = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets Control to retrieve value for.
|
||||
/// </summary>
|
||||
public readonly object Control;
|
||||
/// <summary>
|
||||
/// Gets the Value to evaluate.
|
||||
/// </summary>
|
||||
public readonly object Value;
|
||||
|
||||
/// <summary>
|
||||
/// Gets validator that is requesting value.
|
||||
/// </summary>
|
||||
public readonly ValidatorBase Validator;
|
||||
|
||||
private bool _IsEmpty;
|
||||
/// <summary>
|
||||
/// Gets or sets the value that will be used by validator.
|
||||
/// </summary>
|
||||
public bool IsEmpty
|
||||
{
|
||||
get { return _IsEmpty; }
|
||||
set
|
||||
{
|
||||
_IsEmpty = value;
|
||||
IsEmptySet = true;
|
||||
}
|
||||
}
|
||||
internal bool IsEmptySet = false;
|
||||
/// <summary>
|
||||
/// Resets the Value set and indicates that validator will internally retrieve value for the control.
|
||||
/// </summary>
|
||||
public void ResetIsEmpty()
|
||||
{
|
||||
_IsEmpty = false;
|
||||
IsEmptySet = false;
|
||||
}
|
||||
}
|
||||
public delegate void EvaluateIsEmptyEventHandler(object sender, EvaluateIsEmptyEventArgs ea);
|
||||
#endregion
|
||||
}
|
||||
#endif
|
789
PROMS/DotNetBar Source Code/Validator/SuperValidator.cs
Normal file
789
PROMS/DotNetBar Source Code/Validator/SuperValidator.cs
Normal file
@@ -0,0 +1,789 @@
|
||||
#if FRAMEWORK20
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.ComponentModel;
|
||||
using System.Drawing;
|
||||
using System.Windows.Forms;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.Design;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace DevComponents.DotNetBar.Validator
|
||||
{
|
||||
[ToolboxBitmap(typeof(SuperTooltip), "Validator.SuperValidator.ico"), ToolboxItem(true), ProvideProperty("Validator1", typeof(Control)),
|
||||
ProvideProperty("Validator2", typeof(Control)), ProvideProperty("Validator3", typeof(Control)),
|
||||
System.Runtime.InteropServices.ComVisible(false), Designer("DevComponents.DotNetBar.Design.SuperValidatorDesigner, DevComponents.DotNetBar.Design, Version=14.1.0.37, Culture=neutral, PublicKeyToken=90f470f34c89ccaf")]
|
||||
public class SuperValidator : Component, IExtenderProvider
|
||||
{
|
||||
#region Private Variables
|
||||
private Dictionary<Control, ValidatorBase> _Validators1 = new Dictionary<Control, ValidatorBase>();
|
||||
private Dictionary<Control, ValidatorBase> _Validators2 = new Dictionary<Control, ValidatorBase>();
|
||||
private Dictionary<Control, ValidatorBase> _Validators3 = new Dictionary<Control, ValidatorBase>();
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
/// <summary>
|
||||
/// Occurs when validator retrieves the value for the control. It allows you to return value for the controls validator does not recognize.
|
||||
/// </summary>
|
||||
public event ValidatorGetValueEventHandler GetValue;
|
||||
/// <summary>
|
||||
/// Occurs when CustomValidator needs to validate the control value.
|
||||
/// </summary>
|
||||
public event ValidateValueEventHandler CustomValidatorValidateValue;
|
||||
#endregion
|
||||
|
||||
#region Implementation
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
ClearAllValidators();
|
||||
ContainerControl = null;
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
private void ClearAllValidators()
|
||||
{
|
||||
ClearValidator(_Validators1);
|
||||
ClearValidator(_Validators2);
|
||||
ClearValidator(_Validators3);
|
||||
}
|
||||
|
||||
private void ClearValidator(Dictionary<Control, ValidatorBase> validators)
|
||||
{
|
||||
if (validators == null || validators.Count == 0) return;
|
||||
|
||||
foreach (KeyValuePair<Control, ValidatorBase> item in validators)
|
||||
{
|
||||
item.Key.Validating -= ControlValidating;
|
||||
}
|
||||
|
||||
validators.Clear();
|
||||
}
|
||||
|
||||
|
||||
internal void InvokeCustomValidatorValidateValue(CustomValidator validator, ValidateValueEventArgs e)
|
||||
{
|
||||
OnCustomValidatorValidateValue(validator, e);
|
||||
}
|
||||
/// <summary>
|
||||
/// Raises the CustomValidatorValidateValue event.
|
||||
/// </summary>
|
||||
/// <param name="validator">Validator that needs validation.</param>
|
||||
/// <param name="e">Control to validate.</param>
|
||||
protected virtual void OnCustomValidatorValidateValue(CustomValidator validator, ValidateValueEventArgs e)
|
||||
{
|
||||
ValidateValueEventHandler h = CustomValidatorValidateValue;
|
||||
if (h != null) h(validator, e);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves first level Validator for given control or return null if control does not have validator associated with it.
|
||||
/// </summary>
|
||||
[Editor("DevComponents.DotNetBar.Design.ValidatorEditor, DevComponents.DotNetBar.Design, Version=14.1.0.37, Culture=neutral, PublicKeyToken=90f470f34c89ccaf", typeof(System.Drawing.Design.UITypeEditor)), DefaultValue(null), Localizable(true)]
|
||||
public ValidatorBase GetValidator1(Control c)
|
||||
{
|
||||
if (_Validators1.ContainsKey(c))
|
||||
{
|
||||
ValidatorBase info = _Validators1[c];
|
||||
return info;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/// <summary>
|
||||
/// Associates first level Validator with given control.
|
||||
/// </summary>
|
||||
/// <param name="c">Reference to supported control.</param>
|
||||
/// <param name="info">Instance of validator class. If null is passed existing Validator is detached from the given control.</param>
|
||||
public void SetValidator1(Control c, ValidatorBase info)
|
||||
{
|
||||
if (_Validators1.ContainsKey(c))
|
||||
{
|
||||
if (info == null)
|
||||
{
|
||||
ValidatorBase validator = _Validators1[c];
|
||||
this.RemoveValidator(_Validators1, c);
|
||||
|
||||
DestroyValidator(validator);
|
||||
}
|
||||
else
|
||||
{
|
||||
ValidatorBase validator = _Validators1[c];
|
||||
if (validator != info)
|
||||
DestroyValidator(validator);
|
||||
_Validators1[c] = info;
|
||||
}
|
||||
}
|
||||
else if (info != null)
|
||||
{
|
||||
this.AddValidator(_Validators1, c, info);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves second level Validator for given control or return null if control does not have validator associated with it.
|
||||
/// </summary>
|
||||
[Editor("DevComponents.DotNetBar.Design.ValidatorEditor, DevComponents.DotNetBar.Design, Version=14.1.0.37, Culture=neutral, PublicKeyToken=90f470f34c89ccaf", typeof(System.Drawing.Design.UITypeEditor)), DefaultValue(null), Localizable(true)]
|
||||
public ValidatorBase GetValidator2(Control c)
|
||||
{
|
||||
if (_Validators2.ContainsKey(c))
|
||||
{
|
||||
ValidatorBase info = _Validators2[c];
|
||||
return info;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/// <summary>
|
||||
/// Associates second level Validator with given control.
|
||||
/// </summary>
|
||||
/// <param name="c">Reference to supported control.</param>
|
||||
/// <param name="info">Instance of validator class. If null is passed existing Validator is detached from the given control.</param>
|
||||
public void SetValidator2(Control c, ValidatorBase info)
|
||||
{
|
||||
if (_Validators2.ContainsKey(c))
|
||||
{
|
||||
if (info == null)
|
||||
{
|
||||
ValidatorBase validator = _Validators2[c];
|
||||
this.RemoveValidator(_Validators2, c);
|
||||
|
||||
DestroyValidator(validator);
|
||||
}
|
||||
else
|
||||
{
|
||||
ValidatorBase validator = _Validators2[c];
|
||||
if (validator != info)
|
||||
DestroyValidator(validator);
|
||||
_Validators2[c] = info;
|
||||
}
|
||||
}
|
||||
else if (info != null)
|
||||
{
|
||||
this.AddValidator(_Validators2, c, info);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves third level Validator for given control or return null if control does not have validator associated with it.
|
||||
/// </summary>
|
||||
[Editor("DevComponents.DotNetBar.Design.ValidatorEditor, DevComponents.DotNetBar.Design, Version=14.1.0.37, Culture=neutral, PublicKeyToken=90f470f34c89ccaf", typeof(System.Drawing.Design.UITypeEditor)), DefaultValue(null), Localizable(true)]
|
||||
public ValidatorBase GetValidator3(Control c)
|
||||
{
|
||||
if (_Validators3.ContainsKey(c))
|
||||
{
|
||||
ValidatorBase info = _Validators3[c];
|
||||
return info;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/// <summary>
|
||||
/// Associates third level Validator with given control.
|
||||
/// </summary>
|
||||
/// <param name="c">Reference to supported control.</param>
|
||||
/// <param name="info">Instance of validator class. If null is passed existing Validator is detached from the given control.</param>
|
||||
public void SetValidator3(Control c, ValidatorBase info)
|
||||
{
|
||||
if (_Validators3.ContainsKey(c))
|
||||
{
|
||||
if (info == null)
|
||||
{
|
||||
ValidatorBase validator = _Validators3[c];
|
||||
this.RemoveValidator(_Validators3, c);
|
||||
|
||||
DestroyValidator(validator);
|
||||
}
|
||||
else
|
||||
{
|
||||
ValidatorBase validator = _Validators3[c];
|
||||
if (validator != info)
|
||||
DestroyValidator(validator);
|
||||
_Validators3[c] = info;
|
||||
}
|
||||
}
|
||||
else if (info != null)
|
||||
{
|
||||
this.AddValidator(_Validators3, c, info);
|
||||
}
|
||||
}
|
||||
|
||||
private void DestroyValidator(ValidatorBase validator)
|
||||
{
|
||||
if (this.Site != null && this.DesignMode)
|
||||
{
|
||||
IDesignerHost dh = this.Site.GetService(typeof(IDesignerHost)) as IDesignerHost;
|
||||
if (dh != null)
|
||||
{
|
||||
dh.DestroyComponent(validator);
|
||||
}
|
||||
}
|
||||
}
|
||||
private void RemoveValidator(Dictionary<Control, ValidatorBase> validatorCollection, Control control)
|
||||
{
|
||||
ValidatorBase info = validatorCollection[control];
|
||||
info.SuperValidator = null;
|
||||
validatorCollection.Remove(control);
|
||||
control.Validating -= ControlValidating;
|
||||
}
|
||||
private void AddValidator(Dictionary<Control, ValidatorBase> validatorCollection, Control control, ValidatorBase info)
|
||||
{
|
||||
info.SuperValidator = this;
|
||||
validatorCollection.Add(control, info);
|
||||
control.Validating += ControlValidating;
|
||||
}
|
||||
|
||||
private void ControlValidating(object sender, CancelEventArgs e)
|
||||
{
|
||||
if (!this.Enabled)
|
||||
return;
|
||||
if (_ValidationType == eValidationType.Manual)
|
||||
return;
|
||||
if ((_ValidationType & eValidationType.ValidatingEventPerControl) != eValidationType.ValidatingEventPerControl)
|
||||
return;
|
||||
|
||||
Control control = sender as Control;
|
||||
if (control == null) return;
|
||||
|
||||
ValidatorBase validator = null;
|
||||
bool isValid = true;
|
||||
if (_Validators1.TryGetValue(control, out validator))
|
||||
{
|
||||
bool cancelValidating = false;
|
||||
isValid = ValidateSingleControl(validator, control, out cancelValidating);
|
||||
if (!isValid) AddToLastFailedValidations(validator, control);
|
||||
e.Cancel = cancelValidating;
|
||||
}
|
||||
|
||||
if (isValid && _Validators2.TryGetValue(control, out validator))
|
||||
{
|
||||
bool cancelValidating = false;
|
||||
isValid = ValidateSingleControl(validator, control, out cancelValidating);
|
||||
if (!isValid) AddToLastFailedValidations(validator, control);
|
||||
e.Cancel = cancelValidating;
|
||||
}
|
||||
|
||||
if (isValid && _Validators3.TryGetValue(control, out validator))
|
||||
{
|
||||
bool cancelValidating = false;
|
||||
isValid = ValidateSingleControl(validator, control, out cancelValidating);
|
||||
if (!isValid) AddToLastFailedValidations(validator, control);
|
||||
e.Cancel = cancelValidating;
|
||||
}
|
||||
}
|
||||
private void AddToLastFailedValidations(ValidatorBase validator, Control control)
|
||||
{
|
||||
foreach (ValidatorControlPair item in _LastFailedValidations)
|
||||
{
|
||||
if (item.Control == control && item.Validator == validator) return;
|
||||
}
|
||||
_LastFailedValidations.Add(new ValidatorControlPair(validator, control));
|
||||
}
|
||||
|
||||
private bool ValidateSingleControl(ValidatorBase validator, Control control, out bool cancelValidating)
|
||||
{
|
||||
cancelValidating = false;
|
||||
if (!validator.Enabled) return true;
|
||||
|
||||
bool result = true;
|
||||
bool validateControl = true;
|
||||
if (!string.IsNullOrEmpty(validator.OptionalValidationGroup))
|
||||
{
|
||||
List<ValidatorControlPair> optionalValidators = GetOptionalValidators(validator.OptionalValidationGroup);
|
||||
if (optionalValidators.Count > 1)
|
||||
{
|
||||
result = false;
|
||||
foreach (ValidatorControlPair pair in optionalValidators)
|
||||
{
|
||||
result |= pair.Validator.Validate(pair.Control);
|
||||
}
|
||||
|
||||
#if !TRIAL
|
||||
if (NativeFunctions.keyValidated2 != 266)
|
||||
result = false;
|
||||
#endif
|
||||
|
||||
if (!result)
|
||||
{
|
||||
foreach (ValidatorControlPair pair in optionalValidators)
|
||||
{
|
||||
if (pair.Validator.DisplayError)
|
||||
SetError(pair.Validator, pair.Control);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (ValidatorControlPair pair in optionalValidators)
|
||||
{
|
||||
ClearError(pair.Validator, pair.Control);
|
||||
}
|
||||
}
|
||||
validateControl = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (validateControl)
|
||||
{
|
||||
result = validator.Validate(control);
|
||||
|
||||
#if !TRIAL
|
||||
if (NativeFunctions.keyValidated2 != 266)
|
||||
result = false;
|
||||
#endif
|
||||
|
||||
if (!result)
|
||||
{
|
||||
if (validator.DisplayError)
|
||||
SetError(validator, control);
|
||||
cancelValidating = _CancelValidatingOnControl;
|
||||
}
|
||||
else
|
||||
{
|
||||
ClearError(validator, control);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<ValidatorControlPair> GetOptionalValidators(string optionalValidationGroup)
|
||||
{
|
||||
List<ValidatorControlPair> list = new List<ValidatorControlPair>();
|
||||
|
||||
FindOptionalValidators(_Validators1, optionalValidationGroup, list);
|
||||
FindOptionalValidators(_Validators2, optionalValidationGroup, list);
|
||||
FindOptionalValidators(_Validators3, optionalValidationGroup, list);
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
private void FindOptionalValidators(Dictionary<Control, ValidatorBase> validators, string optionalValidationGroup, List<ValidatorControlPair> list)
|
||||
{
|
||||
foreach (KeyValuePair<Control, ValidatorBase> item in validators)
|
||||
{
|
||||
if (item.Value.OptionalValidationGroup.Equals(optionalValidationGroup, StringComparison.CurrentCultureIgnoreCase))
|
||||
{
|
||||
list.Add(new ValidatorControlPair(item.Value, item.Key));
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Raises the GetValue event.
|
||||
/// </summary>
|
||||
/// <param name="e">Event arguments.</param>
|
||||
protected virtual void OnGetValue(ValidatorGetValueEventArgs e)
|
||||
{
|
||||
ValidatorGetValueEventHandler handler = GetValue;
|
||||
if (handler != null) handler(this, e);
|
||||
}
|
||||
|
||||
internal void InvokeGetValue(ValidatorGetValueEventArgs e)
|
||||
{
|
||||
OnGetValue(e);
|
||||
}
|
||||
|
||||
private bool _SteppedValidation = false;
|
||||
/// <summary>
|
||||
/// Gets or sets whether validation is performed in steps meaning that if first level validation fails, second level is not validated
|
||||
/// until first level validation succeeds. Default value is false which means that all validation levels are validated even if first level fails.
|
||||
/// </summary>
|
||||
[DefaultValue(false), Category("Behavior"), Description("Indicates whether validation is performed in steps meaning that if first level validation fails, second level is not validated until first level validation succeeds")]
|
||||
public bool SteppedValidation
|
||||
{
|
||||
get { return _SteppedValidation; }
|
||||
set
|
||||
{
|
||||
_SteppedValidation = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs validation on all validators. It also uses error provider to display failed validations if validator has that enabled.
|
||||
/// </summary>
|
||||
/// <returns>Returns true if all validations succeeded or false if at least one validation has failed.</returns>
|
||||
public bool Validate()
|
||||
{
|
||||
bool validated = true;
|
||||
|
||||
Dictionary<string, bool> table = new Dictionary<string, bool>();
|
||||
_LastFailedValidations.Clear();
|
||||
|
||||
validated &= ValidateSingleTable(_Validators1, table);
|
||||
if (!validated && _SteppedValidation) return validated;
|
||||
validated &= ValidateSingleTable(_Validators2, table);
|
||||
if (!validated && _SteppedValidation) return validated;
|
||||
validated &= ValidateSingleTable(_Validators3, table);
|
||||
|
||||
return validated;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validate single control. Note that control must have validator assigned to it. This method will change LastFailedValidationResults collection.
|
||||
/// </summary>
|
||||
/// <param name="controlToValidate">Control to validate.</param>
|
||||
/// <returns>returns true if validation succeeds or false if it fails</returns>
|
||||
public bool Validate(Control controlToValidate)
|
||||
{
|
||||
return Validate(controlToValidate, true);
|
||||
}
|
||||
/// <summary>
|
||||
/// Validate single control. Note that control must have validator assigned to it. This method will change LastFailedValidationResults collection.
|
||||
/// </summary>
|
||||
/// <param name="controlToValidate">Control to validate.</param>
|
||||
/// <param name="clearLastFailedValidations">Indicates whether to clear the last set of failed validations by calling ClearFailedValidations() method.</param>
|
||||
/// <returns>returns true if validation succeeds or false if it fails</returns>
|
||||
public bool Validate(Control controlToValidate, bool clearLastFailedValidations)
|
||||
{
|
||||
bool result = true;
|
||||
bool cancelValidating = false;
|
||||
if (clearLastFailedValidations)
|
||||
ClearFailedValidations();
|
||||
|
||||
ValidatorBase validator = null;
|
||||
if (_Validators1.ContainsKey(controlToValidate))
|
||||
{
|
||||
validator = _Validators1[controlToValidate];
|
||||
bool b = ValidateSingleControl(validator, controlToValidate, out cancelValidating);
|
||||
if (!b) _LastFailedValidations.Add(new ValidatorControlPair(validator, controlToValidate));
|
||||
result &= b;
|
||||
}
|
||||
if (!result && _SteppedValidation) return result;
|
||||
|
||||
if (_Validators2.ContainsKey(controlToValidate))
|
||||
{
|
||||
validator = _Validators2[controlToValidate];
|
||||
bool b = ValidateSingleControl(validator, controlToValidate, out cancelValidating);
|
||||
if (!b) _LastFailedValidations.Add(new ValidatorControlPair(validator, controlToValidate));
|
||||
result &= b;
|
||||
}
|
||||
if (!result && _SteppedValidation) return result;
|
||||
|
||||
if (_Validators3.ContainsKey(controlToValidate))
|
||||
{
|
||||
validator = _Validators3[controlToValidate];
|
||||
bool b = ValidateSingleControl(validator, controlToValidate, out cancelValidating);
|
||||
if (!b) _LastFailedValidations.Add(new ValidatorControlPair(validator, controlToValidate));
|
||||
result &= b;
|
||||
}
|
||||
if (!result && _SteppedValidation) return result;
|
||||
|
||||
return result;
|
||||
}
|
||||
private List<ValidatorControlPair> _LastFailedValidations = new List<ValidatorControlPair>();
|
||||
/// <summary>
|
||||
/// Gets the readonly collection that returns failed validations that were result of last Validate method call.
|
||||
/// </summary>
|
||||
[Browsable(false)]
|
||||
public ReadOnlyCollection<ValidatorControlPair> LastFailedValidationResults
|
||||
{
|
||||
get
|
||||
{
|
||||
return new ReadOnlyCollection<ValidatorControlPair>(_LastFailedValidations);
|
||||
}
|
||||
}
|
||||
|
||||
private bool ValidateSingleTable(Dictionary<Control, ValidatorBase> validators, Dictionary<string, bool> table)
|
||||
{
|
||||
bool validated = true;
|
||||
foreach (KeyValuePair<Control, ValidatorBase> item in validators)
|
||||
{
|
||||
// If control has already failed validation do not validate with second/third level validator again
|
||||
if (ControlFailedValidation(item.Key)) continue;
|
||||
string optionalValidationGroup = item.Value.OptionalValidationGroup;
|
||||
if (!string.IsNullOrEmpty(optionalValidationGroup))
|
||||
{
|
||||
if (table.ContainsKey(optionalValidationGroup))
|
||||
continue;
|
||||
else
|
||||
table.Add(optionalValidationGroup, true);
|
||||
}
|
||||
|
||||
bool cancelValidatingEvent = false;
|
||||
bool b = ValidateSingleControl(item.Value, item.Key, out cancelValidatingEvent);
|
||||
if (!b) _LastFailedValidations.Add(new ValidatorControlPair(item.Value, item.Key));
|
||||
validated &= b;
|
||||
}
|
||||
|
||||
return validated;
|
||||
}
|
||||
|
||||
private bool ControlFailedValidation(Control control)
|
||||
{
|
||||
foreach (ValidatorControlPair item in _LastFailedValidations)
|
||||
{
|
||||
if (item.Control == control) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private void SetError(ValidatorBase validator, Control controlToValidate)
|
||||
{
|
||||
List<IErrorProvider> errorProviders = GetErrorProviders();
|
||||
if (errorProviders == null || errorProviders.Count == 0) return;
|
||||
|
||||
foreach (IErrorProvider errorProvider in errorProviders)
|
||||
{
|
||||
#if !TRIAL
|
||||
if (NativeFunctions.keyValidated2 != 266)
|
||||
{
|
||||
errorProvider.SetError(controlToValidate, "Invalid license key for SuperValidator control. Control is disabled.");
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
if (errorProvider is Highlighter)
|
||||
((Highlighter)errorProvider).SetHighlightColor(controlToValidate, validator.HighlightColor);
|
||||
else
|
||||
errorProvider.SetError(controlToValidate, validator.ErrorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes all visual markers from failed validations that were placed on the controls.
|
||||
/// </summary>
|
||||
public void ClearFailedValidations()
|
||||
{
|
||||
foreach (ValidatorControlPair item in _LastFailedValidations)
|
||||
{
|
||||
ClearError(item.Validator, item.Control);
|
||||
}
|
||||
_LastFailedValidations.Clear();
|
||||
}
|
||||
|
||||
private void ClearError(ValidatorBase validator, Control controlToValidate)
|
||||
{
|
||||
List<IErrorProvider> errorProviders = GetErrorProviders();
|
||||
if (errorProviders == null || errorProviders.Count == 0) return;
|
||||
|
||||
foreach (IErrorProvider errorProvider in errorProviders)
|
||||
{
|
||||
errorProvider.ClearError(controlToValidate);
|
||||
}
|
||||
}
|
||||
|
||||
private int _DefaultErrorProviderHighlighterIconPadding = 2;
|
||||
/// <summary>
|
||||
/// Indicates default ErrorProvider IconPadding when Highlighter is used with SuperValidator. Setting this property to 0 will allow you to set IconPadding for each control manually.
|
||||
/// </summary>
|
||||
[DefaultValue(2), Category("Behavior"), Description("Indicates default ErrorProvider IconPadding when Highlighter is used with SuperValidator. Setting this property to 0 will allow you to set IconPadding for each control manually.")]
|
||||
public int DefaultErrorProviderHighlighterIconPadding
|
||||
{
|
||||
get { return _DefaultErrorProviderHighlighterIconPadding; }
|
||||
set
|
||||
{
|
||||
_DefaultErrorProviderHighlighterIconPadding = value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private List<IErrorProvider> GetErrorProviders()
|
||||
{
|
||||
List<IErrorProvider> providers = new List<IErrorProvider>();
|
||||
if (_ErrorProvider != null)
|
||||
providers.Add(new ErrorProviderWrapper(_ErrorProvider, (_Highlighter != null ? _DefaultErrorProviderHighlighterIconPadding : 0)));
|
||||
if (_CustomErrorProvider != null)
|
||||
providers.Add(_CustomErrorProvider);
|
||||
if (_Highlighter != null)
|
||||
providers.Add(_Highlighter);
|
||||
|
||||
return providers;
|
||||
}
|
||||
|
||||
private ErrorProvider _ErrorProvider;
|
||||
/// <summary>
|
||||
/// Gets or sets the error provider that is used by the validator to report validation errors.
|
||||
/// </summary>
|
||||
[DefaultValue(null), Category("Behavior"), Description("Indicates error provider that is used by the validator to report validation errors.")]
|
||||
public ErrorProvider ErrorProvider
|
||||
{
|
||||
get { return _ErrorProvider; }
|
||||
set { _ErrorProvider = value; }
|
||||
}
|
||||
|
||||
private IErrorProvider _CustomErrorProvider = null;
|
||||
/// <summary>
|
||||
/// Gets or sets the custom error provider that is used by validator to report errors. You can provide your own error validators by implementing IErrorProvider interface.
|
||||
/// </summary>
|
||||
[DefaultValue(null), Browsable(false), Category("Behavior"), Description("Indicates custom error provider that is used by validator to report errors. You can provide your own error validators by implementing IErrorProvider interface.")]
|
||||
public IErrorProvider CustomErrorProvider
|
||||
{
|
||||
get { return _CustomErrorProvider; }
|
||||
set { _CustomErrorProvider = value; }
|
||||
}
|
||||
|
||||
private Highlighter _Highlighter;
|
||||
/// <summary>
|
||||
/// Gets or sets the Highlighter component that is used to highlight validation errors.
|
||||
/// </summary>
|
||||
[DefaultValue(null), Category("Appearance"), Description("Highlighter component that is used to highlight validation errors.")]
|
||||
public Highlighter Highlighter
|
||||
{
|
||||
get { return _Highlighter; }
|
||||
set { _Highlighter = value; }
|
||||
}
|
||||
|
||||
private eValidationType _ValidationType = eValidationType.Manual;
|
||||
/// <summary>
|
||||
/// Gets or sets the validation type performed by the control. Default value is Manual.
|
||||
/// </summary>
|
||||
[DefaultValue(eValidationType.Manual), Category("Behavior"), Description("Indicates validation type performed by the control.")]
|
||||
public eValidationType ValidationType
|
||||
{
|
||||
get { return _ValidationType; }
|
||||
set { _ValidationType = value; }
|
||||
}
|
||||
|
||||
private Control _ContainerControl = null;
|
||||
/// <summary>
|
||||
/// Gets or sets the container control validator is bound to. The container control must be set for the ValidationType ValidatingEventOnContainer.
|
||||
/// When ContainerControl is Form the validator handles the Closing event of the form to perform the validation and cancel the form closing.
|
||||
/// You can disable that by setting either Enabled=false or Form.CausesValidation=false.
|
||||
/// </summary>
|
||||
[DefaultValue(null), Description("Indicates container control validator is bound to. Should be set to parent form."), Category("Behavior")]
|
||||
public Control ContainerControl
|
||||
{
|
||||
get
|
||||
{
|
||||
return _ContainerControl;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (_ContainerControl != value)
|
||||
{
|
||||
if (_ContainerControl != null)
|
||||
{
|
||||
if (_ContainerControl is Form)
|
||||
((Form)_ContainerControl).FormClosing -= ContainerFormClosing;
|
||||
else
|
||||
_ContainerControl.Validating -= ContainerValidating;
|
||||
}
|
||||
|
||||
_ContainerControl = value;
|
||||
|
||||
if (_ContainerControl != null)
|
||||
{
|
||||
if (_ContainerControl is Form)
|
||||
((Form)_ContainerControl).FormClosing += ContainerFormClosing;
|
||||
else
|
||||
_ContainerControl.Validating += ContainerValidating;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private void ContainerFormClosing(object sender, FormClosingEventArgs e)
|
||||
{
|
||||
if (!this.Enabled || (_ValidationType & eValidationType.ValidatingEventOnContainer) != eValidationType.ValidatingEventOnContainer || !((Form)sender).CausesValidation)
|
||||
return;
|
||||
|
||||
e.Cancel = !Validate();
|
||||
}
|
||||
private void ContainerValidating(object sender, CancelEventArgs e)
|
||||
{
|
||||
if (!this.Enabled) return;
|
||||
|
||||
if (_ValidationType == eValidationType.ValidatingEventOnContainer)
|
||||
e.Cancel = !Validate();
|
||||
}
|
||||
|
||||
private bool _Enabled = true;
|
||||
/// <summary>
|
||||
/// Gets or sets whether validation is performed. Default value is true.
|
||||
/// </summary>
|
||||
[DefaultValue(true), Category("Behavior"), Description("Indicates whether validation is performed.")]
|
||||
public bool Enabled
|
||||
{
|
||||
get { return _Enabled; }
|
||||
set
|
||||
{
|
||||
_Enabled = value;
|
||||
}
|
||||
}
|
||||
|
||||
private bool _CancelValidatingOnControl = true;
|
||||
/// <summary>
|
||||
/// Gets or sets whether Cancel argument in Validating event for validation type ValidatingEventPerControl is set to true when validation fails.
|
||||
/// </summary>
|
||||
[DefaultValue(true), Category("Behavior"), Description("Indicates whether Cancel argument in Validating event for validation type ValidatingEventPerControl is set to true when validation fails.")]
|
||||
public bool CancelValidatingOnControl
|
||||
{
|
||||
get { return _CancelValidatingOnControl; }
|
||||
set
|
||||
{
|
||||
_CancelValidatingOnControl = value;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IExtenderProvider Members
|
||||
|
||||
public bool CanExtend(object extendee)
|
||||
{
|
||||
return extendee is Control;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Licensing
|
||||
#if !TRIAL
|
||||
private string _LicenseKey = "";
|
||||
[Browsable(false), DefaultValue("")]
|
||||
public string LicenseKey
|
||||
{
|
||||
get { return _LicenseKey; }
|
||||
set
|
||||
{
|
||||
if (NativeFunctions.ValidateLicenseKey(value))
|
||||
return;
|
||||
_LicenseKey = (!NativeFunctions.CheckLicenseKey(value) ? "9dsjkhds7" : value);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endregion
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specifies the validation type for SuperValidator control.
|
||||
/// </summary>
|
||||
[Flags()]
|
||||
public enum eValidationType
|
||||
{
|
||||
/// <summary>
|
||||
/// SuperValidator uses manual validation, i.e. you will call Validate method to perform validation.
|
||||
/// </summary>
|
||||
Manual = 1,
|
||||
/// <summary>
|
||||
/// Validation is performed per control from each controls Validating event. The Cancel is set to true on event arguments if validation fails.
|
||||
/// </summary>
|
||||
ValidatingEventPerControl = 2,
|
||||
/// <summary>
|
||||
/// Validation is performed for all controls from Validating event on container control. By default container control is Form that SuperValidator is on.
|
||||
/// </summary>
|
||||
ValidatingEventOnContainer = 4
|
||||
}
|
||||
/// <summary>
|
||||
/// Defines validator control pair used by SuperValidator control.
|
||||
/// </summary>
|
||||
public struct ValidatorControlPair
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the validator associated with the control.
|
||||
/// </summary>
|
||||
public readonly ValidatorBase Validator;
|
||||
/// <summary>
|
||||
/// Gets control reference.
|
||||
/// </summary>
|
||||
public readonly Control Control;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the ValidatorControlPair structure.
|
||||
/// </summary>
|
||||
/// <param name="validator">Validator associated with the control</param>
|
||||
/// <param name="control">Control reference</param>
|
||||
public ValidatorControlPair(ValidatorBase validator, Control control)
|
||||
{
|
||||
Validator = validator;
|
||||
Control = control;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
BIN
PROMS/DotNetBar Source Code/Validator/SuperValidator.ico
Normal file
BIN
PROMS/DotNetBar Source Code/Validator/SuperValidator.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 7.7 KiB |
269
PROMS/DotNetBar Source Code/Validator/ValidatorBase.cs
Normal file
269
PROMS/DotNetBar Source Code/Validator/ValidatorBase.cs
Normal file
@@ -0,0 +1,269 @@
|
||||
#if FRAMEWORK20
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.ComponentModel;
|
||||
using System.Windows.Forms;
|
||||
using DevComponents.Editors;
|
||||
using DevComponents.Editors.DateTimeAdv;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace DevComponents.DotNetBar.Validator
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents base validator used by SuperValidator component.
|
||||
/// </summary>
|
||||
[ToolboxItem(false), DesignTimeVisible(false)]
|
||||
public abstract class ValidatorBase : Component
|
||||
{
|
||||
#region Events
|
||||
/// <summary>
|
||||
/// Occurs when validator retrieves the value for the control. It allows you to return value for the controls validator does not recognize.
|
||||
/// </summary>
|
||||
public event ValidatorGetValueEventHandler GetValue;
|
||||
#endregion
|
||||
#region Implementation
|
||||
private string _ErrorMessage = "";
|
||||
/// <summary>
|
||||
/// Gets or sets the error message that is displayed by error provider when validation fails.
|
||||
/// </summary>
|
||||
[DefaultValue(""), Category("Appearance"), Description("Indicates error message that is displayed when validation fails."), Localizable(true)]
|
||||
public virtual string ErrorMessage
|
||||
{
|
||||
get { return _ErrorMessage; }
|
||||
set
|
||||
{
|
||||
_ErrorMessage = value;
|
||||
}
|
||||
}
|
||||
|
||||
private bool _LastValidationResult = true;
|
||||
/// <summary>
|
||||
/// Gets the last validation result returned from Validate call. True if validation was successful or false if validation failed.
|
||||
/// </summary>
|
||||
[Browsable(false)]
|
||||
public bool LastValidationResult
|
||||
{
|
||||
get { return _LastValidationResult; }
|
||||
internal set
|
||||
{
|
||||
_LastValidationResult = value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Validates the input control.
|
||||
/// </summary>
|
||||
/// <param name="input">Input control to validate.</param>
|
||||
/// <returns>true if validation is successful otherwise false</returns>
|
||||
public abstract bool Validate(Control input);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the input control value.
|
||||
/// </summary>
|
||||
/// <param name="input">Control to return value for.</param>
|
||||
/// <returns>Controls value</returns>
|
||||
protected virtual object GetControlValue(Control input)
|
||||
{
|
||||
return GetControlValue(input, _ValuePropertyName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the input control value.
|
||||
/// </summary>
|
||||
/// <param name="input">Control to return value for.</param>
|
||||
/// <returns>Controls value</returns>
|
||||
protected virtual object GetControlValue(Control input, string valuePropertyName)
|
||||
{
|
||||
ValidatorGetValueEventArgs args = new ValidatorGetValueEventArgs(input, this);
|
||||
OnGetValue(args);
|
||||
if (args.ValueSet) return args.Value;
|
||||
|
||||
if (_SuperValidator != null)
|
||||
{
|
||||
_SuperValidator.InvokeGetValue(args);
|
||||
if (args.ValueSet) return args.Value;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(valuePropertyName))
|
||||
{
|
||||
return TypeDescriptor.GetProperties(input)[valuePropertyName].GetValue(input);
|
||||
}
|
||||
|
||||
if (input is ComboBox)
|
||||
{
|
||||
ComboBox box = (ComboBox)input;
|
||||
return box.SelectedIndex;
|
||||
}
|
||||
else if (input is DevComponents.DotNetBar.Controls.ComboTree)
|
||||
{
|
||||
DevComponents.DotNetBar.Controls.ComboTree cbt = (DevComponents.DotNetBar.Controls.ComboTree)input;
|
||||
return cbt.SelectedIndex;
|
||||
}
|
||||
|
||||
if (input is DoubleInput)
|
||||
return ((DoubleInput)input).ValueObject;
|
||||
else if(input is IntegerInput)
|
||||
return ((IntegerInput)input).ValueObject;
|
||||
else if (input is DateTimeInput)
|
||||
return ((DateTimeInput)input).ValueObject;
|
||||
|
||||
object value = input.Text;
|
||||
string text = input.Text;
|
||||
long l = 0;
|
||||
int i = 0;
|
||||
double d = 0;
|
||||
if (long.TryParse(text, out l))
|
||||
value = l;
|
||||
else if (int.TryParse(text, out i))
|
||||
value = i;
|
||||
else if (double.TryParse(text, out d))
|
||||
value = d;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
private SuperValidator _SuperValidator = null;
|
||||
/// <summary>
|
||||
/// Returns SuperValidator control validator is assigned to.
|
||||
/// </summary>
|
||||
[Browsable(false)]
|
||||
public SuperValidator SuperValidator
|
||||
{
|
||||
get { return _SuperValidator; }
|
||||
internal set { _SuperValidator = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raises the GetValue event.
|
||||
/// </summary>
|
||||
/// <param name="e">Event arguments.</param>
|
||||
protected virtual void OnGetValue(ValidatorGetValueEventArgs e)
|
||||
{
|
||||
ValidatorGetValueEventHandler handler = GetValue;
|
||||
if (handler != null) handler(this, e);
|
||||
}
|
||||
|
||||
private bool _DisplayError = true;
|
||||
/// <summary>
|
||||
/// Gets or sets whether error is displayed using the error provider on SuperValidator when validation fails. Default value is true.
|
||||
/// </summary>
|
||||
[DefaultValue(true), Category("Behavior"), Description("Indicates whether error is displayed using the error provider on SuperValidator when validation fails.")]
|
||||
public bool DisplayError
|
||||
{
|
||||
get { return _DisplayError; }
|
||||
set
|
||||
{
|
||||
_DisplayError = value;
|
||||
}
|
||||
}
|
||||
|
||||
private bool _Enabled = true;
|
||||
/// <summary>
|
||||
/// Gets or sets whether validator is enabled. Default value is true.
|
||||
/// </summary>
|
||||
[DefaultValue(true), Category("Behavior"), Description("Indicates whether validator is enabled.")]
|
||||
public bool Enabled
|
||||
{
|
||||
get { return _Enabled; }
|
||||
set
|
||||
{
|
||||
_Enabled = value;
|
||||
}
|
||||
}
|
||||
|
||||
private string _OptionalValidationGroup = "";
|
||||
/// <summary>
|
||||
/// Gets or sets the group name validation belongs to. When control belongs to optional validation group the validation is considered successful when any of the controls in the group validates.
|
||||
/// </summary>
|
||||
[DefaultValue(""), Category("Behavior"), Description("Specifies group name validation belongs to. When control belongs to optional validation group the validation is considered successful when any of the controls in the group validates.")]
|
||||
public string OptionalValidationGroup
|
||||
{
|
||||
get { return _OptionalValidationGroup; }
|
||||
set
|
||||
{
|
||||
if (value == null) value = "";
|
||||
_OptionalValidationGroup = value;
|
||||
}
|
||||
}
|
||||
|
||||
private eHighlightColor _HighlightColor = eHighlightColor.None;
|
||||
/// <summary>
|
||||
/// Gets or sets the highlight color for control when validation fails if Highlighter component is used on SuperValidator. Default Value is None.
|
||||
/// </summary>
|
||||
[DefaultValue(eHighlightColor.None), Category("Appearance"), Description("Indicates highlight color for control when validation fails if Highlighter component is used on SuperValidator.")]
|
||||
public eHighlightColor HighlightColor
|
||||
{
|
||||
get { return _HighlightColor; }
|
||||
set { _HighlightColor = value; }
|
||||
}
|
||||
|
||||
private string _ValuePropertyName = "";
|
||||
/// <summary>
|
||||
/// Gets or sets the value property name for the control to validate.
|
||||
/// </summary>
|
||||
[DefaultValue(""), Category("Behavior"), Description("Indicates value property name for the control to validate.")]
|
||||
public string ValuePropertyName
|
||||
{
|
||||
get { return _ValuePropertyName; }
|
||||
set
|
||||
{
|
||||
_ValuePropertyName = value;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
#region ValidatorGetValue
|
||||
public class ValidatorGetValueEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the ValidatorGetValueEventArgs class.
|
||||
/// </summary>
|
||||
/// <param name="control"></param>
|
||||
/// <param name="validator"></param>
|
||||
public ValidatorGetValueEventArgs(Control control, ValidatorBase validator)
|
||||
{
|
||||
Control = control;
|
||||
Validator = validator;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets Control to retrieve value for.
|
||||
/// </summary>
|
||||
public readonly Control Control;
|
||||
|
||||
/// <summary>
|
||||
/// Gets validator that is requesting value.
|
||||
/// </summary>
|
||||
public readonly ValidatorBase Validator;
|
||||
|
||||
private object _Value;
|
||||
/// <summary>
|
||||
/// Gets or sets the value that will be used by validator.
|
||||
/// </summary>
|
||||
public object Value
|
||||
{
|
||||
get { return _Value; }
|
||||
set
|
||||
{
|
||||
_Value = value;
|
||||
ValueSet = true;
|
||||
}
|
||||
}
|
||||
internal bool ValueSet = false;
|
||||
/// <summary>
|
||||
/// Resets the Value set and indicates that validator will internally retrieve value for the control.
|
||||
/// </summary>
|
||||
public void ResetValue()
|
||||
{
|
||||
_Value = null;
|
||||
ValueSet = false;
|
||||
}
|
||||
}
|
||||
public delegate void ValidatorGetValueEventHandler(object sender, ValidatorGetValueEventArgs ea);
|
||||
#endregion
|
||||
}
|
||||
#endif
|
Reference in New Issue
Block a user