My apologies. You need to set the properties *after* it has been shown. Also, you need to Show() instead of ShowDialog() as I indicated in my first post. All Show() does is set the Visible property == true. As a result of setting Visible == true, the OnVisibleChanged method is executed. The OnVisibleChanged method looks like this:
protectedvirtualvoidOnVisibleChanged(EventArgse){
boollocal0;
EventHandlerlocal1;
ControlCollectionlocal2;
intlocal3;
Controllocal4;
if(this.parent!=null&&this.Visible)
if(!(this.Created)){
local0=this.GetAnyDisposingInHierarchy();
if(!(local0))
this.CreateControl();
}
if(this.CanRaiseEvents){
local1=this.Events.get_Item(Control.EventVisible)asEventHandler;
if(local1!=null)
local1.Invoke(this,e);
}
local2=(ControlCollection)this.Properties.GetObject(Control.PropControlsCollection);
if(local2!=null){
local3=0;
while(local3<local2.Count){
local4=local2.get_Item(local3);
if(local4.Visible)
local4.OnParentVisibleChanged(e);
local3++;
}
}
}
So, it's making a call to CreateControl() thereby ignoring the properties changed for position before the call to Show. This is interesting behavior, and I wasn't aware of it until now. The following works for me:
In your button click event:
SubForm sf = new SubForm();
sf.Bounds = yourMainForm.Bounds;
sf.Show();
That works as expected. Make sense?
Originally posted by: noxxic
You usually don't set the dialog box's position before it's created. You need to handle the dialogbox's WM_INITDIALOG message (which is sent after the dialog is created, but before it is shown) and set the window position from there. In VB, I think you can also just override the dialogbox's OnLoad event.
This is .NET, there's no need to handle WM_INITDIALOG. The only time you'd need to handle a message manually in .NET is if you've created a custom message, or you've using p/invoke to call a win32 api function that sends a message (e.g. RegisterHotKey()). Then of course all you need to do is override WndProc from the System.Windows.Forms.Form class.