How to call MDIParent from windows form? - vb.net

I have an MDI form called "MDIParent1", MDI child form "MDIChild1" and I have a windows form called "FrmTest".
Now there is a button called "btnTest" in "MDIChild1" form and here is the click event.
Dim V As New FrmTest
V.MdiParent = MDIParent1
V.Show()
But It couldn't load the "frmTest" form. Is there any another way to do so?
Thanks in advance.

Try This :
Dim V As New FrmTest
V.MdiParent = Me.MdiParent
V.Show()
The above assumes that MDIChild1.MdiParent is already set to MDIParent1
You could also do this :
Dim V As New FrmTest
V.MdiParent = Application.OpenForms("MDIParent1")
V.Show()
To close other forms, iterate through MdiChildren collection :
Dim MyMdiForm as Form = Application.OpenForms("MDIParent1")
For Each Frm As Form In MyMdiForm.MdiChildren
If Frm IsNot V Then
Frm.Close()
End If
Next

Related

vb.net how to open mdichild in mdiparent? using ToolStrip

This is my code, why is it when i open my first form (MdiParent) and when i open the frmCashin form (mdiChild) the child form still exceeds the parents form
Private Sub ToolStrip_Click(sender As Object, e As EventArgs) Handles ToolStrip.Click
Dim formcashin As New frmPOSCashIn
Dim formcashout As New frmPOSTCashout
If CashIn.Selected Then
formcashin.MdiParent = Me.MdiParent
formcashin.Show()
ElseIf Cashout.Selected Then
formcashout.MdiParent = Me.MdiParent
formcashout.Show()
End If
End Sub
just like this in the picture (i use dual monitor), I am new in .net technology,
I can drag the child form to outside the parent form
UPDATE
If CashIn.Selected Then
Dim formcashin As New frmPOSCashIn
Dim formcashout As New frmPOSTCashout
Dim mainform As New frmMain
formcashin.MdiParent = mainform.MdiParent
formcashin.Show()
ElseIf Cashout.Selected Then
Dim formcashin As New frmPOSCashIn
Dim formcashout As New frmPOSTCashout
Dim mainform As New frmMain
formcashout.MdiParent = mainform.MdiParent
formcashout.Show()
End If
Based on the code you posted, I assume that frmMain represents your main form (which is the MDI parent form) and that frmPOSCashIn and frmPOSTCashOut are two MDI child forms. And based on the screenshot, the toolstrip seems to be included in frmMain.
To wrap up the comments by jmcilhinney and Jimi:
You should not explicitly create a mainform variable. You can just use Me, since you are already executing within a frmMain form instance.
Since your frmMain instance itself is the MDI container (its IsMdiContainer property is set to True), it itself should be used as the value for the MdiParent property of your child forms (whose IsMdiContainer properties are set to False). (The MDI architecture allows only one level of hierarchy. You cannot nest MDI container forms as MDI children in an(other) MDI container form. So you should never use the MdiParent property of an MDI container form.)
You can declare your MDI child form variables inside the If-block. And in each block, you should only declare the variable that you actually need.
Something like this:
Private Sub ToolStrip_Click(sender As Object, e As EventArgs) Handles ToolStrip.Click
If CashIn.Selected Then
Dim formcashin As New frmPOSCashIn
formcashin.MdiParent = Me
formcashin.Show()
ElseIf Cashout.Selected Then
Dim formcashout As New frmPOSTCashout
formcashout.MdiParent = Me
formcashout.Show()
End If
End Sub
If you are new to this stuff, I would suggest that you first check out some tutorials/walkthroughs in the Microsoft documentation or other sources on the Internet.

How to reference a variable control on a variable form from another form? Need to get the button.Tag renamed in running program

Here's my problem:
I have a form with a treeview.
That treeview shows all:
other forms in my project as parents
all buttonnames as childs
all buttontags as childs of the buttonnames.
When i select a buttonname in the treeview i have the selection presented as
(textbox1 with the buttonname)
(textbox2 with the buttontag)
(textbox3 with the formname)
I have 1 empty textbox which i want to fill manually, to update the buttontag from the button selected in the treeview.
Either way, all code I have tried ain't updating anything.
This is the code so far, but doesn't seem to work...
My Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim asm = System.Reflection.Assembly.GetExecutingAssembly
Dim myTypes As Type() = asm.GetTypes()
Dim frm As Form
For Each t As Type In myTypes
If t.IsSubclassOf(GetType(System.Windows.Forms.Form)) AndAlso TextBox1.Text = t.Name Then
frm = CType(Activator.CreateInstance(t), Form)
frm.Hide()
Dim thisButtonName As String = TextBox3.Text ' This is the name of the button I'm looking for
Dim thisButtonName2 As String = TextBox2.Text ' this is the new tag name for that button
' Loop all controls in this form
For Each ctrl As Control In Controls
' Is this control a button
If TypeOf (ctrl) Is Button Then
' Is this the correct button
If CType(ctrl, Button).Name = thisButtonName Then
CType(ctrl, Button).Tag = thisButtonName2
End If
End If
Next
End If
Next
TreeView1.Nodes.Clear()
For Each formprop In My.Forms.GetType.GetProperties
Dim node = Me.TreeView1.Nodes.Add(formprop.Name)
Dim form As Form = CType(formprop.GetValue(My.Forms, Nothing), Form)
ControlsTree(node, form.Controls)
Next
End Sub
I think there are two basic problems here:
Your loop For Each ctrl As Control In Controls is not iterating over the controls in the other form object referenced by the frm variable. The Controls property is going to default to this form's set of Controls, not frm.Controls.
It seems like you are trying to change the Tag in the definition of that class at run-time. That is not possible, AFAIK, especially in what you're trying here. Each time you create a new instance of that form object, you are going to get that object initialized as how it was compiled. You can change the Tag values of a running instance of the object, but you can't change the default values of the class without using a technique like dependency injection.

Vb.net Using Public Sub to Open Instance of MDI Child

I am trying lessen the line of code by creating a Public Sub calling in one line. However the form.MdiParent = Me generates error
frmParatemers is a Mdi Child form. frmMain is the MDI parent form.
From frmMain form
Dim MyCtrl As MenuClickOperations
MyCtrl.showChildDialog(New frmParameters)
This is my Class MenuClickOperations
Public Sub showChildDialog(ByVal form As Form)
Dim form2 As Form
For Each form2 In frmMain.MdiChildren
form2.Close()
Next
form.StartPosition = FormStartPosition.CenterScreen
form.MinimizeBox = False
form.MaximizeBox = False
form.MdiParent = Me
form.Show()
End Sub
Help me..Thanks
You should replace form.MdiParent = Me with form.MdiParent = formInstance. You can use frmMain, or the instance of frmMain (if different). If showChildDialog is called from frmMain, you can pass the frmMain instance as a parameter using Me in the call.
in frmMain
Dim MyCtrl As MenuClickOperations
MyCtrl.showChildDialog(New frmParameters, Me)
This is my Class MenuClickOperations
Public Sub showChildDialog(ByVal form As Form, ByVal Itself As Control)
Dim form2 As Form
For Each form2 In frmMain.MdiChildren
form2.Close()
Next
form.StartPosition = FormStartPosition.CenterScreen
form.MinimizeBox = False
form.MaximizeBox = False
form.MdiParent = Itself
form.Show()
End Sub

How to ensure only one instance of a child form is created by a parent mdi form in VB.NET

I have a button on a parent form that can create new Child forms.
However, I don't want more than one instance of each form to be created. I tried putting a public boolean on the parent MDI form:
Dim ChildForm As Boolean = False
And at the point where the child form is created:
ChildFormThere = True
And in the child form's "Leave" event, I thought I could do this:
Me.MdiParent.ChildFormThere = False
But it doesn't recognize the ChildFormThere variable... How can this be done then?
How about something like this. The idea is that if the form has already been created you switch to it, otherwise create one. This assumes you are setting mdiParent correctly when creating the child forms. This code would need to be run on the mdiParent, or have a reference to it to access the MdiChildren property.
For Each f In Me.MdiChildren
If TypeOf (f) Is Form1 Then
f.Show()
Exit Sub
End If
Next
Dim frm As New Form1
frm.Show()
Perhaps instead of:
dim ChildFormThere as Boolean = False ' Or True
You could do:
dim ChildForm as New ChildFormClass
' On Create Button Click:
ChildForm.Visible = True
This way it's always the same instance, so you simple have to manage if it is visible or not.

VB.NET z-order (ordering forms with ownership)

I have situation when opening several "owned" forms from my form.
Dim f As New ownedform
With f
.Owner = Me
.Show()
End With
That works OK.
But I have a question.
Can I somehow by pressing some button on "owner" or "owned" form to get "owner" form to become a toplevel and pop over "owned" forms?
I try a simplest with ".BringToFront" but that don't work.
BringToFront only for controls to set z-order .
but you can make that . first write this code in form1
Me.Hide()
Dim frm As New form2
frm .ShowDialog()
thien write this code in form2
Me.Hide()
Dim frm As New form1
frm .ShowDialog()