Skip to content

Commit

Permalink
Added checking of decimals. Fixed #876
Browse files Browse the repository at this point in the history
  • Loading branch information
rappen committed Aug 3, 2023
1 parent 665fb36 commit ba69276
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 25 deletions.
1 change: 1 addition & 0 deletions FetchXmlBuilder/Controls/conditionControl.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 50 additions & 25 deletions FetchXmlBuilder/Controls/conditionControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.ServiceModel;
using System.Windows.Forms;
Expand Down Expand Up @@ -115,25 +116,10 @@ protected override ControlValidationResult ValidateControl(Control control)

if (cmbOperator.SelectedItem != null && cmbOperator.SelectedItem is OperatorItem oper && (!oper.IsMultipleValuesType || Node.Nodes.Count > 0))
{
AttributeItem attribute = null;
if (cmbAttribute.SelectedItem != null && cmbAttribute.SelectedItem is AttributeItem)
{ // Get type from condition attribute
attribute = (AttributeItem)cmbAttribute.SelectedItem;
}
var valueType = oper.ValueType;
var attribute = cmbAttribute.SelectedItem as AttributeItem; ;
var valueType = GetValueType(oper, attribute);
var attributeType = oper.AttributeType;
var value = ControlUtils.GetValueFromControl(cmbValue).Trim();
if (valueType == AttributeTypeCode.ManagedProperty)
{ // Type not defined by operator
if (attribute != null)
{ // Get type from condition attribute
valueType = attribute.Metadata.AttributeType;
}
else
{ // Default, cannot determine type
valueType = AttributeTypeCode.String;
}
}

if (attributeType != null && attribute != null && control == cmbOperator)
{
Expand Down Expand Up @@ -216,8 +202,7 @@ protected override ControlValidationResult ValidateControl(Control control)
break;

case AttributeTypeCode.DateTime:
DateTime date;
if (!DateTime.TryParse(value, out date))
if (!DateTime.TryParse(value, out DateTime _))
{
return new ControlValidationResult(ControlValidationLevel.Error, "Operator " + oper.ToString() + " requires date value");
}
Expand All @@ -229,8 +214,7 @@ protected override ControlValidationResult ValidateControl(Control control)
case AttributeTypeCode.Picklist:
case AttributeTypeCode.BigInt:
case AttributeTypeCode.EntityName:
int intvalue;
if (!int.TryParse(value, out intvalue))
if (!int.TryParse(value, out int _))
{
return new ControlValidationResult(ControlValidationLevel.Error, "Operator " + oper.ToString() + " requires whole number value");
}
Expand All @@ -239,8 +223,7 @@ protected override ControlValidationResult ValidateControl(Control control)
case AttributeTypeCode.Decimal:
case AttributeTypeCode.Double:
case AttributeTypeCode.Money:
decimal decvalue;
if (!decimal.TryParse(value, out decvalue))
if (!decimal.TryParse(value, out decimal _))
{
return new ControlValidationResult(ControlValidationLevel.Error, "Operator " + oper.ToString() + " requires decimal value");
}
Expand All @@ -250,8 +233,7 @@ protected override ControlValidationResult ValidateControl(Control control)
case AttributeTypeCode.Customer:
case AttributeTypeCode.Owner:
case AttributeTypeCode.Uniqueidentifier:
Guid guidvalue;
if (!Guid.TryParse(value, out guidvalue))
if (!Guid.TryParse(value, out Guid _))
{
return new ControlValidationResult(ControlValidationLevel.Error, "Operator " + oper.ToString() + " requires a proper guid with format: " + Guid.Empty.ToString());
}
Expand All @@ -274,6 +256,28 @@ protected override ControlValidationResult ValidateControl(Control control)
return base.ValidateControl(control);
}

private static AttributeTypeCode? GetValueType(OperatorItem oper, AttributeItem attributeitem)
{
if (oper == null)
{
return null;
}
var valueType = oper.ValueType;
if (valueType == AttributeTypeCode.ManagedProperty)
{ // Type not defined by operator
if (attributeitem != null)
{ // Get type from condition attribute
valueType = attributeitem.Metadata.AttributeType;
}
else
{ // Default, cannot determine type
valueType = AttributeTypeCode.String;
}
}

return valueType;
}

protected override Dictionary<string, string> GetAttributesCollection()
{
var result = base.GetAttributesCollection();
Expand Down Expand Up @@ -665,5 +669,26 @@ public override void Focus()
{
cmbAttribute.Focus();
}

private void cmbValue_KeyPress(object sender, KeyPressEventArgs e)
{
if (char.IsDigit(e.KeyChar))
{
return;
}
var valuetype = GetValueType(cmbOperator.SelectedItem as OperatorItem, cmbAttribute.SelectedItem as AttributeItem);
if (valuetype.IsDecimalable())
{
var sepcurrent = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator[0];
if (e.KeyChar == sepcurrent)
{
var sepinvariant = CultureInfo.InvariantCulture.NumberFormat.NumberDecimalSeparator[0];
if (sepinvariant != sepcurrent)
{
e.KeyChar = sepinvariant;
}
}
}
}
}
}
12 changes: 12 additions & 0 deletions FetchXmlBuilder/Extensions/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ internal static string AttributeValue(this XmlNode node, string key)
return string.Empty;
}

internal static bool IsDecimalable(this AttributeTypeCode? attributetype)
{
if (attributetype == null)
{
return false;
}
return
attributetype == AttributeTypeCode.Decimal ||
attributetype == AttributeTypeCode.Double ||
attributetype == AttributeTypeCode.Money;
}

public static void Move<T>(this List<T> list, T item, int newIndex)
{ // From this tip: https://stackoverflow.com/a/450250/2866704
if (item != null)
Expand Down

0 comments on commit ba69276

Please sign in to comment.