I am using the Common Dialog ShowOpen to select a file to open using VB6. How can I ensure that the control is centered over the calling form. If I use two screens often the dialog control opens on the other screen and is hidden behind other windows.

Recommended Answers

All 5 Replies

By screens you mean monitors right? As in, you have two monitors, and you click "open" in your app, and it loads the commondialog on the other monitor?

Yes that's right. If the user moves the form the common dialog always opens in the location it was first opened which could be on a different monitor.

By screens you mean monitors right? As in, you have two monitors, and you click "open" in your app, and it loads the commondialog on the other monitor?

Ok, I have bad news. As far as I can tell, it can't be done with a commondialog control box. This is because the calling procedure (your app) gets frozen when the call to .showopen takes place. So, you call show open, and everything halts until the user either cancels, or selects a file. I do, however, have a workaround.

Does it have to be centered on the form, or can it be just over the form? The reason I ask, is because if the form is maximized, it will be different code than if the form is a normal sized window.

If the form is a normal sized window, is it cool to align the dialog box with the left and top edges of the form? This will put it on the same screen, and it won't look bad unless the form is bigger than the dialog box. I have been able so far, to center it (top of the dialog is centered between the top and bottom of the form, and the left edge is dead centered between the left and right edges of the form), but it looks really goofy.

The work-around requires the use of the API, and eliminates the need for the common dialog control at all... the only downfall (if you can call it that) is that the dialog window is different than the commondialog window. It's still a dialog to show open, but it's the same one that Operating system uses. Let me know how this all sits with you so far, and then I can proceed.

That solution seems fine. As long as the Dialog box is over the form somewhere the user will see it. The problem I have been having is that sometimes the Dialog box appears on the other monitor hidden behind a Window that has been set as on top and then the user can't see it and hence can't close the dialog to get focus back to the calling window.

Thanks for your help.

Ok, I have bad news. As far as I can tell, it can't be done with a commondialog control box. This is because the calling procedure (your app) gets frozen when the call to .showopen takes place. So, you call show open, and everything halts until the user either cancels, or selects a file. I do, however, have a workaround.

Does it have to be centered on the form, or can it be just over the form? The reason I ask, is because if the form is maximized, it will be different code than if the form is a normal sized window.

If the form is a normal sized window, is it cool to align the dialog box with the left and top edges of the form? This will put it on the same screen, and it won't look bad unless the form is bigger than the dialog box. I have been able so far, to center it (top of the dialog is centered between the top and bottom of the form, and the left edge is dead centered between the left and right edges of the form), but it looks really goofy.

The work-around requires the use of the API, and eliminates the need for the common dialog control at all... the only downfall (if you can call it that) is that the dialog window is different than the commondialog window. It's still a dialog to show open, but it's the same one that Operating system uses. Let me know how this all sits with you so far, and then I can proceed.

A Little Bit Here. Basically, There is a module, and the module contains the API Declarations, The Required UDT's (User Defined Types) and a public function that when called, will show the dialogbox, and return what the user selected to the calling procedure (like in a variable) filename = dialogbox or whatever. As soon as the dialogbox is launched (in this example, with a command button), it enables the timer control. When the program is first run, the timer is not enabled (no need to be, since we only need to check for the dialogbox when we call the function to show the dialog box). The timers interval is set to 100 milliseconds... you can lower this if you want, but it seems to work pretty efficiently.
The timer code, basically looks for the dialog box window, and if it doesn't find it, it just exits the sub, and waits for the timer to fire again.... at 100 milliseconds, this doesn't take long. If it does find the dialogbox (this is done using the findwindow API) then it gets the dialog box's dimensions (left, right, top and bottom in pixels). It also gets the form's dimensions, and then calculates the width and height of the dialog box. It then calls movewindow to move the dialogbox flush with the left edge and top edge of the form, and then quites the timer.

The code itself is overly commented, so that you know exactly what's going on, and why. If you have any questions, or suggestions, just let me know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.