הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Monday, April 21, 2008 11:37 PM
Hi Everyone,
I have a main and some sub forms. On the main form I have a button for a user to click once they want to log off. Once they do this all buttons on the form are disabled apart from two. one which would enable the user to log in again and one to exit.
When a user has a subform open but decides to go to the main form and log off I would like to the program to close all forms except main form or remind the user that they must first close the subform before they can log off. Then logoff. Do you have any ideas out there?
Thanks
Brandville
All replies (7)
Monday, April 21, 2008 11:41 PM ✅Answered
Sure, use the Application.OpenForms property to iterate through the open forms. Then check to see which is your main form, and if it is NOT the main form, Close the form.
for each currentForm as Form in Application.OpenForms
if currentForm.Name <> "myMainForm" then
currentForm.Close()
end if
next
Does this help?
Tuesday, April 22, 2008 12:32 AM ✅Answered
ah yes of course, forgot that it would remove it from the collection. OK, instead, try this.
Dim formNames as List(Of string)
for each currentForm as Form in Application.OpenForms
if currentForm.Name <> "formName" then
formNames.Add(currentForm.Name)
end if
next
for each currentFormName as string in formNames
Application.OpenForms(currentFormName).Close()
next
Does this work for you?
Tuesday, April 22, 2008 12:47 AM ✅Answered
Hi
Thanks for your reply. Its almost working apart from the line below,
'formNames.Add(currentForm.Name)
"formNames." has a green underline. with the following information.
"the variable formNames is used before it has been assigned a value. A null reference exception could result at runtime."
The variable just requires a value. I have tried "" empty string but that cannot be held in a collection.
What are your thoughts?
Thanks
Brandville
Tuesday, April 22, 2008 12:49 AM ✅Answered
my apologies, my keyboard seems to miss letters and gives me typos....urgh
the correct code:
Dim formNames as new List(Of string)
for each currentForm as Form in Application.OpenForms
if currentForm.Name <> "formName" then
formNames.Add(currentForm.Name)
end if
next
for each currentFormName as string in formNames
Application.OpenForms(currentFormName).Close()
next
Tuesday, April 22, 2008 1:31 AM ✅Answered
Thanks alot. Its working now.
Brandville
Monday, April 21, 2008 11:47 PM
Hi ahmedilyas
Thanks for your super fast reply. I will try it and let you know the outcome but i think its exactly what i am looking for.
Thanks
Tuesday, April 22, 2008 12:19 AM
Hi
I have tried the code and it does not work properly. I have tried it with three forms open i.e. Main form and two subforms. It does close the first subform and when it loops back it breaks at Next. It throws the following exception.
Note : I have already changed the mainform name to reflect myform name. the rest of the code remains unchanged.
System.InvalidOperationException was unhandled
Message="Collection was modified; enumeration operation may not execute."
Do you what i do to debug it.
Thanks
Brandville