Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Loop through multiple charts on a form in vb express

Posted on 2014-08-12
2
Medium Priority
?
876 Views
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

Etc.

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.

Regards

Terry
0
Comment
Question by:Terrygordon
2 Comments
 
LVL 11

Accepted Solution

by:
Visio_Guy earned 2000 total points
ID: 40255269
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

        Next

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:

c.HasChildren()

Open in new window


Then loop through, for example:

For Each cChild as Control in c.Controls()
...

Open in new window

0
 

Author Comment

by:Terrygordon
ID: 40255309
Hi Visio guy

Brilliant. It works perfectly.

Regards

Terry
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

581 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