-
July 28th, 2008, 12:34 AM
#1
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???
-
July 28th, 2008, 12:56 AM
#2
Re: For Loop
that would only find the forms that were being used. Isn't there also a FORMS collection? Haven't looked, though.
-
July 28th, 2008, 01:13 AM
#3
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 ¿
-
July 28th, 2008, 12:24 PM
#4
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?
-
July 28th, 2008, 12:40 PM
#5
Re: For Loop
You could parse the solution folder, but that'd require a hack to get out of the \bin folder
-
July 28th, 2008, 08:13 PM
#6
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???
-
July 29th, 2008, 12:40 AM
#7
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.
-
July 29th, 2008, 11:49 AM
#8
Re: For Loop
i would prefer a .ini file but how would i go about doing that?
-
July 30th, 2008, 03:13 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|