|
-
Jun 18th, 2013, 04:08 PM
#1
Thread Starter
PowerPoster
[VB2010] - if form is a control, why isn't in collection list?
see these code:
Code:
dim intControlsIndex as integer
for intControlsIndex = 0 to me.controls.count-1
messagebox.show(me.controls(intControlsIndex).name )
next
if the form is a control why isn't in collection list?
-
Jun 18th, 2013, 04:35 PM
#2
Re: [VB2010] - if form is a control, why isn't in collection list?
Try:
Code:
For Each itm As Control In Me.Controls
MessageBox.Show(itm.Name)
Next
-
Jun 18th, 2013, 04:37 PM
#3
Re: [VB2010] - if form is a control, why isn't in collection list?
Or:
Code:
dim intControlsIndex as integer
for intControlsIndex = 0 to me.controls.count-1
messagebox.show(me.controls.item(intControlsIndex).name )
next
-
Jun 18th, 2013, 04:42 PM
#4
Thread Starter
PowerPoster
Re: [VB2010] - if form is a control, why isn't in collection list?
 Originally Posted by dday9
Or:
Code:
dim intControlsIndex as integer
for intControlsIndex = 0 to me.controls.count-1
messagebox.show(me.controls.item(intControlsIndex).name )
next
sorry the form isn't in list
(have you tested the code?)
-
Jun 18th, 2013, 04:46 PM
#5
Re: [VB2010] - if form is a control, why isn't in collection list?
That's because Me is parent container. Consider this:
Code:
dim intControlsIndex as integer
for intControlsIndex = 0 to Panel1.controls.count-1
messagebox.show(Panel1.controls.item(intControlsIndex).name )
next
It won't return panel1 would it? If you want to get all the forms in an application look at this.
-
Jun 18th, 2013, 05:02 PM
#6
Thread Starter
PowerPoster
Re: [VB2010] - if form is a control, why isn't in collection list?
 Originally Posted by dday9
That's because Me is parent container. Consider this:
Code:
dim intControlsIndex as integer
for intControlsIndex = 0 to Panel1.controls.count-1
messagebox.show(Panel1.controls.item(intControlsIndex).name )
next
It won't return panel1 would it? If you want to get all the forms in an application look at this.
sorry error on Panel1, i try use the form name, but VB tell me for use 'me'.... strange
-
Jun 18th, 2013, 05:09 PM
#7
Re: [VB2010] - if form is a control, why isn't in collection list?
The Panel1 was an example. That was if you where to drag a panel to your form and add some controls to it. It would return all the controls inside the panel, but not the panel itself. Just like your iteration through each of the items in Me(which is the form you're using) will return all the controls inside the form, but not the form itself. If you wanted to get the parent of the controls you'd call: <control>.Parent
-
Jun 18th, 2013, 07:49 PM
#8
Re: [VB2010] - if form is a control, why isn't in collection list?
if the form is a control why isn't in collection list?
Because it isn't a control! It's a form. And even if it were a control you obviously wouldn't find it in it's own Controls collection. That would be like a shopping bag that contained all the shopping and itself! There is a collection of open forms available at Application level if that's of any use to you.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Jun 19th, 2013, 12:11 AM
#9
Re: [VB2010] - if form is a control, why isn't in collection list?
 Originally Posted by dunfiddlin
Because it isn't a control!
Yes it is. Check the inheritance hierarchy: http://msdn.microsoft.com/en-us/libr...orms.form.aspx:
Code:
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.ScrollableControl
System.Windows.Forms.ContainerControl
System.Windows.Forms.Form
-
Jun 19th, 2013, 12:36 AM
#10
Re: [VB2010] - if form is a control, why isn't in collection list?
Doesn't matter that it is or it isn't... looping through me.controls isn't going to find it because you're already in it... in the end that's the "answer" to the "problem" at hand... which is why doesn't it appear in the controls collection... because the collection is in the form object, and it's not going to have a reference to itself in it (because if it did, that would result in an infinite recursive reference cycle from which one would not be able to return from).
-tg
-
Jun 19th, 2013, 02:47 AM
#11
Re: [VB2010] - if form is a control, why isn't in collection list?
Consider this. If you had a bag and you wanted to list all the things that were inside the bag, would you add the bag to that list? Of course not. When you loop through the form's Controls collection you are getting all the controls that are on the form. The form is not on itself so of course it's not in that collection.
-
Jun 19th, 2013, 04:58 AM
#12
Thread Starter
PowerPoster
Re: [VB2010] - if form is a control, why isn't in collection list?
 Originally Posted by jmcilhinney
Consider this. If you had a bag and you wanted to list all the things that were inside the bag, would you add the bag to that list? Of course not. When you loop through the form's Controls collection you are getting all the controls that are on the form. The form is not on itself so of course it's not in that collection.
and if you need? what collection i can use?
(i'm reading Teach YourSelf Visual Basic 2010 in 24 Hours... i have the Mastering Visual Basic 2010(and the 4 chapers bonus), but it's a big book for start )
-
Jun 19th, 2013, 05:08 AM
#13
Re: [VB2010] - if form is a control, why isn't in collection list?
The code you posted originally must already be in the form. That code accesses the Controls collection of the form. In order to get its Controls collection, you must already have the form. What do you suppose this is:
Code:
dim intControlsIndex as integer
for intControlsIndex = 0 to me.controls.count-1
messagebox.show(me.controls(intControlsIndex).name )
next
-
Jun 19th, 2013, 06:02 AM
#14
Thread Starter
PowerPoster
Re: [VB2010] - if form is a control, why isn't in collection list?
 Originally Posted by jmcilhinney
The code you posted originally must already be in the form. That code accesses the Controls collection of the form. In order to get its Controls collection, you must already have the form. What do you suppose this is:
Code:
dim intControlsIndex as integer
for intControlsIndex = 0 to me.controls.count-1
messagebox.show(me.controls(intControlsIndex).name )
next
sorry my english... but you are tell me, if i need change some properties on form and controls from that form, i must 1st change the form(outside of loop) and then use the loop for change the controls properties, right?
"Every form has a Controls collection, which might not contain any controls; even
if no controls are on the form, the form still has a Controls collection."(i read from book)
is these afirmation correct(about the form)?
Last edited by joaquim; Jun 19th, 2013 at 06:06 AM.
-
Jun 19th, 2013, 06:06 AM
#15
Re: [VB2010] - if form is a control, why isn't in collection list?
 Originally Posted by techgnome
Doesn't matter that it is or it isn't... looping through me.controls isn't going to find it because you're already in it...
No, it doesn't matter to the problem of "why it isn't in the collection of controls". However, I figured that had already been answered so was merely correcting an inaccuracy. To further the form-as-a-shopping-bag metaphor*, is there any reason why you couldn't put another shopping bag inside a shopping bag? No, which is why it's possible for an instance of a Form to be inside the Controls collection of another instance of a Form (or, guessing at where the Controls collection comes from, an instance of a ContainerControl to be inside the Controls collection of another instance of a ContainerControl). I'm guessing that the Forms framework takes care of maintaining a cycle-free graph, although it would be an interesting experiment to try.
Another question is "what does it mean for a Form to be a child control of another control? Does that make any sense in terms of the Forms framework?
-
Jun 19th, 2013, 06:17 AM
#16
Thread Starter
PowerPoster
Re: [VB2010] - if form is a control, why isn't in collection list?
"Another question is "what does it mean for a Form to be a child control of another control? Does that make any sense in terms of the Forms framework?"
MDI(Multi Document Interface) have the Parent Form and some Child Forms
-
Jun 19th, 2013, 06:20 AM
#17
Re: [VB2010] - if form is a control, why isn't in collection list?
 Originally Posted by Evil_Giraffe
I'm guessing that the Forms framework takes care of maintaining a cycle-free graph, although it would be an interesting experiment to try.
Adding a ContainerControl directly to its own Controls collection throws an ArgumentException with the message "A circular control reference has been made. A control cannot be owned by or parented to itself." The same exception is thrown if you create an indirect cycle (panel1 added top panel2's Controls collection, then adding panel2 to panel1's Controls). I would assume it checks all the way up the tree and it won't matter how many ContainerControls are in the cycle, it will always detect it.
 Originally Posted by Evil_Giraffe
Another question is "what does it mean for a Form to be a child control of another control? Does that make any sense in terms of the Forms framework?
Another ArgumentException is thrown with the message "Top-level control cannot be added to a control.".
Here's the code I used to test (with a breakpoint set on the three Catch lines):
vbnet Code:
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Try Dim panel As New Panel() panel.Controls.Add(panel) Catch ex As Exception End Try Try Dim panel1 As New Panel Dim panel2 As New Panel panel1.Controls.Add(panel2) panel2.Controls.Add(panel1) Catch ex As Exception End Try Try Dim form2 As New Form2 Me.Controls.Add(form2) Catch ex As Exception End Try End Sub End Class
-
Jun 19th, 2013, 06:55 AM
#18
Re: [VB2010] - if form is a control, why isn't in collection list?
Interesting... it must be controlled by the control... or overridable... I worked on an app once that used a tab control, the contents of the individual tabs were actually built as forms and I was able to create a tab, then add the form to the controls collection of the tabpage. I've never really had the chance to try that with other controls until now... so I had assumed it was the same throughout... but that doesn't appear to be the case... but that was also some time ago... so either, 1) tabpages are the odd duck, or 2) that behavior has changed since then.
-tg
-
Jun 19th, 2013, 07:41 AM
#19
Re: [VB2010] - if form is a control, why isn't in collection list?
The Form class has a TopLevel control. To nest a form inside another form you must set TopLevel to False for the child form.
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
|