Loop through multiple charts on a form in vb express

Posted on 2014-08-12
Last Modified: 2014-08-12
Hi all

I have a form with several chart objects (Chart1, Chart2, Chart3, etc.). The charts are created at runtime (currently using a with statement to define numerous properties of the chart).

At the minute I am using:

With Chart1
List of properties

With Chart2
List of properties


However, as all the charts have multiple properties in common, I wondered if it was possible to loop through every chart on the form and set these common properties? This would drastically reduce the amount of code.

I've played around with trying to assign variable names to the charts and using 'Do' for each control on the form that is a chart, but my programming skills are pretty basic, and there is always something that VB doesn't like.


Question by:Terrygordon
    LVL 11

    Accepted Solution

    Hi Terry,

    You can easily loop through all the controls on a page. You can then check their type or try to cast them as a Chart object. If successful, you know you've got a Chart object, and you can perform Chart object operations.

    Here's some sample code. Make a Windows Form, drop a bunch of controls (including Charts) on it, then run the code (place the For loop in a button click procedure, for example.)

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            For Each c As Control In Me.Controls '//...Me is the form
                '// Dump the name of every control on the form:
                Debug.WriteLine("Control name: " & c.Name)
                '// Detect if this control is a chart, then do
                '// chart stuff to it:
                Dim chrt As Chart = TryCast(c, Chart)
                If (chrt IsNot Nothing) Then
                    '// For example Control objects don't have Images.Count, 
                    '// but Charts do!
                    Debug.WriteLine("-Chart image count: " & chrt.Images.Count)
                End If

    Open in new window

    Keep in mind, I don't think this will hit everything if you've got controls on Panels or in groups. You would need to recurse into any container objects and look at each child of the container. You can check with:


    Open in new window

    Then loop through, for example:

    For Each cChild as Control in c.Controls()

    Open in new window


    Author Comment

    Hi Visio guy

    Brilliant. It works perfectly.



    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
    Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
    Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
    In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now