我试图通过继承图表控件来实现滚动折线图(类似于旧图表记录器)。要做到这一点,我需要做两件事:
虽然我可以手动执行所有操作,但我想将所有内容封装到一个自定义控件中。我想只使用设计器添加自定义控件,并且已经设置了所有这些属性,并且已经包含了行为。
作为测试,我试图只更改继承的图表控件的Text属性,但没有成功:
Public Class ScrollChart
Inherits Chart
Public Sub New()
Me.Text = "Test"
End Sub
End Class
通过覆盖Text属性:
Public Class ScrollChart
Inherits Chart
Private _myText As String = "Test"
Public Overrides Property Text() As String
Get
Return _myText
End Get
Set(value As String)
_myText = value
End Set
End Property
End Class
我尝试以两种不同的方式更改Series chartType:
Public Class ScrollChart
Inherits Chart
Friend WithEvents Chart1 As Chart
Private Sub InitializeComponent()
Dim ChartArea1 As System.Windows.Forms.DataVisualization.Charting.ChartArea = New System.Windows.Forms.DataVisualization.Charting.ChartArea()
Dim Legend1 As System.Windows.Forms.DataVisualization.Charting.Legend = New System.Windows.Forms.DataVisualization.Charting.Legend()
Dim Series1 As ScrollSeries = New ScrollSeries
Me.Chart1 = New System.Windows.Forms.DataVisualization.Charting.Chart()
CType(Me.Chart1, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'Chart1
'
ChartArea1.Name = "ChartArea1"
Me.Chart1.ChartAreas.Add(ChartArea1)
Legend1.Name = "Legend1"
Me.Chart1.Legends.Add(Legend1)
Me.Chart1.Location = New System.Drawing.Point(0, 0)
Me.Chart1.Name = "Chart1"
Series1.ChartArea = "ChartArea1"
Series1.Legend = "Legend1"
Series1.Name = "Series1"
Series1.ChartType = SeriesChartType.Line
Me.Chart1.Series.Add(Series1)
Me.Chart1.Size = New System.Drawing.Size(300, 300)
Me.Chart1.TabIndex = 0
Me.Chart1.Text = "Test"
CType(Me.Chart1, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
End Class
Public Class ScrollSeries
Inherits Series
Public Sub New()
MyBase.New()
Me.ChartType = SeriesChartType.Line
End Sub
End Class
在每种情况下,我使用设计器添加的控件中的属性都没有改变。我也尝试继承DataPointCollection,但不能因为它没有暴露的New(),因此无法继承。
我怀疑我需要替换我继承的Chart使用的Series和DataPointCollection类,以便更改它们的属性和方法,但到目前为止我还不知道如何这样做。
答案 0 :(得分:2)
创建用户控件(不继承)并向其添加图表控件,然后您可以在用户控件上创建包装图表控件的属性。例如:
Public Property ChartText() As String
Get
Return Chart1.Text
End Get
Set(value As String)
Chart1.Text = value
End Set
End Property
在您的控制中,您可以指定所需的任何属性!
在这种情况下,我没有看到继承的好处。