devxlogo

A simple way to control a common dialog’s position

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 frmCDfrmCD.Move 0, 0frmCD.CommonDialog1.ShowColor

Similarly, here’s how you display the common dialog approximately at the center of the screen:

Dim frmCD As New frmCDfrmCD.Move (Screen.Width - frmCD.Width) / 2, (Screen.Height - frmCD.Height) / 2frmCD.CommonDialog1.ShowColor

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.

See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist