[VB.net]自定义集合错误 [英] [VB.net] Custom Collection Error
问题描述
很抱歉,当我在google等搜索时,我找不到我需要的东西
我无法收藏对于项目,当我退出设计师或调试时,我的收藏变得空白..
这是我的代码:
ExplorerBarItemData.vb
公共 类 ExplorerBarItemData
#RegionFields
Private fText 作为 字符串 = 文字
私人 fIcon 作为图片
< span class =code-keyword>私有 fLink 作为 字符串
< span class =code-preprocessor> #End Region
#Region属性
公共 属性文本作为 字符串
获取
返回 fText
结束 获取
设置( ByVal 值 As 字符串)
fText = value
结束 设置
结束 属性
公开 属性图标作为图片
获取
返回 fIcon
结束 获取
设置( ByVal 值 As Image)
fIcon = value
结束 设置
< span class =code-keyword>结束 属性
公共 属性链接作为 字符串
获取
返回 fLink
结束 获取
设置( ByVal value 作为 字符串)
fLink = value
结束 设置
结束 属性
#End Region
#RegionCreator
公开 Sub 新()
结束 Sub
公开 Sub 新( ByVal 文字作为 字符串, ByVal 图标 As Image, ByVal 链接作为 字符串)
我 .Text =正文
我 .Icon = Icon
我 .Link = Link
结束 Sub
公开 < span class =code-keyword> Sub 新( ByVal 数据 As ExplorerBarItemData)
Me .Text = Data.Text
我 .Icon = Data.Icon
结束 Sub
#End Region
结束 类
ExplorerBarItem.vb
Public Class ExplorerBarItem
#RegionFields
Private fData 作为 ExplorerBarItemData
#End Region
#Region属性
<可浏览( False )> _
公开 属性数据 As ExplorerBarItemData
获取
返回 fData
结束 获取
设置( ByVal value As ExplorerBarItemData)
fData = value
End 设置
结束 属性
公共 属性文本 As 字符串
获取
惩戒urn Data.Text
结束 获取
设置( ByVal 值作为 String )
Data.Text = value
End Set
结束 属性
公开 属性链接作为 字符串
获取
返回 Data.Link
结束 获取
设置( ByVal 值作为 字符串)
Data.Link = value
结束 设置
结束 属性
公共 属性图标 As 图片
获取
返回 Data.Icon
结束 获取
设置( ByVal 值 As 图片)
Data.Icon = value
结束 设置
结束 属性
#End Region
公共 Sub 新()
如果 我 .Data 没什么 然后 我 .Data = 新 ExplorerBarItemData
结束 Sub
朋友 Sub 渲染( ByVal G 作为图形, ByVal X As 整数, ByVal Y 作为 整数, ByVal 宽度作为 整数, ByVal 高度 As 整数, ByVal MyFont As 字体)
如果数据 没什么 然后 退出 Sub
G.FillRectangle(Brushes.White,X,Y,Width,Height)
< span class =code-keyword> Dim ImageRectangle As 新矩形( 0 ,Y, 24 , 24 )
Dim TextRectangle As 新矩形( 24 ,Y,宽度 - 25 , 24 )
Dim SF As New StringFormat
SF.Alignment = StringAlignment.Near
SF.LineAlignment = StringAlignment。中心
如果 我 .Data.Icon IsNot Nothing 然后
G.DrawImage( Me .Data.Icon,ImageRectangle)
结束 如果
Dim 字体作为 新字体(MyFont.FontFamily,M yFont.Size)
Dim FontColor As Brush = Brushes.Black
如果 字符串 .IsNullOrWhiteSpace(我 .Data.Link)= False 然后
字体= 新字体(MyFont.FontFamily,MyFont.Size,FontStyle.Underline)
FontColor = Brushes.Blue
其他
结束 如果
G.DrawString( Me .Data.Text,Font,FontColor,TextRectangle,SF)
结束 Sub
公共 功能 Clone()作为 ExplorerBarItem
返回 CType ( Me .MemberwiseClone,ExplorerBarItem)
结束 功能
结束 类
ExplorerBarItemCollection.vb
公共 类 ExplorerBarItemCollection
继承列表( ExplorerBarItem )
结束 类
控制:
公开 类 ExplorerBarItemGroup
继承它 Control
#RegionFields
Private fItems < span class =code-keyword> As 新 ExplorerBarItemCollection
#End Region
#RegionProperties
Public ReadOnly 属性项作为 ExplorerBarItemCollection
获取
返回 fItems
结束 获取
结束 属性
#End Region
#RegionMethods
<跨越式s =code-preprocessor> #End Region
#RegionCreator
公开 Sub 新()
我 .DoubleBuffered = True
我。高度= Items.Count * 24
结束 Sub
#End Region
#RegionRender
受保护的 覆盖 Sub OnPaint( ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase .OnPaint(e)
项目.Item(I).Render(e.Graphics, 0 ,I * 24 ,e.ClipRectangle。宽度, 24 ,我 .Font)
下一步
结束 Sub
#End Region
结束 类 您必须将以下行添加到您的属性中:
< system.componentmodel。 designerserializationvisibility(system.componentmodel.designerserializationvisibility.content)GT;
公共 ReadOnly 属性项目作为 ExplorerBarItemCollection
获取
返回 fItems
结束 获取
结束 财产
Sorry, when I search in google etc. i can't find what i needed
I can't keep a collection with items, when I exit the designer or debug, my Collection get blank..
Here is my code:
ExplorerBarItemData.vb
Public Class ExplorerBarItemData
#Region "Fields"
Private fText As String = "Text"
Private fIcon As Image
Private fLink As String
#End Region
#Region "Properties"
Public Property Text As String
Get
Return fText
End Get
Set(ByVal value As String)
fText = value
End Set
End Property
Public Property Icon As Image
Get
Return fIcon
End Get
Set(ByVal value As Image)
fIcon = value
End Set
End Property
Public Property Link As String
Get
Return fLink
End Get
Set(ByVal value As String)
fLink = value
End Set
End Property
#End Region
#Region "Creator"
Public Sub New()
End Sub
Public Sub New(ByVal Text As String, ByVal Icon As Image, ByVal Link As String)
Me.Text = Text
Me.Icon = Icon
Me.Link = Link
End Sub
Public Sub New(ByVal Data As ExplorerBarItemData)
Me.Text = Data.Text
Me.Icon = Data.Icon
End Sub
#End Region
End Class
ExplorerBarItem.vb
Imports System.ComponentModel
Public Class ExplorerBarItem
#Region "Fields"
Private fData As ExplorerBarItemData
#End Region
#Region "Properties"
<Browsable(False)> _
Public Property Data As ExplorerBarItemData
Get
Return fData
End Get
Set(ByVal value As ExplorerBarItemData)
fData = value
End Set
End Property
Public Property Text As String
Get
Return Data.Text
End Get
Set(ByVal value As String)
Data.Text = value
End Set
End Property
Public Property Link As String
Get
Return Data.Link
End Get
Set(ByVal value As String)
Data.Link = value
End Set
End Property
Public Property Icon As Image
Get
Return Data.Icon
End Get
Set(ByVal value As Image)
Data.Icon = value
End Set
End Property
#End Region
Public Sub New()
If Me.Data Is Nothing Then Me.Data = New ExplorerBarItemData
End Sub
Friend Sub Render(ByVal G As Graphics, ByVal X As Integer, ByVal Y As Integer, ByVal Width As Integer, ByVal Height As Integer, ByVal MyFont As Font)
If Data Is Nothing Then Exit Sub
G.FillRectangle(Brushes.White, X, Y, Width, Height)
Dim ImageRectangle As New Rectangle(0, Y, 24, 24)
Dim TextRectangle As New Rectangle(24, Y, Width - 25, 24)
Dim SF As New StringFormat
SF.Alignment = StringAlignment.Near
SF.LineAlignment = StringAlignment.Center
If Me.Data.Icon IsNot Nothing Then
G.DrawImage(Me.Data.Icon, ImageRectangle)
End If
Dim Font As New Font(MyFont.FontFamily, MyFont.Size)
Dim FontColor As Brush = Brushes.Black
If String.IsNullOrWhiteSpace(Me.Data.Link) = False Then
Font = New Font(MyFont.FontFamily, MyFont.Size, FontStyle.Underline)
FontColor = Brushes.Blue
Else
End If
G.DrawString(Me.Data.Text, Font, FontColor, TextRectangle, SF)
End Sub
Public Function Clone() As ExplorerBarItem
Return CType(Me.MemberwiseClone, ExplorerBarItem)
End Function
End Class
ExplorerBarItemCollection.vb
Public Class ExplorerBarItemCollection
Inherits List(Of ExplorerBarItem)
End Class
The Control:
Public Class ExplorerBarItemGroup
Inherits Control
#Region "Fields"
Private fItems As New ExplorerBarItemCollection
#End Region
#Region "Properties"
Public ReadOnly Property Items As ExplorerBarItemCollection
Get
Return fItems
End Get
End Property
#End Region
#Region "Methods"
#End Region
#Region "Creator"
Public Sub New()
Me.DoubleBuffered = True
Me.Height = Items.Count * 24
End Sub
#End Region
#Region "Render"
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
For I As Integer = 0 To Items.Count - 1
Items.Item(I).Render(e.Graphics, 0, I * 24, e.ClipRectangle.Width, 24, Me.Font)
Next
End Sub
#End Region
End Class
You must add the following line to your Property :
<system.componentmodel.designerserializationvisibility(system.componentmodel.designerserializationvisibility.content)> Public ReadOnly Property Items As ExplorerBarItemCollection Get Return fItems End Get End Property
这篇关于[VB.net]自定义集合错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!