Why MDI form children reorder after closing last child? - vb.net

I have a windows forms application project in VB with a MdiContainer called "FMenuMdiParent" and I show every form child this way:
With form1
.MdiParent = FMenuMdiParent
.Show()
End With
All works fine but when I have more than 2 forms and close any of them the first form always get focus and I need they respect the order one each other.
I tried with the next code and it works. But now I can't swap the focus between forms:
Private Sub FMenu_MdiChildActivate(sender As Object, e As EventArgs) Handles MyBase.MdiChildActivate
Try
Me.MdiChildren(Me.MdiChildren.Length - 1).Activate()
Catch ex As Exception
LogError.RegistrarErrorEF(SYS, FORM_PAGINA, Reflection.MethodBase.GetCurrentMethod().Name, ex.Message, Sesion.Login, Programador.Juan_Erroa)
End Try
End Sub

Related

Parent form sometimes does not close (Only in Windows 10)

My main form is frmInvoice. This sub is located inside frmInvoice.
This is one of the Subs that sometimes causes frmDark to not close. frmLookup does not display when this happens. frmDark just stays there covering frmInvoice. It's like it doesn't reach the call to frm.ShowDialog(frmDark), cause when I press the lookup key, it displays the frmLookup, but upon closing frmLookup, frmDark is still there.
No exception is being raised.
Note that this only happens in Windows 10. In Windows 8/7, this never happened. What am I missing?
This happens at different times. Sometimes I could press the lookup key for 20 times and it will display fine. Sometimes, after 1 press of the lookup key and this happens.
Private Sub ItemLookup()
Try
Using frmDark As New Form
With frmDark
.ShowInTaskbar = False
.Icon = Me.Icon
.FormBorderStyle = Windows.Forms.FormBorderStyle.None
.BackColor = Color.Black
.Opacity = 0.95
.WindowState = FormWindowState.Maximized
.Show(Me)
Using frm As New frmLookup
With frm
.Icon = Me.Icon
.ShowDialog(frmDark)
frmDark.Close()
If .DialogResult = Windows.Forms.DialogResult.OK Then
' Do stuff here
End If
End With
End Using
End With
End Using
Catch ex As Exception
ErrMsg(ex)
End Try
End Sub
UPDATE: I'm using .Net Framework 4.8
Thanks
I would suggest rearranging the code like so:
Dim lookupResult As DialogResult
Using frmDark As New Form With {.ShowInTaskbar = False,
.Icon = Me.Icon,
...}
frmDark.Show(Me)
Using frm As New frmLookup With {.Icon = Me.Icon}
lookupResult = frm.ShowDialog(frmDark)
End Using
End Using
If lookupResult = DialogResult.OK Then
'...
End If
Because that code exits the Using block that created frmDark, there should be no way that it can't close.
Also, instead of using a vanilla Form and configuring it on demand, I would suggest that you create a dedicated form type to use as the overlay in that scenario. You can then get rid of all the property assignments.
Having a dedicated overlay form would also allow you to reconfigure things significantly and, in my opinion, better. The overlay form could have a property of type Form. You main form could then create a frmLookup instance and assign it to that property, than call ShowDialog on the overlay form. In the Shown event handler of the overlay form, it could then call ShowDialog on the form in that property. When that call returns, it could assign the result to its own DialogResult property and close itself. The main form would then just get the result from calling ShowDialog on the overlay. That might look like this:
Public Class OverlayForm
Public Property DialogueForm As Form
Private Sub OverlayForm_Shown(sender As Object, e As EventArgs) Handles Me.Shown
DialogResult = DialogueForm.ShowDialog()
End Sub
End Class
and this:
Public Class MainForm
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using dialogue As New DialogueForm,
overlay As New OverlayForm With {.DialogueForm = dialogue}
If overlay.ShowDialog() = DialogResult.OK Then
MessageBox.Show("OK")
End If
End Using
End Sub
End Class

vb.net cannot access a disposed object when opening form

I have a program that we use to run different reports. Based on the menu option chosen, I open the same form that lists the reports based on the menu option.
(There are also different options and functionalities in the program, not just one form).
When clicking a menu option, I have the following bit of code
Private Sub ReportsToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles ReportsToolStripMenuItem1.Click
FormLocation = "F_Legal"
FormName = "Legal"
PrepareForm(F_Select_Report)
End Sub 'ReportsToolStripMenuItem1_Click
Where F_Select_Report the form is that is opened.
Private Sub PrepareForm(formName As Form)
Cursor = Cursors.WaitCursor
For Each Form In Me.MdiChildren
Form.Close()
Next
formName.MdiParent = Me
formName.Height = Me.Height
formName.Width = Me.Width
formName.Show()
Cursor = Cursors.Arrow
End Sub 'PrepareForm
This bit is called, closing all other opened forms, and then open the form that is called.
This works fine on the first time I try and open a form, but on the second try, I get an error message saying
Cannot access a disposed object.
And then on the third try, it opens the form again.
How would I fix this up?
Thanks a lot
Form.Close implicitly calls Form.Dispose
So if the formName is an MdiChild it gets disposed in the For Each loop.
Then, on the next line your code attempts to assign to its MdiParent property and there the error occurs.
So you need to skip it when closing MDI children like this:
For Each Form In Me.MdiChildren
If Not Form Is formName Then Form.Close
Next
Given your code I think it is better to close the children before showing the F_Select_Report form. I.e. move the For Each loop as is to the top of the ReportsToolStripMenuItem1_Click handler.
Not sure if it is the best/nicest solution, but found a solution to this.
Instead of 1 Sub that does both closes all open forms, and then opens the new one, I split it out over 2 Subs.
Close all open ones
Private Sub CloseAllForms()
For Each Form In Me.MdiChildren
Form.Close()
Next
End Sub 'CloseAllForms
And then open the new form
Private Sub PrepareForm(formName As Form)
Cursor = Cursors.WaitCursor
Try
formName.MdiParent = Me
formName.Height = Me.Height
formName.Width = Me.Width
formName.Show()
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Cursor = Cursors.Arrow
End Sub 'PrepareForm
Now it works as needed.

Working with passed data between forms

Currently in my windows form, I have a few WinForms to work with. One WinForm acts as a main menu and is supposed to call another form as a secondary window on its own.
Private Sub btnMainGame_Click(sender As Object, e As EventArgs) Handles btnMainGame.Click
' This is the button to call up the main game controller. So simply hide this form aned then open the new form.
Dim frmController As New frmControllerScreen
frmController.Show()
Me.Hide() ' Happens on .Close as well
End Sub
The above code invokes another WinForm which is used to handle more options. When the user clicks on a particular button, a sub form is created again.
Dim OpenNewGameWindow As New frmGameConfig
OpenNewGameWindow.ShowDialog(Me)
Me.DialogResult = DialogResult.None ' Used to prevent the subform from closing the main form when it catches a dialog result.
Now in the frmGameConfig, the program is supposed to take data and pass it back to the form that called it.
Private Sub btnNewGameStartGame_Click(sender As Object, e As EventArgs) Handles btnNewGameStartGame.Click
' ... Skipped code...
frmControllerScreen.MasterQuestionList = QuestionList
frmControllerScreen.blnBankedTime = cbBankedTime.Checked
' ... Skipped code...
End Sub
However, when the frmController tries to reference MasterQuestionList... it returns a nullreference error as if it was not set.
Here's where things get funny...
When I made this code, frmControllerScreen was actually the startup form. Now when I change this form back to frmMainMenu, I get NullReference errors constantly.
My question: How am I supposed to pass information from one form to the next form if it was instantiated from a parent form. (Note I even moved the declartion to Public as a "module-wide" variable... and nothing happens but the same result.) The same error happens even if I go ahead and declare frmController.MasterQuestionList as well.
Instead of trying to pass data back from the called form to the caller, you can reference the called form's controls from the calling code after .ShowDialog.
Dim OpenNewGameWindow As New frmGameConfig
If OpenNewGameWindow.ShowDialog() Then
MasterQuestionList = OpenNewGameWindow.QuestionList
blnBankedTime = OpenNewGameWindow.cbBankedTime.Checked
End If
In OpenGameWindow button click:
Private Sub btnNewGameStartGame_Click(sender As Object, e As EventArgs) Handles btnNewGameStartGame.Click
Me.DialogResult = True
End Sub

Closing Window's from upon switching to another or vice versa

I wrote a Window Application which has two different parent-forms (form1, and form2). Each form has several child forms. After I login it opens form1. Now I have a button (called switch to form2) on form1, which switch to form2. Now I need to close form1 after opening form2. I need to same thing from form2 to form1.
What would be the best way to handle this.
I tried something like below by adding this code to close form under form load of each forms, but I am getting the following exception.
A first chance exception of type 'System.InvalidOperationException' occurred in mscorlib.dll
Additional information: Collection was modified; enumeration operation may not execute.
Can you please suggest me to handle this problem?
Form 1
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
For Each frm As Form In Application.OpenForms
If frm.Name.ToLower = "form2" Then
frm.Close()
End If
Next
End Sub
Form 2
Private Sub Form2_Load(sender As Object, e As System.EventArgs) Handles Me.Load
For Each frm As Form In Application.OpenForms
If frm.Name.ToLower = "form1" Then
frm.Close()
End If
Next
End Sub
I suppose you are responsible for creating your forms.
If so, I would overload the constructor of each of the forms to take a parameter of the type of the other form and close it there.
For your Form1 it would be :
Public Sub New(form2 As Form2)
InitializeComponent()
'and the rest of your initialization code
If form2 IsNot Nothing Then
form2.Close()
End If
End Sub
Vice versa for the constructor of Form2.
It doesn't even have to be so specialized like above. You could always generalize it to take an object of the type Form as a parameter.
why not use Form1.Hide()?
that way you can still access the other form while it is not visible.
I hope this helps.

Open Form as ShowDialog But Close Initation Form

Is there any way to do the following, other than hiding then closing the hidden form later?
Mainform opens SecondForm as show dialog, i need to Open ThirdForm from SecondForm while closing SecondForm while keeping third form acting as "showdialog" on the MainForm?
When you show SecondForm(), pass in MainForm() as the owner to ShowDialog():
Public Class MainForm
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sf As New SecondForm
If sf.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
' ... do some processing in here ...
End If
End Sub
End Class
Now, in SecondForm(), you can then set the owner of ThirdForm() to that of SecondForm():
Public Class SecondForm
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
Dim tf As New ThirdForm
tf.ShowDialog(Me.Owner)
Me.DialogResult = Windows.Forms.DialogResult.OK
End Sub
End Class
You could simply open the third form from the main form as soon as the second form returns a dialog result
You also might want to look at MDI this gives you more control over what the user can and can't do.
After trying Idle_mind suggestion it was still giving me problems going back and forth between forms continously showing them as .showdialog. I solved my problem just like tinstaafl suggested. I wish i would have got his message before a couple hours of trying different methods before coming up with this one.
When i close each form, i set a boolean flag in the main form. then i call a sub that is in the main form to show the next form as showdialog from the main form. i use the flag which triggers logic in the form im loading whether or not to bind data from datatable so i can edit it.
sorry with all those forms i know it gets confusing. to sum it up, close the dialog form (me.close), set flag so calling code knows what to do once the showdialog code is satisfied.