In September 1998 I published a tip of the month called Location, Location, Location. This month's tip provides VB.Net code for the functions. I tried to maintain compatibility with the VB 6 version.
The tip gathers into one module, functions, subroutines and a class that set a window's location, build a form's Window menu, allow users to size a window using standard resolution settings [800x600, etc.] and arrange MDI child forms. If any, the initial window location information is retrieved from the registry. Location information is stored in the registry when the form is unloaded. This begins the sixth year of these tips.
2006/07/29 - Convert to VB 2005.
Form Positioning Routines
Function or Sub Meaning TB_CenterForm Center the form on the screen. Does not consider the task bar or other docked bars. TB_CenterForm32 Center the form in the desktop work area, exclude docked bars. TB_FillDesktop32 Resize the form to fill the desktop work area. TB_WindowIsItAlreadyFullScreen Return true if the form already fills the desktop work area. TB_CenterFormInMDI Center a child form within its MDI form. TB_FillFormInMDI Resize the child form to fill the MDI work area. GetMDIScaleHeight
In VB 6 an MDI's ScaleHeight or ScaleWidth return the size of the non-docked client area - for example exclude toolbars. I could not find any way to easily do this in VB.Net so I wrote two functions that return the height/width less the size of docked controls. TB_Zone Set a screen zone to be used by the centering routines. Sets the screen quadrant. Use this before loading a non-child form. Then use TB_CenterForm to center the form in the quadrant. TB_ZoneFullScreen After using TB_Zone use this to reset the screen quadrant. TB_MakeSureOnScreen Make sure the form is fully displayed on the screen. Use this routine during the form load event. Is called by TB_GetFormInformation. TB_WindowSizeOnWindow Make sure the form is displayed in the desktop area. TB_GetFormInformation Get information about the form's location from the registry. Use this routine in the form load event. TB_SaveFormInformation Save form information into the registry. Call this routine in the form unload event. TB_GetScreenSize Get the current screen size. Works in conjunction with the following routine. TB_SaveScreenSize Save the current screen size. Call during the application unload process. Use TB_GetScreenSize and the current screen sizes to determine if the user has changed screen resolution since the application was last run. If so then use function 'TB_MakeSureOnScreen' to make sure forms fit on the screen. TB_SaveGetFullScreen If you want to restore a form using TB_FillDesktop32 but don't know if it was last displayed in Full Screen use this function. TB_GetDesktopWorkArea Used to get the left, top, width and height of the desktop area. Used by the above centering in desktop area routines. It can be used directly. TB_Dock Docks a window in the Left, Right, Top or Bottom positions of the desktop area. TB_Corner Move the form to the Top Left, Top Right, Bottom Left or Bottom Right positions of the desktop area. Added 2002/09/01. TB_InvokeMethodInActiveChild In VB 6 we could call a public method in the active child using and trapping the error if the child did not contain the method. That technique is not possible with VB.Net. This subroutine achieves the same result.
Window Menu Items
A Window menu is dynamically created using class BGLocLocLoc. See source code below. A sample Window menu:
Build the Window Menu
First add a menu component. Open the form. In the Toolbox double-click Windows Forms MainMenu. MainMenu1 is added to the component tray. For example:
Then call code similar to:
Dim objLocLocLoc as New BGLocLocLoc
.BuildWindowMenu(Me, Me.MainMenu1, .eWinBuildArrange.Build, .eWinBuildDock.Build, .eWinBuildSize.Build) 'build the windows menu
Calling BuildWindowMenu In BGLocLocLoc
Method and Enums Meaning .BuildWindowMenu Build the Window menu. Center is the only 'default'. eWinBuildArrange.Build/NoBuild If 'Build' then the 'Arrange' menu is created. Meaningful only with MDI forms. eWinBuildDock.Build/NoBuild If 'Build' then the 'Fill' menu is created. eWinBuildSize.Build/NoBuild If 'Build' then the 'Size' menu is created. eWinBuildCorner.Build/NoBuild If 'Build' then the 'Corner' menu is created. Added 2002/09/01.
What Happens in BuildWindowMenu
First the menu items are created dynamically then events are added to process the menu item selection. This is possible because VB.Net allows menus and submenus to be created in code. And event handlers can be created for the menu items. Code similar to the following is used in class BGLocLocLoc:
m2 = New MenuItem() 'Arrange
m2.Text = "Arrange"
AddHandler .Select, New System.EventHandler(AddressOf WindowMenuItem_Select)
m3 = New MenuItem()
m3.Text = "Tile Horizontal"
AddHandler .MenuItems(m3.Index).Click, New System.EventHandler(AddressOf WindowMenuSubItemArrange_Click)
The Demo Application
I wrote a VB.Net MDI application to demonstrates the routines. The MDI form:
Contained in the top panel are three commands used with the MDI form. They are self explanatory. The commands shown in the bottom panel are used to load additional child or non-child forms. The MDI form contains 5 docked controls. The left docked group box contains a bottom docked label. The right docked control contains a picture. Docking is a new property of some of the VB.Net controls. A status bar is added dynamically. See: Status Bar - VB.Net Version for details. Sample child forms are shown above in the MDI form. A sample, non-child form:
A window menu and status bar are dynamically added to the non-child form. Code similar to the following is used to create the menu and status bar:
Dim objLocLocLoc as New BGLocLocLoc
With objLocLocLoc 'build the window menu
.BuildWindowMenu(Me, Me.MainMenu1, .eWinBuildArrange.NoBuild, _
.eWinBuildDock.Build, .eWinBuildSize.Build) 'build the windows menu
Dim objStatusBar as New BGStatusBar
With objStatusBar 'add a status bar
.AddStatusBarToForm(Me, , True)
Most of the centering routines accept optional offset values. The value is added to the 'SetBounds' operation. For example: Set the left offset to 50 and the top offset to -50 before calling a center command. The form will be offset right 50 and up 50 pixels.
Using the Routines
You will need to add the following modules to your application if you want to use these routines:
modLocationLocationLocation.vb The routines and class BGLocLocLoc described in this document. modRegRead.vb Routines used to read and write to the registry. Click here to review the VB.Net RegRead documentation.
Used in the demo:
BGStatusBar.vb Add a status bar to the form. See: Status Bar - VB.Net Version for details. BGWebConnect.vb Connect to the web using a URL and the System.Diagnostics.Process class. BGAbout.vb Provide an About message BGInfo.vb Connect to the web and display this page. BGHelpMenu.vb Dynamically create the help menu.
Updated January 17, 2003
Added support for 1280 x 768. This size is used by the Sony SDM-V72W [wide] monitor. A monitor I began using recently.
Updated May 5, 2003
Fixed Bug: WindowState was not being returned via function TB_GetFormInformation. I began using the 'Return' statement with the VB.Net version and did not realize that Return not only returns a value but exits the function.
Download VB Code
Click here to download the VB.Net Source Code [70k].
Click here to download the VB 2005 Source Code [71k].
Previous Tip Tip Index Next Tip First Tip