簡體   English   中英

如何在C#中將numericupdown控件添加到自定義屬性網格?

[英]How to add numericupdown control to custom Property grid in c#?

如何在應用程序的屬性網格中添加數字上/下控件?

您需要創建一個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; } }

adrianwadey的回答對我來說很好。 我必須將其轉換為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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM