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

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 コントロールに指定していました。

同じロジックを使用して、NumericUpDown コントロールの代わりに TrackBar を配置できます。

TrackBar コントロールと NumericUpDown コントロールの両方を使用しているユーザー コントロールへのリンクを次に示します。このページの最後の投稿からファイルをダウンロードします (メンバーはダウンロードでき、登録は無料です):

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

于 2015-09-20T03:49:00.937 に答える