/
NumericUpDownTypeEditor.cs
92 lines (77 loc) · 3.05 KB
/
NumericUpDownTypeEditor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace Free.Core.Design
{
public class NumericUpDownTypeEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
if(context==null||context.Instance==null) return base.GetEditStyle(context);
return context.PropertyDescriptor.IsReadOnly?UITypeEditorEditStyle.None:UITypeEditorEditStyle.DropDown;
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
try
{
if(context==null||context.Instance==null||provider==null) return value;
//use IWindowsFormsEditorService object to display a control in the dropdown area
IWindowsFormsEditorService frmsrv=(IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if(frmsrv==null) return value;
NumericUpDown nmr;
NumericUpDownTypeSettingsAttribute attr=(NumericUpDownTypeSettingsAttribute)context.PropertyDescriptor.Attributes[typeof(NumericUpDownTypeSettingsAttribute)];
if(attr==null)
{
// check context.PropertyDescriptor.PropertyType and make Range depending on the type
Type type=context.PropertyDescriptor.PropertyType;
decimal Min=0;
decimal Max=100;
if(type==typeof(byte)) { Min=byte.MinValue; Max=byte.MaxValue; }
else if(type==typeof(sbyte)) { Min=sbyte.MinValue; Max=sbyte.MaxValue; }
else if(type==typeof(short)) { Min=short.MinValue; Max=short.MaxValue; }
else if(type==typeof(ushort)) { Min=ushort.MinValue; Max=ushort.MaxValue; }
else if(type==typeof(int)) { Min=int.MinValue; Max=int.MaxValue; }
else if(type==typeof(uint)) { Min=uint.MinValue; Max=uint.MaxValue; }
else if(type==typeof(long)) { Min=long.MinValue; Max=long.MaxValue; }
else if(type==typeof(ulong)) { Min=ulong.MinValue; Max=ulong.MaxValue; }
else if(type==typeof(float)||type==typeof(double)||type==typeof(decimal)) { Min=decimal.MinValue; Max=decimal.MaxValue; }
else { Min=0; Max=100; }
decimal v=(decimal)Convert.ChangeType(value, typeof(decimal));
int decimalPlaces=Math.Min((int)BitConverter.GetBytes(decimal.GetBits(v)[3])[2], 10);
nmr=new NumericUpDown
{
Size=new Size(60, 120),
Minimum=Min,
Maximum=Max,
Increment=decimal.One,
DecimalPlaces=decimalPlaces,
Value=v
};
}
else
{
decimal v=attr.PutInRange(value);
int decimalPlaces=attr.DecimalPlaces;
if(decimalPlaces<0) decimalPlaces=Math.Min((int)BitConverter.GetBytes(decimal.GetBits(v)[3])[2], 10);
nmr=new NumericUpDown
{
Size=new Size(60, 120),
Minimum=attr.Min,
Maximum=attr.Max,
Increment=attr.Increment,
DecimalPlaces=decimalPlaces,
Value=v
};
}
frmsrv.DropDownControl(nmr);
context.OnComponentChanged();
return Convert.ChangeType(nmr.Value, context.PropertyDescriptor.PropertyType);
}
catch { }
return value;
}
}
}