Results 1 to 4 of 4

Thread: [RESOLVED] Count the Number of Controls on a WPF Form

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Burlington, ON, Canada
    Posts
    343

    Resolved [RESOLVED] Count the Number of Controls on a WPF Form

    Hi again,

    I'm looking to count the number of controls on a form within WPF. As usual, I have a working winforms example and I'm trying to get its WPF equivalent.

    Here's the winforms code based on looking for a chart control:

    Code:
    Dim countnumberofcharts As Integer = 0
                For Each ctrl As Control In Me.Controls
                    If TypeOf ctrl Is System.Windows.Forms.DataVisualization.Charting.Chart Then
                        countnumberofcharts = countnumberofcharts + 1
                    End If
                Next
    And I'm erroring out with "Controls is not a member of MyApp.ControlCountTest"

    Thanks to all who have been assisting in my uphill battle of transitioning from winforms to WPF - its definitely an adventure.


    Also, as a postscript, is there any known native charting controls for WPF or am I stuck trying to pull in the winforms charts within a wpf window?

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Burlington, ON, Canada
    Posts
    343

    Re: Count the Number of Controls on a WPF Form

    okay so i changed my code to the following:

    Code:
     Dim countnumberofcharts As Integer = 0
                For Each ctrl As Control In Me.grid1.Children
                    If TypeOf ctrl Is Chart Then
                        countnumberofcharts = countnumberofcharts + 1
                    End If
                Next
    and specifying grid1.children seemed to help the for next loop itsef, but now its erroring out with the following:

    "expression of type system.windows.controls.control can never be of type system.windows.forms.datavisualization.charting.chart"

    I have all of the imports statements in place for the class - i'll keep digging as usual but if anybody has encountered trying to identify a chart as a control in wpf, i'm all ears.

    Thanks.

  3. #3
    Lively Member
    Join Date
    Jul 2007
    Posts
    127

    Re: Count the Number of Controls on a WPF Form

    you can always just count by using the index. so like this:

    Code:
    For i as int32 = 0 to grid1.children.count - 1
      if grid1.children.item(i).gettype = gettype(Chart) then
        count = count + 1
      End If
    next i

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Burlington, ON, Canada
    Posts
    343

    Re: Count the Number of Controls on a WPF Form

    absolutely bang on - worked like a charm - thx

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