Results 1 to 6 of 6

Thread: for each i as (object here) or for each i in (form here)

  1. #1

    Thread Starter
    Frenzied Member Ultimasnake's Avatar
    Join Date
    Feb 2002
    Location
    Amsterdam, holland
    Posts
    1,172

    for each i as (object here) or for each i in (form here)

    i cant seem to get either of these two vb6 commands correctly

    VB Code:
    1. dim i as popbox
    2. for each i in frmmain ' where frmmain is the form i am on
    3.   'do something with object here
    4. next i
    5.  
    6.  
    7. ' and
    8. dim i as object
    9. for each i as popbox ' where popbox is a usercontroll in the current form
    10.   'do something with object here
    11. next i

    so what is the proper Vb.net way to loop through a certain kind of object and do something with it?
    For my PC and MS Smartphone 2003 software visit
    http://www.ultimasoftware.nl

  2. #2
    Hyperactive Member MarkusJ_NZ's Avatar
    Join Date
    Jun 2001
    Posts
    375
    You have to look at the controls collection of the object that you want to loop though...

    VB Code:
    1. Dim oO As Object
    2.  
    3. For Each oO In Me.Controls
    4.             msgBox(oO.id)
    5. Next

    HTH
    Cheers
    MarkusJ

  3. #3

    Thread Starter
    Frenzied Member Ultimasnake's Avatar
    Join Date
    Feb 2002
    Location
    Amsterdam, holland
    Posts
    1,172
    ok getting closer though i only want the controls that are "made" from the popbox control... there named popbox1 popbox2 etc etc :S

    Dim i As Popbox

    For Each i In Me.Controls
    i.color_deselect()
    Next i

    doesnt seem to work though :S

    An unhandled exception of type 'System.InvalidCastException' occurred in pop3 checker pro.net.exe

    Additional information: Specified cast is not valid.
    For my PC and MS Smartphone 2003 software visit
    http://www.ultimasoftware.nl

  4. #4
    Hyperactive Member MarkusJ_NZ's Avatar
    Join Date
    Jun 2001
    Posts
    375
    Hi, you have to test the type of control in the collection and depending on what it is (popup in your case) do something...

    Two ways would be to test either then name of the control (All your controls that you want to check are called popup) or test the control type..

    The following is off the top of my head so beware the bugs

    VB Code:
    1. Dim i As object
    2. For Each i In Me.Controls
    3. ' Check the name. If the name of the control is popup then it is one of your controls...
    4. if i.id.toString.toLower.indexof("popup") then
    5. i.color_deselect()
    6. end if
    7. Next i
    8.  
    9. ' you could also check the type of the control. Syntax below is probably wrong but you get the idea
    10. if i.getType = popup then
    11. i.color_deselect()
    12. end if
    13.  
    14. 'Finally you could just do an error catch <shady crap programmer grin>
    15.  
    16. Dim i As object
    17. For Each i In Me.Controls
    18. try
    19. ii.color_deselect()
    20. catch
    21. end try
    22. Next i

    hth
    Cheers
    MarkusJ

  5. #5
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539

    dont know if this wil help

    Over the weekend ive been messing with my old vb program i started ages ago and never got around to finishing it

    there was loads of unsupported vb6 commands in my code which i really really needed to use so i just added this to import list

    Imports vb = Microsoft.visualbasic

    then if you need to access an old vb6 function then just use this
    vb.Left(blah,1)

    I guess this is not the correct way to do things but when your stressed out about finding how to do it and you know how vb6 done it, its a good alternative

    Hope it helps
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

  6. #6
    Addicted Member PeteD's Avatar
    Join Date
    Jun 2003
    Location
    Sydney
    Posts
    158
    In VB.NET 2003, you can do this in a neater way:

    VB Code:
    1. For Each textBoxControl as TextBox in Me.Controls
    2.      Console.WriteLine textBoxControl.Name
    3. Next

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