c# - 如何在 C# 中将 numericupdown 控件添加到自定义属性网格?

标签 c# vb.net winforms

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

最佳答案

您需要创建一个 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; } }

关于c# - 如何在 C# 中将 numericupdown 控件添加到自定义属性网格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14291291/

相关文章:

c# - 通过 WebService 发送电子邮件

c# - 在 C# 代码中解析(大)XML 的最佳方法是什么?

c# - 使用ServiceStack上传图片文件

vb.net - 在 Visual Studio 中设计时如何从表单中隐藏控件?

c# - C# 用户控件中的滚动条

c# - 如何在 C# 中排列或排序桌面图标?

c# - 当它在 splittercontainer 中时,查找相对于父窗体的控件位置

VB.NET 2012 在属性获取上设置属性

以字符串数组作为参数的 C# Invoke() 委托(delegate) (winforms)

c# - 单击按钮时在表单上运行所有验证事件