4

如何在我的应用程序的属性网格中添加数字向上/向下控件?

4

2 回答 2

4

您需要创建一个 UI 类型编辑器,然后在其中创建向上/向下控件。我不确定是否有办法在设置中指定最小值/最大值。我对它们进行了硬编码。

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;
    }
}

将其添加到您的设置中,如下所示:

//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; } }
于 2014-09-23T10:49:35.723 回答
1

阿德里安瓦迪的回答对我来说很好。我不得不将它转换为 VB Net,因为它对我来说更容易(将下面的代码转换为 C# 应该不难)。

为了对具有不同数据类型的多个属性使用相同的 UpDownValueEditor 并让每个属性为 NumericUpDown 控件指定自己的值,这是我在假设用户一次仅更改 1 个属性的情况下所做的,因此允许动态更改NumericUpDown 值:

1) 在 UpDownValueEditor 类中声明公共共享变量:

Public Shared udControl As New NumericUpDown()
Public Shared valueType As String

2) 修改 EditValue 函数,使其仅处理属性值

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) 从属性的 Get 语句中传递所有必需的值(应使用以下示例):

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

该代码对我来说工作得很好,每个属性都将它自己的值指定给 NumericUpDown 控件。

使用相同的逻辑,可以放置 TrackBar 代替 NumericUpDown 控件。

这是一个使用 TrackBar 和 NumericUpDown 控件的用户控件的链接。从本页最后一篇文章下载文件(会员可以下载,免费注册):

http://advancedhmi.com/forum/index.php?PHPSESSID=6e4661fc9662685cf4ad61874a12fa86&topic=673.0

于 2015-09-20T03:49:00.937 回答