|
-
Nov 28th, 2009, 05:47 PM
#1
Thread Starter
Addicted Member
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.
-
Nov 28th, 2009, 06:14 PM
#2
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 -
-
Nov 28th, 2009, 06:15 PM
#3
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:
Dim panels = Me.Controls.OfType(Of Panel).ToArray() 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:
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.
-
Nov 28th, 2009, 06:49 PM
#4
Thread Starter
Addicted Member
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.
-
Nov 28th, 2009, 06:54 PM
#5
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:
Private panels As Panel() Private Sub Form1_Load() Handles MyBase.Load Me.panels = New Panel() {Me.p1, Me.p2, Me.p3, Me.p4, Me.p5, Me.p6, Me.p7, Me.p8, Me.p9, Me.p10} 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.
-
Nov 28th, 2009, 07:01 PM
#6
Thread Starter
Addicted Member
Re: How Count Elements on the form
Coz i dont know how many panel user will create on the form.
-
Nov 28th, 2009, 07:07 PM
#7
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.
-
Nov 28th, 2009, 07:18 PM
#8
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|