I have two WinForms. A frmMainMenu and a frmIndividual with frmIndividual a child of frmMainMenu. Each window has its own MenuStrip in the designer. However at runtime, the child menu does not appear to have a menu strip.
Here is the code creating the child. (It's literally the templated MDI script but modded.)
Private Sub IndividualToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles IndividualPlayerToolStripMenuItem.Click
Dim ChildForm As New frmIndividual
' Make it a child of this MDI form before showing it.
ChildForm.MdiParent = Me
m_ChildFormNumber += 1
ChildForm.Text = "(" & m_ChildFormNumber & ") " & ChildForm.Text
ChildForm.Show()
End Sub
I doubled over the follow two questions/answers:
Merge menu strip items for MDI windows
Merge menu items from MDI child into container's menu
And I can confirm that I do now have a single menu, but the merging of menus happens even when the form isn't maximized.
What am I missing? Why are the menus merging when they weren't supposed to be?
This is the default behavior, the tool strip manager gets what you set to the merge-related properties in the MDI parent and child forms and strips then acts accordingly. The window state is not a merge factor. The Form.MainMenuStrip, ToolStrip.AllowMerge, ToolStripItem.MergeAction properties and strip's visibility are.
Now, if you need to hide and merge the MenuStrip of the MdiChild Form only when you maximize it, then override its OnSizeChanged method to AllowMerge and merge the strips when so, otherwise, show the MenuStrip and revert the merge. Use the ToolStripManager class to manually merge and revert.
' MDI Child Form
Protected Overrides Sub OnSizeChanged(e As EventArgs)
MyBase.OnSizeChanged(e)
Dim isMax = WindowState = FormWindowState.Maximized
menuStrip1.AllowMerge = isMax
menuStrip1.Visible = Not isMax
If MdiParent?.MainMenuStrip IsNot Nothing Then
If isMax Then
ToolStripManager.Merge(menuStrip1, MdiParent.MainMenuStrip)
Else
ToolStripManager.RevertMerge(MdiParent.MainMenuStrip)
End If
End If
End Sub
Related
I have on my screen design Panel1(left half), and panel2 through 10(right half), the panels on the right half are named based on data from a database.
I need to be able to click on a button in panel1 and when I do so, I need to set visibility to false for the current panel on the right half and set visibility to true that is referenced from the button click. I know that I can do the following but I think this is just way too much overhead and there has to be a better solution than this:
For Each control In Me.Controls.OfType(Of Panel)
If control.visible = true Then
control.visible = false
exit
Next
Panel that the visibility that needs to be set to false was dynamically created so it can not just be accessed by just name, otherwise that would solve my issue easily.
I seen this bit of code elsewhere and it would make my life easier, however not sure how to implement it dynamically when the panels are created, since the name of the panels are unknown at creation.
This bit of code to be able to reference the panels directly.
Dim machinePanel As Panel = DirectCast(Me.Controls.Item("pnl" & strMTB & strMachineID), Panel)
I'm not sure what you mean but "is referenced from the button click", so I'll assume that the text of the button refers to the panel name.
Create a method that handles the visibility
Private Sub SetPanelVisibility(button As Button)
'panel that the button is in
Dim leftPanel = CType(button.Parent, Panel)
'get right panels and ignore left
Dim rightpanels = Me.Controls.OfType(Of Panel).Where(Function(x) x.Name <> leftPanel.Name)
'set visibility of each panel.
For Each panel In rightpanels
panel.Visible = panel.Name = button.Text
Next
End Sub
To call the method just pass the button in on the click event. e.g.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SetPanelVisibility(sender)
End Sub
I am trying to get a form to center the parent. I have done lots of 'googling' and none if it has worked.
The issue is. When I have a form set to "Center Parent Form" it will work, unless the parent form is moved from the original starting location.
Example: Parent form starts top left of screen. I move it to center screen. I then preform and action to have a popup window, that window gets Centered to the original location of the parent form in the top left. Not the current one.
Visual: http://imgur.com/a/jGBfP
I have set the form properties to be 'Center Parent' as well as
Me.StartPosition = FormStartPosition.CenterParent
In which class and method are you setting the Me.StartPosition property?
Have you seen this Stackoverflow answer? https://stackoverflow.com/a/30199106/1337635
UPDATE
You need to do two things to get this to work:-
As per #mark-hall, you need to show the form and pass in the parent:-
Dim child As frmChild
child = New frmChild()
child.Show(Me) 'Explicitly declare the parent
As per the above answer I referenced, in the Load event of the child form, you need to call Me.CenterToParent()
I was able to duplicate the problem. Try adding the Shown EventHandler to your popup form. If you then assign the owner to the Form when you show it you should be able to position your form in the Handler, something like this. Be aware if you move the Owning Form the popup Form will not change.
Make sure you Show the Form using Show(Me) or else Owner will not be populated.
Public Class popup
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
BackColor = Color.LightBlue 'So I can see it
End Sub
Private Sub popup_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
Left = (Owner.Width / 2 - Width / 2) + Owner.Left
Top = (Owner.Height / 2 - Height / 2) + Owner.Top
End Sub
End Class
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 container (a panel) which can contain multiple Form controls.
(Form.TopLevel = False)
When the user moves the Forms around I would like to display scrollbars when a form is out of the panel bounds.
When I register the Form.Move event, I can set the AutoScrollPosition. This works unless the user uses the scrollbars.
The problem is that the form.move event is also fired when the scrollbars are used. The result is that the scrollbars don't work. (And I have currently no idea how to find out whether the form has been moved by the mouse or by the scrollbar)
So the question is: How can I make the scrollbars of the panel appear/work when a form (or multiple) forms of the panel exceed the boundaries? I think there must be a simpler way than to handle the move event..
Note:
The panel is placed inside a Infragistics DockableControlPane. (Managed by an UltraDockManager)
(So there are multiple panels which contain at least one form per panel)
The reason is that the "panels" should appear as tabs, can be moved around using the DockManager and display their "sub" forms (Which also can be moved around on their panel).
Any idea would be great
It looks like the LocationChanged event could be used. Example with only one form:
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
Dim f As New Form
f.TopLevel = False
AddHandler f.LocationChanged, AddressOf Form_LocationChanged
Panel1.Controls.Add(f)
f.Show()
Call Form_LocationChanged(f, EventArgs.Empty)
End Sub
Private Sub Form_LocationChanged(sender As Object, e As EventArgs)
With DirectCast(sender, Form)
Panel1.AutoScrollMinSize = New Size(.Bounds.Right, .Bounds.Bottom)
End With
End Sub
Using an MDI form seems to be more appropriate though for something like this.
I made a simple application in which an MDI parent loads a child form at a click of a menu item..(I am going to implement this in a much bigger and more useful application)...It contains initially a MenuStrip and on a click of the MenuStrip item a new form will open (typical MDI parent form style) This form is border less and I would like it to fit and fill the remaining space under the the MDI parents MenuStrip.
I need to know the properties to use in order to achieve this and at the same time (for the child form) to fit into the MDI parent form's free space at any resolution at which the display is.
So far I tried this but I got this error: Property access must assign to the property or use its value.
Private Sub frmGenPay_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.StartPosition(10, 10)
Call FitToScreenCompanyDetails()
End Sub
In the module:
Public Sub FitToScreen()
frmMDImainform.Size = My.Computer.Screen.Bounds.Size
End Sub
The second code above is for the MDI parent form if someone could give me some suggestions on this too it would be really kick-start things.
Thanks to everyone for any piece of advice. Thanks in Advance!
Me.StartPosition(10, 10)
That just isn't valid code. StartPosition is a property, you are treating it as though it is a method. Nor does it take a location. My best guess for the intended code:
Me.StartPosition = FormStartPosition.Manual
Me.Location = New Point(10, 10)
You'll run into more trouble implementing your intended design. There should be only one MDI main window. MDI child windows cannot be borderless.
Instead of worrying about startpositions, sizes and bounds, why can't you just (in the child form) set:
Me.WindowState = FormWindowState.Maximized
Me.MdiParent = myMDIParent