Exemplo n.º 1
0
        private void AdvPropertyGrid1_ConvertFromStringToPropertyValue(object sender, ConvertValueEventArgs e)
        {
            var switchExpr = e.PropertyDescriptor.PropertyType;

            switch (switchExpr)
            {
            case var @case when @case == typeof(bool):
            {
                if (e.StringValue == "Yes")
                {
                    e.TypedValue = true;
                }
                else
                {
                    e.TypedValue = false;
                }

                e.IsConverted = true;
                break;
            }

            case var case1 when case1 == typeof(byte):
            {
                e.TypedValue  = Conversions.ToByte(TextValueConverter.ValueFromText(e.StringValue.Trim()));
                e.IsConverted = true;
                break;
            }

            case var case2 when case2 == typeof(sbyte):
            {
                e.TypedValue  = Conversions.ToSByte(TextValueConverter.ValueFromText(e.StringValue.Trim()));
                e.IsConverted = true;
                break;
            }

            case var case3 when case3 == typeof(short):
            {
                e.TypedValue  = Conversions.ToShort(TextValueConverter.ValueFromText(e.StringValue.Trim()));
                e.IsConverted = true;
                break;
            }

            case var case4 when case4 == typeof(ushort):
            {
                e.TypedValue  = Conversions.ToUShort(TextValueConverter.ValueFromText(e.StringValue.Trim()));
                e.IsConverted = true;
                break;
            }

            case var case5 when case5 == typeof(int):
            {
                e.TypedValue  = Conversions.ToInteger(TextValueConverter.ValueFromText(e.StringValue.Trim()));
                e.IsConverted = true;
                break;
            }

            case var case6 when case6 == typeof(uint):
            {
                e.TypedValue  = Conversions.ToUInteger(TextValueConverter.LongFromText(e.StringValue.Trim()));
                e.IsConverted = true;
                break;
            }

            case var case7 when case7 == typeof(float):
            {
                e.TypedValue  = Conversions.ToSingle(e.StringValue.Trim());
                e.IsConverted = true;
                break;
            }

            case var case8 when case8 == typeof(double):
            {
                e.TypedValue  = Conversions.ToDouble(e.StringValue.Trim());
                e.IsConverted = true;
                break;
            }

            case var case9 when case9 == typeof(decimal):
            {
                e.TypedValue  = Conversions.ToDecimal(e.StringValue.Trim());
                e.IsConverted = true;
                break;
            }
            }
        }