vb.net Closing multiple form and open another form - vb.net

I have 2 forms open, my current form is in dialog box. Now I want to close that 2 forms and open another form.
Dim frmStg2 As New frmStageTwo
Dim frmStg1 As New frmStageOne
frmStg2.Show()
Me.Close()
frmStg1.Close()
But this code seems not working the frmStageOne is still open and showing frmStageTwo but I can't view that form.
This is what happen

You are trying to close a form instance that has not yet been opened.
When you use the New keyword you are declaring a new instance of an object (a Form in this case) which has it's own version of the base class's properties and fields.
In order to close an already existing instance of the form you can loop through the Application.OpenForms property.
For Each frm As Form In Application.OpenForms
If frm.GetType() Is GetType(frmStageOne) Then
frm.Close()
End If
Next
You could actually make it a method:
Public Sub CloseFormsOfType(ByVal TargetForm As Form)
For Each frm As Form In Application.OpenForms
If frm.GetType() Is TargetForm.GetType() Then
frm.Close()
End If
Next
End Sub
...and just call:
CloseFormsOfType(frmStageOne)

Related

Tabbing on Form disappears when using Control.FromHandle

VB2010: I have a form that I display within a mapping application that supports their framework through .NET. So when the user clicks a button on a custom extension then my form will be displayed.
Private frm As New frmDesigner 'since this is a floating form
Public Overrides Sub OnClick()
Try
''display the form normally
'Dim frm As New frmDesigner
'frm.ShowDialog()
'display the modeless form and the form will always be on top of the main app.
If frm.IsDisposed Then frm = New frmDesigner 'To handle user closing form
If frm.Visible Then
frm.BringToFront()
Else
Dim appCtr As Control = Control.FromHandle(CType(MyApp.hWnd, IntPtr))
frm.Show(appCtr)
End If
Catch ex As Exception
DisplayException(ex)
End Try
End Sub
This works great and I have been using this approach for a couple years now. The problem that I am seeing now is that the tabbing through controls on the form does not work. Once the form appears I press on the Tab button and the cursor does not tab through the controls. I can set focus on a textbox through the mouse and then attempt to tab over to the next control but that doesn't work either.
If I display the form normally through frm.ShowDialog then the tabbing does work. Is there something I am missing here to make the tabbing work properly?

VB.NET Calling forms by Variable

I'm playing around in vb.net and would like to create a menu for my application. The application has an Main which is the MDI Parent and then has the sub forms that open up as the user browses the menu structure. I would like to track which form the user came from so that when they close their current form, it opens up the previous form they had open.
What I have so far is a public variable "FormTracking" which is a list of Form. The open form function works fine, but when they close the form and it calls the "OpenPreviousForm" function it fails as the previous form has been closed/disposed. How can i create a new instance of the form as calling "NEW" doesn't seem to work
Public FormTracking As New List(Of Form)
Public Sub OpenForm(theNewForm As Object)
'First Declare the new form
Dim f As Form = DirectCast(theNewForm, Form)
'Set the Forms parent
f.MdiParent = frmMain2
'Add the form to the tracking
FormTracking.Add(theNewForm)
'Show the form
f.Show()
End Sub
Public Sub OpenPreviousForm()
If modMenu.FormTracking.Count > 1 Then
Dim f As New Form
f = modMenu.FormTracking(modMenu.FormTracking.Count - 2)
'Set the Forms parent
f.MdiParent = frmMain2
'Remove the last item
modMenu.FormTracking.RemoveAt(modMenu.FormTracking.Count - 1)
'Show the form
f.Show()
Else
MessageBox.Show("Whoops, run out of forms.... ", "modMenu - OpenPreviousForm")
End If
End Sub
When opening a form it it calls:
modMenu.OpenForm(menupage_1)
me.close
And when closing a form, i try to call
modMenu.OpenPreviousForm()
The error i am getting when calling the above code is
"An unhandled exception of type 'System.ObjectDisposedExeption' occured in System.Windows.Forms.dll
Additional Information: Cannot access a disposed object
I understand i need to create the object again, but not sure how to using the functionality as above. Alternatively, if someone can present a better way of doing an application menu, please feel free to let me know.
Cheers
Use Me.Hide instead of Me.Close so you do not dispose the form.

Closing winform from button on another winform. Form will not close

I have 2 forms on my application. I have the main form that opens TopMost, CenterScreen and Maximized. Then I have another form on this screen that pops open when I press a button. That second screen has a button that navigates to another screen, so when I press that button the second form closes, and the main form is suppose to close as well and the selected sheet open up.
However, the second screen closes fine, but my main screen remains open and active, while the called sheet opens but does not enable. I track down what was happening and the issue is that form all code runs, but the main screen does not seem to want to close. Here is my code:
Private Sub btnOpenDashboard_Click(sender As Object, e As EventArgs) Handles btnOpenDashboard.Click
Dim welcomeForm As New frmWelcomePage
If lblReportTitle.Text = "Employee Dashboard" Then
Me.Close() 'This works
welcomeForm.Close() 'This one remains open and active
Globals.dsbEmployeeBoard.Select() 'This one opens but is not enabled
End If
End Sub
I assume from your description that you already have a welcome form created and displayed before the form with the button is displayed.
This line of code:
"Dim welcomeForm As New frmWelcomePage"
is creating a new copy of the Welcome Page and closing it.
Instead of creating a new one, you need to reference the original one that is open.
If I recall correctly, you should be able to just remove that line and use frmWelcomPage.Close.
You need to pass a reference of your first form (Form1) to the second form (Form2), so that in the second form you can close the first form, like this:
Public Class Form2 Inherits Form
Private _form1 As Form1
Public Sub New(form1 As Form1)
Me.Form1 = form1
End Sub
End Class
Private Sub btnOpenDashboard_Click(sender As Object, e As EventArgs) Handles btnOpenDashboard.Click
If lblReportTitle.Text = "Employee Dashboard" Then
_form1.Close()
End If
End Sub
Then when you instantiate Form2, you would pass a reference to Form1, like this:
Dim form2 As New Form2(Me)
Note: Me is the instance of Form1.

Showing another form in VB

I want to make a program with many forms in VB, I need to make a button that opens another form.
form2.open()
Do not use the default instance of the form(as has been shown in the other answers), instead
Dim foo As New Form2
foo.Show()
'or
'foo.ShowDialog()
If you want to show multiple instances of the same form this
Form2.Show()
Form2.Show()
will not work.
This will
Dim foo As New Form2
foo.Show()
Dim bar As New Form2
bar.Show()
Simply the code will be
form2.show()
You can open a form inside yours as a MDI child, or you can "open" your new form in a new window with this methods:
form2.Show()
form2.ShowDialog()
Read here and here about them.
As SysDragon already mentioned,
.Show()
.ShowDialog(owner)
are the methods you can use.
If the calling Form should be frozen until the new opened one is closed again, then you should use ShowDialog.

Passing Values from modal form to parent form vb.net

I am trying to pass information to parent form from modal form in vb.net winforms application.
1.) I created a copy of a form and displayed it using following code.
dim f=new frmParent()
f.show()
2.) Depending on conditions a button on frmParent opens a modal child form and asks for some informations. I used following code for that:
dim f = new ChildForm()
f.showDialog()
Both code works fine. When user press saves in child form i need to close childForm and use the user types values in parent form. I know how to close the childform but not sure how to pass info from childform to parent form.
Have a public property on your childForm
Public Property MyData As MyType
Then when you show the form you can do
dim f as new ChildForm()
If f.showDialog = DialogResult.OK Then
Data = f.MyData()
End if
If you need to allow them to be able to edit that data again then you might also want to consider passing in the Data to the constructor of the dialog.