I recently received this question from one of the users of a messageboard that I answer questions for:
>>Hi,
>>I'm trying to open up a form and set it's top and left propertyto be the same as one of my panels on my form. However, it's not working out the way i >>planned. I even tried adding up the top of form1 and the top of the panel but that didn't work either. Any help on this would be appreciative.
His original code is below.
Private Sub lbl_LinkClicked( ByVal sender As System. Object , _ ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) _Handles lbl.LinkClicked Dim frmForm As New Form2 With Form2 .Show() .Width = Me .Panel1.Width .Height = Me .Panel1.Height .Top = Me .Top + Me .Panel1.Top .Left = Me .Left + Me .Panel1.Left End With End Sub
At first glance of the code it looks like this should work. He makes the form's width the same as the panel, then makes it the same height as well. Now it is time to place it on top of where the panel is located. He then sets the Top of the form equal to the Top of the current form plus the top of the panel on the form. This should place it right on top of the panel right? Well no, actually he forgot to take into account the forms menubar and side border.
So this should work for him right. Well yes it does but who wants to hard code that kind of detail. What if the size of the menubar or border changes?
Dim frmForm As New Form2 With frmForm .Show() .Width = Me .Panel1.Width .Height = Me .Panel1.Height .Top = Me .Top + Me .Panel1.Top + 30 .Left = Me .Left + Me .Panel1.Left + 4 End With
To get the exact size you can use the System.Windows.Forms.SystemInformation Namespace. You can use the FrameBorderSize.Width and the CaptionHeight (Menubar) to accomplish this. Make sure that you add the CaptionHeight and FrameBorderWidth together for the Top calculation because the border goes around the whole form.
Dim frmForm As New Form2 With frmForm .Show() .Width = Me .Panel1.Width .Height = Me .Panel1.Height Dim intBorder As Integer intBorder = System.Windows.Forms. _ SystemInformation.FrameBorderSize.Width Dim intMenuHeight As Integer intMenuHeight = System.Windows.Forms. _ SystemInformation.CaptionHeight .Top = Me.Top + _ Me.Panel1.Top + _ intBorder + _ intMenuHeight .Left = Me.Left + _ Me.Panel1.Left + _ intBorder End With
So this got me to thinking. What else can the SystemInformation Namespace do for me? Well quite a bit actually. below I have detailed out some of the information you can get using this namespace.
What is the computer name? SystemInformation.ComputerNameIs a connection to a network present? Network : SystemInformation.NetworkWhat is the users Domain NameSystemInformation.UserDomainNameWhat is the UserName?SystemInformation.UserNameWhat was the boot mode (Normal, Safe Mode etc...)SystemInformation.BootMode.ToStringAdjusting the disply for multiple monitors.SystemInformation.MonitorCount.ToStringSystemInformation.MonitorsSameDisplayFormat.ToStringSystemInformation.ArrangeDirection.ToStringSystemInformation.PrimaryMonitorSize.ToString Questions about the mouse.MousePresent : SystemInformation.MousePresent.ToStringMouseButtonsSwapped : SystemInformation.MouseButtonsSwapped.ToStringMouseButtons: SystemInformation.MouseButtons.ToString
There are other items in there as well. Check out the help files to see all the things this namespace can give you.
Happy Programming,
DotNetDoc