提前道歉。我一直都在努力探索命名空间,类,方法,参考,库等。
我已将下面的代码保存为项目中的类和单独文件。它基本上允许您在ContextMenus上包含Slider控件。我可以通过Designer在现有表单上包含这个新控件没问题。
Imports System.Windows.Forms.Design
<ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ContextMenuStrip), DebuggerStepThrough()>
Public Class TrackBarMenuItem
Inherits ToolStripControlHost
Public Sub New()
MyBase.New(New TrackBar())
End Sub
Public ReadOnly Property TrackbarControl() As TrackBar
Get
Return CType(Control, TrackBar)
End Get
End Property
' PROPERTIES
' Add properties, events etc. you want to expose...
Public Property Value() As Integer
Get
Return TrackbarControl.Value
End Get
Set(value As Integer)
TrackbarControl.Value = value
End Set
End Property
Public Property Minimum() As Integer
Get
Return TrackbarControl.Minimum
End Get
Set(value As Integer)
TrackbarControl.Minimum = value
End Set
End Property
Public Property Maximum() As Integer
Get
Return TrackbarControl.Maximum
End Get
Set(value As Integer)
TrackbarControl.Maximum = value
End Set
End Property
Public Property LargeChange() As Integer
Get
Return TrackbarControl.LargeChange
End Get
Set(value As Integer)
TrackbarControl.LargeChange = value
End Set
End Property
Public Property SmallChange() As Integer
Get
Return TrackbarControl.SmallChange
End Get
Set(value As Integer)
TrackbarControl.SmallChange = value
End Set
End Property
Public Property Orientation() As Integer
Get
Return TrackbarControl.Orientation
End Get
Set(value As Integer)
TrackbarControl.Orientation = value
End Set
End Property
Public Property TickFrequency() As Integer
Get
Return TrackbarControl.TickFrequency
End Get
Set(value As Integer)
TrackbarControl.TickFrequency = value
End Set
End Property
Public Property TickStyle() As Integer
Get
Return TrackbarControl.TickStyle
End Get
Set(value As Integer)
TrackbarControl.TickStyle = value
End Set
End Property
'EVENTS:
Protected Overrides Sub OnSubscribeControlEvents(ByVal c As Control)
' Call the base so the base events are connected.
MyBase.OnSubscribeControlEvents(c)
' Cast the control to a Taskbar control.
Dim trackbarcontrol As TrackBar = CType(c, TrackBar)
' Add the events:
AddHandler trackbarcontrol.ValueChanged, AddressOf HandleValueChanged
'AddHandler trackbarcontrol.Scroll, AddressOf HandleScroll
End Sub
' Slider changed event
Public Event SliderValueChanged As EventHandler
Private Sub HandleValueChanged(ByVal sender As Object, ByVal e As EventArgs)
RaiseEvent SliderValueChanged(Me, e)
End Sub
Protected Overrides Sub OnUnsubscribeControlEvents(ByVal c As Control)
' Call the base method so the basic events are unsubscribed.
MyBase.OnUnsubscribeControlEvents(c)
' Cast the control to a MonthCalendar control.
Dim monthCalendarControl As MonthCalendar = _
CType(c, MonthCalendar)
' Remove the event.
RemoveHandler TrackbarControl.ValueChanged, AddressOf HandleValueChanged
End Sub
End Class
但是,在编译完整代码之前会出现错误,如下所示:
Error 5 Type 'CPWBAdmin.TrackBarMenuItem' is not defined. C:\Users\Steve\Documents\Visual Studio 2013\Projects\CPWBAdmin\CPWBAdmin\MarqueeEditor.Designer.vb 208 34 CPWBAdmin
在设计师视图中,一个冒犯文章的例子:
Me._ImgBrightnessSlider = New CPWBAdmin.TrackBarMenuItem()
如果我将其更改为:
Me._ImgBrightnessSlider = New Global.CPWBAdmin.TrackBarMenuItem()
然后它编译并运行正常。 直到下次我编辑控件时,错误会再次出现。
阅读,可能与类名称相关&#39;匹配命名空间&#39; - 但我不知道如何确定这个类的命名空间(文件名?)。
当然这是很多专业人士的基础,所以任何业余爱好者的帮助都会受到赞赏!