“chartareacollection”中已存在名称的图表元素 [英] A chart element with the name already exists in the 'chartareacollection'

查看:293
本文介绍了“chartareacollection”中已存在名称的图表元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在VB.NET中开发一个应用程序,我试图每隔3秒更新一次我的图表,我尝试了一些东西,但它让我一个错误。我已经制作了一个Public方法然后将其调用到我的表单中。



我尝试过的方法:



这是方法:

Dim Chart1 =新图表()

Public Sub UpdateChart()

试试

SQLCon =新的SqlConnection

SQLCon.ConnectionString =...........

Timer1.Interval = 3000

Timer1.Start()

Dim sqlStatis As String =SELECT Top 5 Filename,Filesize FROM infofile

Dim da As New SqlDataAdapter (sqlStatis,SQLCon)

Dim ds As New DataSet()

da.Fill(ds,infofile)

Chart1.Refresh( )

Dim ChartArea1 As ChartArea = New ChartArea()

Dim ChartArea2 As ChartArea = New ChartArea()

Dim Legend1 As Legend = New Legend ()

Dim Legend2 As Legend = New Legend ()

Dim Series1 As Series = New Series()

Dim Series2 As Series = New Series()

Me.Controls.Add( Chart1)



ChartArea1.Name =ChartArea1

Chart1.ChartAreas.Add(ChartArea1)

Legend1 .Name =Legend1

Chart1.Legends.Add(Legend1)

Chart1.Location = New System.Drawing.Point(12,12)

Chart1.Name =Chart1

Series1.ChartArea =ChartArea1

Series1.Legend =Legend1

Series1。 Name =Tamanho do ficheiro

Chart1.Series.Add(Series1)

Chart1.Size = New System.Drawing.Size(600,300)

Chart1.TabIndex = 0

Chart1.Text =Chart1



Chart1.Series(Tamanho do ficheiro) .XValueMember =文件名

Chart1.Series(Tamanho do ficheiro)。YValueMembers =Filesize



Chart1.DataSource = ds.Tables(infofile)

Catch ex As Exception

MessageBox.Show(ex.ToString())

最后

SQLCon.Dispose()

结束尝试

结束子



然后我将其调成我的表格:



PublicSubGráfico_Load(发件人作为对象,e作为EventArgs)处理MyBase.Load



UpdateChart()

< br $>
结束子



进入计时器:

Private Sub Timer1_Tick(发送者为对象,e为EventArgs)处理Timer1.Tick



UpdateChart()



结束子



每隔3秒就会出现一个错误:名称中的图表元素已经存在于'ChartAreaCollection'中

So I am developing an application in VB.NET and I am trying to update my chart every 3 seconds and I've tried some things but it keeps me an error. I've made a Public method and then call it into my form.

What I have tried:

This is the method:
Dim Chart1 = New Chart()
Public Sub UpdateChart()
Try
SQLCon = New SqlConnection
SQLCon.ConnectionString = "..........."
Timer1.Interval = 3000
Timer1.Start()
Dim sqlStatis As String = "SELECT Top 5 Filename, Filesize FROM infofile"
Dim da As New SqlDataAdapter(sqlStatis, SQLCon)
Dim ds As New DataSet()
da.Fill(ds, "infofile")
Chart1.Refresh()
Dim ChartArea1 As ChartArea = New ChartArea()
Dim ChartArea2 As ChartArea = New ChartArea()
Dim Legend1 As Legend = New Legend()
Dim Legend2 As Legend = New Legend()
Dim Series1 As Series = New Series()
Dim Series2 As Series = New Series()
Me.Controls.Add(Chart1)

ChartArea1.Name = "ChartArea1"
Chart1.ChartAreas.Add(ChartArea1)
Legend1.Name = "Legend1"
Chart1.Legends.Add(Legend1)
Chart1.Location = New System.Drawing.Point(12, 12)
Chart1.Name = "Chart1"
Series1.ChartArea = "ChartArea1"
Series1.Legend = "Legend1"
Series1.Name = "Tamanho do ficheiro"
Chart1.Series.Add(Series1)
Chart1.Size = New System.Drawing.Size(600, 300)
Chart1.TabIndex = 0
Chart1.Text = "Chart1"

Chart1.Series("Tamanho do ficheiro").XValueMember = "Filename"
Chart1.Series("Tamanho do ficheiro").YValueMembers = "Filesize"

Chart1.DataSource = ds.Tables("infofile")
Catch ex As Exception
MessageBox.Show(ex.ToString())
Finally
SQLCon.Dispose()
End Try
End Sub

Then I called it into my form:

Public Sub Gráfico_Load(sender As Object, e As EventArgs) Handles MyBase.Load

UpdateChart()

End Sub

And into the timer:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

UpdateChart()

End Sub

And every 3 seconds it will give me an error called: A chart element with the name already exists in the 'ChartAreaCollection'

推荐答案

Y你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



为了缩小问题范围:

- 通过在更新例程中添加断点来准备代码

- 禁用计时器。

- 火马图表更新。

- 用调试器逐行运行程序。



调试器将显示程序崩溃的位置。

你可能忘了删除一个图表元素,或者你需要重置图表。

一旦发现错误,请告诉我们。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

In order to narrow the problem:
- prepare the code by adding a breakpoint in update routine
- disable the timer.
- fire manually the chart update.
- run the program line by line with the debugger.

The debugger will show you where the program crash.
May be you forgot the remove a chart element or you need to reset the chart.
Once found what is wrong, let us know.


这篇关于“chartareacollection”中已存在名称的图表元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆