how change the image on picturebox1 in childform on vb.net 2010 my app is a mdiform and child, sub on module (for mdiform,property changing or getting codes are include mdiform) is gonna change the image property of the picturebox1 in the child form
The Me.MdiChildren property in the MdiContainer contains the references to all the children.
It is a 0-based indexed array. So the first child would be Dim f = Me.MdiChildren(0)
To create and add a new form, you can use this type of process in the MDI parent.
Private Sub AddNewForm()
Dim dlg As New ChildForm
dlg.WindowState = FormWindowState.Maximized
dlg.MdiParent = Me
dlg.Text = $"Child {Me.MdiChildren.Length}"
dlg.Show()
End Sub
Related
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.
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.
I would like to set the background color for a certain type of control on all child forms that open. I have an MdiParent form that is used to open the other forms within itself. I don't want to add code to each child form as this would be very extensive. This would be used as a theme feature for the application so I would like to have it automatically change the background colors based on logic in the main form. Is there something like a global event that could trigger for all Form.Load events?
So far I have created an event in the Parent form but it doesn't work for nested controls
Private Sub frmMain_MdiChildActivate(sender As Object, e As EventArgs) Handles Me.MdiChildActivate
Dim ParentControl As frmMain = sender
Dim ChildControl = ParentControl.ActiveControl
If ChildControl IsNot Nothing Then
For Each FormControl As Control In ChildControl.Controls
If FormControl.GetType = GetType(GroupBox) Then
RemoveHandler FormControl.Paint, AddressOf PaintBorderlessGroupbox
AddHandler FormControl.Paint, AddressOf PaintBorderlessGroupbox
End If
Next
End If
End Sub
I was able to accomplish this by using Form.MdiChildActivate and adding the event to the appropriate controls based on the Event and EventHandler.
Private Sub frmMain_MdiChildActivate(sender As Object, e As EventArgs) Handles Me.MdiChildActivate
Dim ParentForm As frmMain = sender
Dim ChildForm = ParentForm.ActiveMdiChild
Dim EventName = "Paint"
Dim EventHandlerName = "PaintBorderlessGroupBox"
If ChildForm IsNot Nothing Then
AddEventToControls(ChildForm, GetType(GroupBox), EventName, EventHandlerName)
End If
End Sub
Private Sub AddEventToControls(Control As Control, ControlType As Type, ControlEventName As String, ControlEventMethod As String)
For Each ChildControl In Control.Controls
If ChildControl.GetType = ControlType Then
If ChildControl.Controls.Count > 0 Then
AddEventToControls(ChildControl, ControlType, ControlEventName, ControlEventMethod)
End If
Dim EventMethod = Me.GetType().GetMethod(ControlEventMethod, BindingFlags.NonPublic Or BindingFlags.Instance)
Dim ControlEvent As EventInfo = ControlType.GetEvent(ControlEventName)
Dim del = [Delegate].CreateDelegate(ControlEvent.EventHandlerType, Me, EventMethod)
ControlEvent.RemoveEventHandler(ChildControl, del)
ControlEvent.AddEventHandler(ChildControl, del)
End If
Next
End Sub
The call to AddEventToControls() assigns the handler to the Control and any child controls that it would also apply to. In this case I am setting the Control.Paint event to paint a GroupBox a specific way. This may not be the cleanest method to accomplish this but I was able to create a "Global Event" for all child forms without ever touching the code on each form.
In your parent form, keep a collection of all Child Forms that have been activated. You can then traverse that collection and change the relevant control property for each one.
For Each ChildForm in MyCollection
ChildForm.TextBox.BackColor = Red
Next
Of course:
The ChildForm control has to be accessible by the parent form
The ChildForm has to still exist (i.e. not been closed in the mean
time)
You can't check for ChildForm closure because you are not adding any
code to the ChildForm to signal such an event.
You have to handle the exceptions when you try to change a form that
has been closed.
Much easier to include a method in your ChildForm design for ChangeColour(), whether that method is fired by event or direct call is your design decision. And to include a method to tell the parent form when a ChildForm dies so that it stops looking for it.
How do we arrange child forms in a parent MDI window? I'm able to call and display a child form from a menu on the parent, but the child pops up outside the parent - I want it to actually be inside the parent. I've checked in C# and VB.Net solutions but they all say pretty much the same, i.e. try to access LayoutMDI, such as here:
http://msdn.microsoft.com/en-us/library/x9fhk181.aspx
The problem is, where do I access this? When I'm in the code of my MDI parent, Me.LayoutMdi is not recognized. In which part of the application do I put the Me.LayoutMDI code?
Edit
The Me.LayoutMDI code worked in the parent after all. I'd been trying for a while but don't know where I was going wrong.
However, the child continues to pop up out of the parent. Here's an image of how that happens. The broader form in the back is the parent, and the one with the gridview and two buttons is the new child that popped up. I want it to pop up "Docked" within the parent.
If your form is "outside" the MDI parent, then you most likely didn't set the MdiParent property:
Dim f As New Form
f.MdiParent = Me
f.Show()
Me, in this example, is a form that has IsMdiContainer = True so that it can host child forms.
For re-arranging the child form layout, you just call the method from your MdiContainer form:
Me.LayoutMdi(MdiLayout.Cascade)
The MdiLayout enum also has tiling and arrange icons values.
Dont set MDI Child property from MDIForm
In Chileform Load event give the below code
Dim l As Single = (MDIForm1.ClientSize.Width - Me.Width) / 2
Dim t As Single = ((MDIForm1.ClientSize.Height - Me.Height) / 2) - 30
Me.SetBounds(l, t, Me.Width, Me.Height)
Me.MdiParent = MDIForm1
end
try this code
You will want to set the MdiParent property of your new form to the name of the MDI parent form, as follows:
dim form as new yourform
form.show()
form.MdiParent = nameParent
Private Sub FileMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) handles FileMenu.Click
Form1.MdiParent = Me
Form1.Dock = DockStyle.Fill
Form1.Show()
End Sub
Try adding a button on mdi parent and add this code' to set your mdi child inside the mdi parent. change the yourchildformname to your MDI Child's form name and see if this works.
Dim NewMDIChild As New yourchildformname()
'Set the Parent Form of the Child window.
NewMDIChild.MdiParent = Me
'Display the new form.
NewMDIChild.Show()
Try the code below and.....
1 - change the name of the MENU as in my sample the menuitem was called 'Form7ToolStripMenuItem_Click'
2 - make SURE to paste it into an MDIFORM and not just a basic FORM
Then let me know if the CHILD form still shows OUTSIDE the parent form
Private Sub Form7ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Form7ToolStripMenuItem.Click
Dim NewForm As System.Windows.Forms.Form
NewForm = New System.Windows.Forms.Form
'USE THE NEXT LINE - to add an existing CUSTOM form you already have
'NewForm = Form7
NewForm.Width = 400
NewForm.Height = 250
NewForm.MdiParent = Me
NewForm.Text = "CAPTION"
NewForm.Show()
DockChildForm(NewForm, "left") 'dock left
'DockChildForm(NewForm, "right") 'dock right
'DockChildForm(NewForm, "top") 'dock top
'DockChildForm(NewForm, "bottom") 'doc bottom
'DockChildForm(NewForm, "full") 'fill the client area (maximise the child INSIDE the parent)
'DockChildForm(NewForm, "Anything-Else") 'center the form
End Sub
Private Sub DockChildForm(ByRef Form2Dock As Form, ByVal Position As String)
Dim XYpoint As Point
Select Case Position
Case "left"
Form2Dock.Dock = DockStyle.Left
Case "top"
Form2Dock.Dock = DockStyle.Top
Case "right"
Form2Dock.Dock = DockStyle.Right
Case "bottom"
Form2Dock.Dock = DockStyle.Bottom
Case "full"
Form2Dock.Dock = DockStyle.Fill
Case Else
XYpoint = New Point
XYpoint.X = ((Me.ClientSize.Width - Form2Dock.Width) / 2)
XYpoint.Y = ((Me.ClientSize.Height - Form2Dock.Height) / 2)
Form2Dock.Location = XYpoint
End Select
End Sub
Try Making the Child Form's StartPosition Property set to Center Parent. This you can select from the form Properties.
See this page for the solution!
https://msdn.microsoft.com/en-us/library/7aw8zc76(v=vs.110).aspx
I was able to implement the Child form inside the parent.
In the Example below Form2 should change to the name of your child form.
NewMDIChild.MdiParent=me is the main form since the control that opens (shows) the child form is the parent or Me.
NewMDIChild.Show() is your child form since you associated your child form with Dim NewMDIChild As New Form2()
Protected Sub MDIChildNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
Dim NewMDIChild As New Form2()
'Set the Parent Form of the Child window.
NewMDIChild.MdiParent = Me
'Display the new form.
NewMDIChild.Show()
End Sub
Simple and it works.
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.