Results 1 to 6 of 6

Thread: [RESOLVED] Referring to MSChart with Variable - CType

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Location
    UK
    Posts
    14

    Resolved [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

    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!

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,024

    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
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Location
    UK
    Posts
    14

    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
    Last edited by min1me; Dec 12th, 2013 at 06:35 AM.

  4. #4
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,024

    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
    Last edited by KGComputers; Dec 12th, 2013 at 07:01 AM.
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Location
    UK
    Posts
    14

    Re: Referring to MSChart with Variable - CType

    Excellent - thank you for your help - been stuck on that all morning!

  6. #6
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,024

    Re: Referring to MSChart with Variable - CType

    Great that helped you! Kindly mark this thread as RESOLVED.

    KG
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width