I found the following code from stackoverflow
It works correctly when I execute the MDIParent form as the first form in the project
For Each f As Form In Application.OpenForms
If TypeOf f Is Form1 Then
f.Activate()
Return
End If
Next
Dim myChild As New Form1
myChild.MdiParent = Me
myChild.Show()
I need to open the LOGIN form as the first form and open MDIParent.
If I open the login form as the first form the above code it not working.. Need a solution
I added this code to a button control
Set your Login form as the start form for your project.
On your button, Show the MDI Form (and close the Login form if you wish)
MDI_Main.Show()
Me.Dispose()
Then to open your MDI Child forms, use the following code:
frmChild.MdiParent = Me
frmChild.Show()
This is all the code you should need.
Related
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?
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)
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.
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.
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.