Loop through multiple charts on a form in vb express

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.


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TerrygordonAuthor Commented:
Hi Visio guy

Brilliant. It works perfectly.


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.