Results 1 to 19 of 19

Thread: [VB2010] - if form is a control, why isn't in collection list?

  1. #1

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,961

    [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?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,376

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,376

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,961

    Re: [VB2010] - if form is a control, why isn't in collection list?

    Quote Originally Posted by dday9 View Post
    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?)
    VB6 2D Sprite control

    To live is difficult, but we do it.

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,376

    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  6. #6

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,961

    Re: [VB2010] - if form is a control, why isn't in collection list?

    Quote Originally Posted by dday9 View Post
    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
    VB6 2D Sprite control

    To live is difficult, but we do it.

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,376

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  8. #8
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  9. #9
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: [VB2010] - if form is a control, why isn't in collection list?

    Quote Originally Posted by dunfiddlin View Post
    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

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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

    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.
    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

  12. #12

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,961

    Re: [VB2010] - if form is a control, why isn't in collection list?

    Quote Originally Posted by jmcilhinney View Post
    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)
    VB6 2D Sprite control

    To live is difficult, but we do it.

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

    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
    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

  14. #14

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,961

    Re: [VB2010] - if form is a control, why isn't in collection list?

    Quote Originally Posted by jmcilhinney View Post
    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.
    VB6 2D Sprite control

    To live is difficult, but we do it.

  15. #15
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: [VB2010] - if form is a control, why isn't in collection list?

    Quote Originally Posted by techgnome View Post
    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?

  16. #16

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,961

    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
    VB6 2D Sprite control

    To live is difficult, but we do it.

  17. #17
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: [VB2010] - if form is a control, why isn't in collection list?

    Quote Originally Posted by Evil_Giraffe View Post
    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.

    Quote Originally Posted by Evil_Giraffe View Post
    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:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    4.         Try
    5.             Dim panel As New Panel()
    6.             panel.Controls.Add(panel)
    7.         Catch ex As Exception
    8.  
    9.         End Try
    10.  
    11.         Try
    12.             Dim panel1 As New Panel
    13.             Dim panel2 As New Panel
    14.             panel1.Controls.Add(panel2)
    15.             panel2.Controls.Add(panel1)
    16.         Catch ex As Exception
    17.  
    18.         End Try
    19.  
    20.         Try
    21.             Dim form2 As New Form2
    22.             Me.Controls.Add(form2)
    23.         Catch ex As Exception
    24.  
    25.         End Try
    26.  
    27.     End Sub
    28. End Class

  18. #18
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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

    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.
    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

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