How do I add a numeric up/down control to a property grid in my application?
-
@kmatyaszek Windows form applicationPraveen Kumar– Praveen Kumar2013-01-12 09:06:10 +00:00Commented Jan 12, 2013 at 9:06
-
@mikez i tried adding combobox to the the propertygrid,but i am unable to add numericupdown to propertygrid....Praveen Kumar– Praveen Kumar2013-01-12 09:13:20 +00:00Commented Jan 12, 2013 at 9:13
2 Answers
You need to create a UI type editor and then create the up/down control within it. I'm not sure whether there's a way you can specify min/max in the settings. I hard coded them.
public class UpDownValueEditor : UITypeEditor {
public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context) {
return UITypeEditorEditStyle.DropDown;
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) {
IWindowsFormsEditorService editorService = null;
if (provider != null) {
editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
}
if (editorService != null) {
NumericUpDown udControl = new NumericUpDown();
udControl.DecimalPlaces = 0;
udControl.Minimum = 0;
udControl.Maximum = 127;
udControl.Value = (UInt16)value;
editorService.DropDownControl(udControl);
value = (UInt16)udControl.Value;
}
return value;
}
}
Add it to your settings as follows:
//MinimumVolume
[Description("When using a sound card for MIDI output use this to adjust the minimum volume.\r\n" +
"Set this to zero for output to play back expression as it was recorded."),
DisplayName("Minimum Volume"),
Editor(typeof(UpDownValueEditor), typeof(UITypeEditor)),
Category("MIDI")]
public UInt16 MinimumVolume { get { return Settings.MinimumVolume; } set { Settings.MinimumVolume = value; } }
Comments
The answer by adrianwadey worked fine for me. I had to convert it to VB Net since it is easier for me (it shouldn't be difficult to convert the code below to C#).
In order to use this same UpDownValueEditor for multiple properties with different data types and have each property specify its own values for the NumericUpDown control, here is what I did under assumption that a user will be changing only 1 property at the time thus allow dynamic change of the NumericUpDown values:
1) Declare public shared variables within the class UpDownValueEditor:
Public Shared udControl As New NumericUpDown()
Public Shared valueType As String
2) Modify the EditValue function so it deals with property value only
Public Overrides Function EditValue(ByVal context As ITypeDescriptorContext, ByVal provider As IServiceProvider, ByVal value As Object) As Object
Dim editorService As IWindowsFormsEditorService = Nothing
If provider IsNot Nothing Then
editorService = TryCast(provider.GetService(GetType(IWindowsFormsEditorService)), IWindowsFormsEditorService)
End If
If editorService IsNot Nothing Then
udControl.Value = value
editorService.DropDownControl(udControl)
If valueType = "Single" Then value = CSng(udControl.Value)
If valueType = "Integer" Then value = CInt(udControl.Value)
End If
Return value
End Function
3) Pass all the required values from within a property's Get statement (the following should be used as an example):
Private _lineWidth As Single = 2.0F
<Browsable(True), Editor(GetType(UpDownValueEditor), GetType(UITypeEditor)), DefaultValue(2.0F)> _
Public Property RectangleLineWidth As Single
Get
UpDownValueEditor.udControl.DecimalPlaces = 0
UpDownValueEditor.udControl.Increment = 1
UpDownValueEditor.udControl.Minimum = 0
UpDownValueEditor.udControl.Maximum = 20
UpDownValueEditor.valueType = "Single"
Return Me._lineWidth
End Get
Set(ByVal value As Single)
Me._lineWidth = value
End Set
End Property
The code worked fine for me and each property was specifying it's own values to NumericUpDown control.
Using the same logic, one can place TrackBar instead of NumericUpDown control.
Here is a link to a user control that is using both TrackBar and NumericUpDown controls. Download files from the last post on this page (members can download it and registration is FREE):
http://advancedhmi.com/forum/index.php?PHPSESSID=6e4661fc9662685cf4ad61874a12fa86&topic=673.0