如何在应用程序中的属性网格中添加数字向上/向下控件?
答案 0 :(得分:3)
您需要创建一个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; } }
答案 1 :(得分: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控件指定它自己的值。
使用相同的逻辑,可以放置TrackBar而不是NumericUpDown控件。
以下是使用TrackBar和NumericUpDown控件的用户控件的链接。从本页最后一篇文章下载文件(会员可以下载并免费注册):
http://advancedhmi.com/forum/index.php?PHPSESSID=6e4661fc9662685cf4ad61874a12fa86&topic=673.0