[RESOLVED] Referring to MSChart with Variable - CType
Hello,
Is it possible to use CType to refer to an MSChart control using a variable? I've seen many examples of this being used for textbox, combobox etc but I can't seem to get it to work for MSChart :confused:
Dim chartID = "Chart" & x
CType(Controls(chartID), System.Web.UI.DataVisualization.Charting.Chart).Visible = True
CType(Controls(chartID), System.Web.UI.DataVisualization.Charting.Chart).Width = 800
Is it "System.Web.UI.DataVisualization.Charting.Chart" that I'm getting wrong? Is there a better way of doing it?
Thanks!
Re: Referring to MSChart with Variable - CType
Hi,
Yes, you can use CType for to cast controls to its specific type. Actually, there are other methods
for type casting.
Quick question though, are you referring to ASP.NET charting control or winforms chart control?
If its the latter, change System.Web.UI.DataVisualization.Charting.Chart to
System.Windows.Forms.DataVisualization.Charting.Chart.
Code:
CType(Controls("chart1"), System.Windows.Forms.DataVisualization.Charting.Chart).Visible = True
CType(Controls("chart1"), System.Windows.Forms.DataVisualization.Charting.Chart).Dock = DockStyle.Fill
CType(Controls("chart1"), System.Windows.Forms.DataVisualization.Charting.Chart).BackColor = Color.AliceBlue
KG
Re: Referring to MSChart with Variable - CType
It's the ASP.NET charting control - I get this error when trying to use a variable in lieu of the actual chart ID:
System.InvalidCastException: Conversion from string "Chart1" to type 'Integer' is not valid. ---> System.FormatException: Input string was not in a correct format. at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat) at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value) --- End of inner exception stack trace
If I simplify to CType(Controls("Label1"), Label).Text = "Hello!!" and run in debug - I get similar error:
Conversion from string "Label1" to type 'Integer' is not valid
Re: Referring to MSChart with Variable - CType
Just a slight modification. For accessing asp.net control, use FindControl() method.
You might use CType() as alternative to DirectCast().
See sample below:
Code:
Dim id As String = DirectCast(Page.FindControl("Chart1"), System.Web.UI.DataVisualization.Charting.Chart).ID
KG
Re: Referring to MSChart with Variable - CType
Excellent - thank you for your help - been stuck on that all morning!
Re: Referring to MSChart with Variable - CType
Great that helped you! Kindly mark this thread as RESOLVED. :)
KG