For Loop
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: For Loop

  1. #1
    Join Date
    Sep 2005
    Posts
    233

    For Loop

    I am looking to have a "for loop" search all forms in my project (opened and closed). So far i have the following:

    Code:
    Dim frm as Form
    For Each frm in .......
        If frm.Name = "Form1" Then
               MsgBox ("Form1 Found")
        End If
    Next frm
    what should i replace the .......'s with???
    Seed Gaming
    www.seedgaming.com

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    15,002

    Re: For Loop

    that would only find the forms that were being used. Isn't there also a FORMS collection? Haven't looked, though.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,101

    Re: For Loop

    To loop through all the open forms, you could just use :
    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim frm As Form
            For Each frm In My.Application.OpenForms
                If Not frm.InvokeRequired Then
    
                    MessageBox.Show(frm.Name)
                End If
            Next frm
        End Sub
    Does that help

  4. #4
    Join Date
    Sep 2005
    Posts
    233

    Re: For Loop

    That's the right idea, but i am looking for the loop to search the entire solution, not just the opened forms. As to what dglienna was saying, is there a forms collection, or do i need to create an array and add each and every one of my forms?
    Seed Gaming
    www.seedgaming.com

  5. #5
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    15,002

    Re: For Loop

    You could parse the solution folder, but that'd require a hack to get out of the \bin folder
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Sep 2005
    Posts
    233

    Re: For Loop

    I could do that, but i am looking for an even simpler solution.

    To broaden my question, i am actually looking for a way so that a user can save their file and have the current form saved in the text file. Upon load, i want the corresponding form to appear. I have many forms and it would be pointless to have tons of if statements.

    I was asking about the for loop because if i could loop through all my forms until the name came up that i was looking for, and made that one visible, i wouldn't have to code for each possible outcome.

    So, how can i record the current form to a text file and have it appear upon load???
    Seed Gaming
    www.seedgaming.com

  7. #7
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,101

    Re: For Loop

    The My.Application.OpenForms is the equivalent of the Forms Collection, as we knew it in VB6.

    I'd recommend you using either an INI file or even the Registry, to save & retrieve the form's info.

  8. #8
    Join Date
    Sep 2005
    Posts
    233

    Re: For Loop

    i would prefer a .ini file but how would i go about doing that?
    Seed Gaming
    www.seedgaming.com

  9. #9
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,101

    Re: For Loop

    Here's an idea!
    Why not just use the app.config file to store the name of all our forms, then, read it from there

    For example.
    Say for instance you had a form named Form1.
    In the Properties Window, select the + sign next to ApplicationSettings
    Click on PropertyBinding
    You will see a small ellipses button ( ... ) appear inside PropertyBinding
    Click on the ellipses button
    That will produce a screen called Application Settings for 'Form1'
    In the list, scroll down until you find the Name property
    Once clicked, you will see a dropdown button, click it
    Select New...
    A screen called New Application Setting appears
    Give it a name - use the Form's name
    Then click OK
    And click OK again

    Do this for each form

    If you were to open your app.config file, you would see a section similar to this :
    HTML Code:
        <userSettings>
            <YourApplicationName.My.MySettings>
                <setting name="Form2" serializeAs="String">
                    <value>Form2</value>
                </setting>
                <setting name="Form1" serializeAs="String">
                    <value>Form1</value>
                </setting>
                <setting name="Form3" serializeAs="String">
                    <value>Form3</value>
                </setting>
            </YourApplicationName.My.MySettings>
        </userSettings>
    Now, to read this property from the app.config file, you could just use :
    Code:
            MessageBox.Show(My.Settings.Form1.ToString)
    You could also perhaps store all these names into an array, then compare My.Application.OpenForms to the elements of the array etc.

    Just an idea

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center