楼上两位的方法好像不是我想要的,其实,这个问题我在vb版提问了。不过没有人回答。背 http://topic.csdn.net/u/20111222/01/a3fb97be-bb0d-48f1-91b9-949170fb910c.html 如上图,menugroup 是我写的一个导航菜单,menuitem是菜单条目,这是一个简单的类,没有继承而来。menugroup有个集合属性items 是关于menuitem的集合。我现在想要在上图点击添加添加菜单的条目,但不知道怎么让那个属性网格的name自动赋值,比如赋个menuitem1,menuitem2,。。也好。 我试过了让menuitem继承control ,那name属性是可以自动赋值的。 我想只是我不知道设置哪个特性把?menuitem的代码Imports System.ComponentModel Imports System.ComponentModel.Design ''' <summary> ''' 菜单条目 ''' </summary> ''' <res></res> <Serializable()> _ Public Class MenuItem ' Inherits Control Private _Text As String Private _img As Image Private _Name As String <NonSerialized()> _ Private _Owner As MenuGroup Private _State As ControlState = ControlState.Normal Public Sub New() End Sub Public Sub New(ByVal Text As String, ByVal Name As String, ByVal Image As Image) _Text = Text _Name = Name _img = Image End Sub Public Sub New(ByVal text As String, ByVal name As String) _Text = text _Name = name End Sub #Region " 属性" Property State() As ControlState Get Return _State End Get Set(ByVal value As ControlState) If _State <> value Then _State = value Invalidate() End If End Set End Property ''' <summary> ''' 显示文本 ''' </summary> ''' <value></value> ''' <returns></returns> ''' <res></res> <DefaultValue("")> Property Text() As String Get Return _Text End Get Set(ByVal value As String) If _Text <> value Then _Text = value Invalidate() End If End Set End Property ''' <summary> ''' 条目的图标 ''' </summary> ''' <value></value> ''' <returns></returns> ''' <res></res> Property Image() As Image Get Return _img End Get Set(ByVal value As Image) _img = value Invalidate() End Set End Property ''' <summary> ''' 条目的名称或者说是条目的key ''' </summary> ''' <value></value> ''' <returns></returns> ''' <res></res> <DefaultValue("")> Property Name() As String Get Return _Name End Get Set(ByVal value As String) _Name = value End Set End Property ''' <summary> ''' 拥有本条目的所有者 ''' </summary> ''' <value></value> ''' <returns></returns> ''' <res></res> Property Owner() As MenuGroup Get Return _Owner End Get Set(ByVal value As MenuGroup) _Owner = value End Set End Property #End Region #Region "方法" ''' <summary> ''' 使控件的特定区域无效并向控件发送绘制消息(不一定会马上更新界面) ''' </summary> ''' <res></res> Public Sub Invalidate() End Sub ''' <summary> ''' 强制控件使其工作区无效并立即重绘自己和任何子控件 ''' </summary> ''' <res></res> Public Sub Refresh() End Sub #End Region End Class
加上特性试试 [Localizable(true)]
网上查了好久,总算解决了。贴上来说不定那位后来着就不用想我这么麻烦了。测试控件 Imports System.ComponentModel.Design Imports System.Windows.Forms.Design Imports System.ComponentModel<DefaultProperty("Items")> _ Public Class myControl Inherits Control Dim _items As New List(Of item) <EditorAttribute(GetType(myConCollectionEditorDgr), GetType(System.Drawing.Design.UITypeEditor))> _ ReadOnly Property Items() As List(Of item) Get Return _items End Get End Property Class item Private _name As String Private _ID As Int32 Property Name() As String Get Return _name End Get Set(ByVal value As String) _name = value End Set End Property Property ID() As Int32 Get Return _ID End Get Set(ByVal value As Int32) _ID = value End Set End Property Public Overrides Function ToString() As String Return _ID.ToString End Function End Class End Class继承集合编辑器Imports System.Windows.Forms.Design Imports System.ComponentModel.DesignPublic Class myConCollectionEditorDgr Inherits CollectionEditor Dim indexs As New List(Of Integer) Dim destroys As New List(Of Integer) Sub New(ByVal type As Type) MyBase.New(type) End Sub Protected Overrides Sub DestroyInstance(ByVal instance As Object) Dim item As myControl.item = instance indexs.Remove(item.ID) destroys.Add(item.ID) destroys.Sort() MyBase.DestroyInstance(instance) End Sub Protected Overrides Function CreateInstance(ByVal itemType As System.Type) As Object Dim item As New myControl.item Dim n As Integer = -1 For Each i As Integer In destroys If Not indexs.Contains(i) Then n = i Exit For End If Next If n = -1 Then n = indexs.Count item.ID = n indexs.Add(n) Return item 'Dim count As Integer 'Dim item As New myControl.item 'If _getItems IsNot Nothing Then ' count = _getItems.Count 'Else ' count = 0 'End If 'Dim arr(-1) As myControl.item 'Me.GetItems(arr) 'Console.WriteLine(arr.Length) 'item.Name = "item" & count 'Return item '' here we are making sure that we generate a unique column name every time 'Dim cols As Object() 'Dim strtmpcolname As String ' 'Do ' strtmpcolname = "column" & _count.ToString() ' Console.WriteLine(strtmpcolname) ' cols = Me.GetItems(strtmpcolname) ' _count += 1 'Loop While cols.Length <> 0 '' instance the column and set its ident name 'Dim col As Object = MyBase.CreateInstance(itemType) 'DirectCast(col, myControl.item).Name = strtmpcolname 'Return col ' _count = MyBase.GetItems().Length ' Return _count + 1 'If itemType Is GetType(Integer) Then 'Dim col As Object = MyBase.CreateInstance(itemType) 'DirectCast(col, myControl.item).Name = _count.ToString '_count += 1 'Return col 'Else ' Return MyBase.CreateInstance(itemType) 'End If End Function ''Protected Overrides Function GetObjectsFromInstance(ByVal instance As Object) As System.Collections.IList '' '_count = MyBase.GetObjectsFromInstance(instance).Count '' _items = MyBase.GetObjectsFromInstance(instance) '' Return _items ''End Function 'Protected Overrides Function GetItems(ByVal editValue As Object) As Object() ' ' _count = MyBase.GetItems(editValue).Length ' _getItems = MyBase.GetItems(editValue) ' Return _getItems 'End Function End Class
http://topic.csdn.net/u/20111222/01/a3fb97be-bb0d-48f1-91b9-949170fb910c.html
如上图,menugroup 是我写的一个导航菜单,menuitem是菜单条目,这是一个简单的类,没有继承而来。menugroup有个集合属性items 是关于menuitem的集合。我现在想要在上图点击添加添加菜单的条目,但不知道怎么让那个属性网格的name自动赋值,比如赋个menuitem1,menuitem2,。。也好。
我试过了让menuitem继承control ,那name属性是可以自动赋值的。
我想只是我不知道设置哪个特性把?menuitem的代码Imports System.ComponentModel
Imports System.ComponentModel.Design
''' <summary>
''' 菜单条目
''' </summary>
''' <res></res>
<Serializable()> _
Public Class MenuItem
' Inherits Control
Private _Text As String
Private _img As Image
Private _Name As String
<NonSerialized()> _
Private _Owner As MenuGroup
Private _State As ControlState = ControlState.Normal Public Sub New() End Sub
Public Sub New(ByVal Text As String, ByVal Name As String, ByVal Image As Image)
_Text = Text
_Name = Name
_img = Image
End Sub
Public Sub New(ByVal text As String, ByVal name As String)
_Text = text
_Name = name
End Sub
#Region " 属性" Property State() As ControlState
Get
Return _State
End Get
Set(ByVal value As ControlState)
If _State <> value Then
_State = value
Invalidate()
End If
End Set
End Property
''' <summary>
''' 显示文本
''' </summary>
''' <value></value>
''' <returns></returns>
''' <res></res>
<DefaultValue("")> Property Text() As String
Get
Return _Text
End Get
Set(ByVal value As String)
If _Text <> value Then
_Text = value
Invalidate()
End If
End Set
End Property
''' <summary>
''' 条目的图标
''' </summary>
''' <value></value>
''' <returns></returns>
''' <res></res>
Property Image() As Image
Get
Return _img
End Get
Set(ByVal value As Image)
_img = value
Invalidate()
End Set
End Property
''' <summary>
''' 条目的名称或者说是条目的key
''' </summary>
''' <value></value>
''' <returns></returns>
''' <res></res>
<DefaultValue("")> Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property ''' <summary>
''' 拥有本条目的所有者
''' </summary>
''' <value></value>
''' <returns></returns>
''' <res></res>
Property Owner() As MenuGroup
Get
Return _Owner
End Get
Set(ByVal value As MenuGroup)
_Owner = value
End Set
End Property
#End Region
#Region "方法" ''' <summary>
''' 使控件的特定区域无效并向控件发送绘制消息(不一定会马上更新界面)
''' </summary>
''' <res></res>
Public Sub Invalidate() End Sub
''' <summary>
''' 强制控件使其工作区无效并立即重绘自己和任何子控件
''' </summary>
''' <res></res>
Public Sub Refresh() End Sub
#End Region
End Class
[Localizable(true)]
Imports System.ComponentModel.Design
Imports System.Windows.Forms.Design
Imports System.ComponentModel<DefaultProperty("Items")> _
Public Class myControl Inherits Control Dim _items As New List(Of item) <EditorAttribute(GetType(myConCollectionEditorDgr), GetType(System.Drawing.Design.UITypeEditor))> _
ReadOnly Property Items() As List(Of item)
Get
Return _items
End Get
End Property
Class item
Private _name As String
Private _ID As Int32
Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Property ID() As Int32
Get
Return _ID
End Get
Set(ByVal value As Int32)
_ID = value
End Set
End Property
Public Overrides Function ToString() As String
Return _ID.ToString
End Function
End Class
End Class继承集合编辑器Imports System.Windows.Forms.Design
Imports System.ComponentModel.DesignPublic Class myConCollectionEditorDgr
Inherits CollectionEditor Dim indexs As New List(Of Integer)
Dim destroys As New List(Of Integer) Sub New(ByVal type As Type)
MyBase.New(type)
End Sub
Protected Overrides Sub DestroyInstance(ByVal instance As Object)
Dim item As myControl.item = instance indexs.Remove(item.ID)
destroys.Add(item.ID)
destroys.Sort()
MyBase.DestroyInstance(instance)
End Sub
Protected Overrides Function CreateInstance(ByVal itemType As System.Type) As Object Dim item As New myControl.item
Dim n As Integer = -1
For Each i As Integer In destroys
If Not indexs.Contains(i) Then
n = i
Exit For
End If
Next
If n = -1 Then n = indexs.Count
item.ID = n
indexs.Add(n)
Return item 'Dim count As Integer
'Dim item As New myControl.item 'If _getItems IsNot Nothing Then
' count = _getItems.Count
'Else
' count = 0
'End If 'Dim arr(-1) As myControl.item
'Me.GetItems(arr)
'Console.WriteLine(arr.Length)
'item.Name = "item" & count
'Return item '' here we are making sure that we generate a unique column name every time
'Dim cols As Object()
'Dim strtmpcolname As String
'
'Do
' strtmpcolname = "column" & _count.ToString()
' Console.WriteLine(strtmpcolname)
' cols = Me.GetItems(strtmpcolname) ' _count += 1
'Loop While cols.Length <> 0 '' instance the column and set its ident name
'Dim col As Object = MyBase.CreateInstance(itemType)
'DirectCast(col, myControl.item).Name = strtmpcolname 'Return col ' _count = MyBase.GetItems().Length
' Return _count + 1
'If itemType Is GetType(Integer) Then
'Dim col As Object = MyBase.CreateInstance(itemType)
'DirectCast(col, myControl.item).Name = _count.ToString
'_count += 1
'Return col
'Else
' Return MyBase.CreateInstance(itemType)
'End If
End Function
''Protected Overrides Function GetObjectsFromInstance(ByVal instance As Object) As System.Collections.IList
'' '_count = MyBase.GetObjectsFromInstance(instance).Count
'' _items = MyBase.GetObjectsFromInstance(instance)
'' Return _items
''End Function
'Protected Overrides Function GetItems(ByVal editValue As Object) As Object()
' ' _count = MyBase.GetItems(editValue).Length
' _getItems = MyBase.GetItems(editValue)
' Return _getItems
'End Function
End Class