Results 1 to 8 of 8

Thread: How Count Elements on the form

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    162

    How Count Elements on the form

    Hi

    im facing to 1 problem here. I need to count the Panel elements on the current form and put them into array, but i dont have any idead where to start. Anyone please ?
    i know how to count all elements on the form but thats about it.

    Thx for help
    Last edited by michalss; Nov 28th, 2009 at 06:06 PM.

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: How Count Elements on the form

    You just loop thru the form.Controls collection and test each control to see if it's a Panel.
    Code:
    Dim aPanel As Panel = Nothing
    For Each ctrl as Control In Me.Controls
       If TypeOf ctrl Is Panel Then
          aPanel = DirectCast(ctrl, Panel)
          'Do whatever desired with this panel here
       End If
    Next
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How Count Elements on the form

    There are various ways you could do it but, assuming .NET 3.5, the simplest way would be to use LINQ:
    vb.net Code:
    1. Dim panels = Me.Controls.OfType(Of Panel).ToArray()
    2.  
    3. MessageBox.Show("There are " & panels.Length & " Panels on the form.")
    That will only get Panels on the form itself, not those nested within other containers. For that you'd have to write your own recursive method.

    Note that, if you know how to count all controls then you pretty much know how to count Panels. You simply test the type of each control as you find it:
    vb.net Code:
    1. If TypeOf ctl Is Panel Then
    Note also that you can Add the Panels to a List as you find them because, unlike an array, a List will grow dynamically. At the end you can get the Count property of the List. If you really need an array then you can call ToArray on the List. You can then get the arrays Length property too.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    162

    Re: How Count Elements on the form

    Thx perfect but works but i have small problem. I have panels on the form named from p1 to p10 but i need them in the array systematics like
    p0,p1,p2, an so on.
    Last edited by michalss; Nov 28th, 2009 at 06:52 PM.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How Count Elements on the form

    You know for a fact that you have Panels p1 to p10 yet you need to count how many Panels you have? You just said that you have 10, so why do you need to count them? Why can't you just create the array directly yourself like this:
    vb.net Code:
    1. Private panels As Panel()
    2.  
    3. Private Sub Form1_Load() Handles MyBase.Load
    4.     Me.panels = New Panel() {Me.p1, Me.p2, Me.p3, Me.p4, Me.p5, Me.p6, Me.p7, Me.p8, Me.p9, Me.p10}
    5. End Sub
    Don't make things harder than they need to be. If you can hard-code something because it never changes then hard-code it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    162

    Re: How Count Elements on the form

    Coz i dont know how many panel user will create on the form.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How Count Elements on the form

    I think you need to provide a FULL and CLEAR explanation of EXACTLY what's happening, which you really should do in the first post. If you have requirements like that the Panels need to be in a specific order then you need to tell us that in the first place so that we don't waste our time and yours providing solutions that don't meet your requirements.

    So, are there Panels on your form to begin with or not? Does the user add Panels at run time? Presumably yes. I don't see that you need to count Panels after the fact. If the user is adding the Panels then can't you just add them to a List as the user adds them to the form? That way they will be in order the whole time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    162

    Re: How Count Elements on the form

    Sorry mate its all done now it works perfect. I made 1 mistake. And now its fine. thx for help

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