Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB4,VB5,VB6
Expertise: beginner
Mar 11, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

A simple way to control a common dialog's position

The CommonDialog control doesn't provide any direct way to decide where a common dialog appears on the screen. In all cases, in fact, the control tries to display the common dialog centered on its owner form. This behavior suggests a simple way to affect the approximate position of the common dialog.

The trick is simple: instead of using a CommonDialog control on the current form, you place the CommonDialog control on another form. This form will be always invisible, and its only purpose is to affect where the common dialog be be displayed.

Create a form, name it frmCD, and place a CommonDialog control on it. Then, run the following code from another form when you want to display the Color common dialog in the upper-left corner of the screen:

Dim frmCD As New frmCD
frmCD.Move 0, 0
Similarly, here's how you display the common dialog approximately at the center of the screen:

Dim frmCD As New frmCD
frmCD.Move (Screen.Width - frmCD.Width) / 2, (Screen.Height - frmCD.Height) / 2
Note that this method lets you affect the position of the common dialog only approximately. For a more precise placement you must resort to more advanced techniques, such as subclassing.
Francesco Balena
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date