How to close windows opened by webview2 in VB.Net - vb.net

I have a WinForms application which uses webview2 to embed an Edge browser window in my application. It works great, but when the user clicks a link within it, it opens an extra "child" window. That's fine too, but if that child window is left open, it causes the web application to timeout, so I'd like to close them automatically after a time.
I tried the common solutions to close open forms, but they only get the top level forms, and never these child ones.
I've tried this code to close them all, but the dynamically opened forms are not closed:
Public Sub CloseAllForms()
Dim FormList As ArrayList = New ArrayList()
'Add all opened forms into a Collection.
For Each Frm As Form In Application.OpenForms
FormList.Add(Frm.Name)
Next
'Now Close the forms
For Each FormName As String In FormList
If FormName <> "frmMain" Then Application.OpenForms(FormName).Close()
Next
End Sub
How can I reference these windows so that I can close them on a schedule?

Related

How to stop VB.Net FixedToolWindows getting maximized

I have an application with several FixedToolWindow forms and one Sizable window form inside MDIParent form which runs maximized. If i run FixedToolWindows while Sizable window is unloaded, FixedToolWindows runs with correct sizes.
But if I try to run a FixedToolWindow while the Sizable window running in maximized mode, all the FixedToolWindows gets maximized too.
Following is the code, i use to load forms,
If Application.OpenForms().OfType(Of frmFrontEndLED).Any Then
MessageBox.Show("Already opened")
Else
Dim frmFendLED As New frmFrontEndLED()
'Set the Parent Form of the Child window.
frmFendLED.MdiParent = Me
'Display the New form.
frmFendLED.TopMost = True
frmFendLED.Show()
End If
What am i doing wrong?. How can i correct this?

How to iterate through open forms in a VSTO add-in?

I have a VSTO add-in for MS Project that opens forms where the data is related to the specific project file that was active when the form was open. It is possible to open one form related to one project file, while having another different form open that is related to a second open project file.
When I close a project file I would like to check each open form, and close it if the forms base project ID equals the project ID of the project file that is closing. How do I access the open forms collection of the vsto application (or do something equivalent)? The Application.OpenForms object doesn't appear to exist in the vsto world.
Use a Dictionary object to store an instance of the form along with the name of the file. Every time a form is created, add it to the dictionary and when the project is closed, search the dictionary to close the appropriate copy.
Friend ProjectForms As New Dictionary(Of String, MyForm)
Friend Sub ShowForm()
Dim f As New MyForm
Try
ProjectForms.Add(ProjApp.ActiveProject.Name, f)
Catch AlreadyInTheDictionary As Exception
' do nothing, it's already in the dictionary
End Try
f.Show()
End Sub
Put this in the module with the application events (typically ThisAddIn).
Private Sub Application_ProjectBeforeClose(pj As MSProject.Project, ByRef Cancel As Boolean) Handles Application.ProjectBeforeClose
ProjectForms(pj.Name).Close()
End Sub

Closing a Dialog Form and Open another form

I've got (what I believe to be) a weird issue with closing a form and opening another.
I have a button on a particular form (that is opened using the .ShowDialog if that makes any difference), when the button is clicked, the following code runs:
Me.Close()
LC.ShowDialog()
I would expect that the form containing the button should close and the LC form should open as a dialog form. What actually happens is that the Me form stays open and the LC form appears behind it with main focus.
Why would this be?
UPDATE 1
Just to clarify the set up of the forms:
Form1 opens the me form as a Dialog (Where Form1 is the main form that is launched on startup)
The me form opens the LC form and should close in the process
Ok it seems you might have a conception issue...
Let's get this through, if you call me.close(), you are asking your form to terminate itself, and to kill all forms he generated.
However, it is not direct, a windows Message is posted to your application that will be treated whenever you function is done.
Then right after that, you create a new form and you say you want to wait for it to close to continue.
I don't know what your purpose is but you have a few solutions :
If you want to go back to your main form (Form1) when done with LC :
Me.Hide()
LC.ShowDialog()
=>Your code will pick up here when LC will be closed
Me.Show()
If you don't want your Form1 to open ever again, then don't make it your starting form, or start working in the Sub Main()
make the form1 a flowmanager of the forms. I mean, form1 should opens the lc and close the me.
You must call both Dialog Forms from a parent form.
You may do something like this:
PARENT FORM
FirstDialog.Showdialog
(the app will open this dialog and wait it close)
SecondDialog.Showdialog
Doing this way the second dialog will open ONLY when the first one be closed.
You cannot start another dialog from a dialog being closed.
Good luck

How to properly close form1 after showing form2

I have a script editor that I'm using to code through the object model of software my company uses. It has a login form to make sure that you are able to access the system and a main form to pull the script from the object and edit using ScintillaNET.
The issue that I am having is when I pass the object to the main form and leave the login form open it runs smoothly but I want to close the login form after the main form is open. When I do this it stops immediately after opening the main form.
Here is a sample of my code. You can see for now that I have commented out the line Me.Close() as that seems to the issue.
' Close this form, show the main form and pass the M3 object to it
Dim f As FormMain = New FormMain
' Pass the M3 object to next form
f.M3System = M3System
f.Show()
'Me.Close()
any help would be awesome!!!
This is a built-in feature for VB.NET. Use Project + Properties, Application tab.
Change the Shutdown mode setting to "When last form closes".

showdialog.dispose focus to another open application

I have a problem with showdialog when the showdialog form is being disposed. The Application focus to another program opened in taskbar. if nothing is opened then it focused to desktop.
This will happen when the dialog is getting disposed too early. All of the windows are disabled by the dialog so Windows can't keep the focus on any of them and has to pick the window of another app. Make sure you use the standard pattern with the Using keyword so the dialog object is disposed after it closes:
Using dlg As New FooDialog
If dlg.ShowDialog(Me) = DialogResult.OK Then
'' Use dlg properties
''
End If
End Using
its as simple as this:
form2.showdialog()
me.bringtofront()