• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Quick VB .Net question

JonTheBaller

Golden Member
I have two forms... let's just say MainForm and SubForm. I want to hit a button on MainForm that will hide MainForm and open SubForm on the screen in the same position where MainForm was (sort of like how it works on a PDA if you got a new window). When I close SubForm, I want MainForm to reappear and be in the position SubForm was in when it was closed. If I wasn't clear, please tell me. Thanks.
 
Just instantiate the SubForm class in the Click event of the button on the MainForm. Before calling .Show on the instance of SubForm, set the Width and Height properties = to that of the MainForm.

e.g.:

Dim subForm As New SubForm
subForm.Width = Me.Width
subForm.Height = Me.Height
subForm.Show()

Make sense?
 
they may now open on top of each other and can be moved independently, you should be able to use Mainform.Visible = false to hide the main form, dont know if that is the exact syntax, I usually code c#...
 
But then he has to deal with lining up the MainForm when he closes the SubForm. I was leaving it up to him to do that...

The easiest way to handle syncing the positions of the MainForm when the SubForm is visible would be to handle the LocationChanged event.
 
i was assuming in the event he is using to change back to the main form, he would set the mainform.top and mainform.left to the Me.Top and Me.Left of the subform, the make the subform invisible or unload it and make the main form visible again. :shrug: and just a side note, in your example above, you set the forms size the same, but depending on its start up location, it may not startup where the main form was. Another solution that may be better for him is to have the forms modal and open the sub form inside the main form and maximize it, but I dont have enough information to make that determination.
 
Thanks for your help guys.

This is the code I have for clicking the button that takes you back to the main form from the subform:

Private Sub HomeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HomeButton.Click
Dim mainForm As New Main()
mainForm.Width = Me.Width
mainForm.Height = Me.Width
mainForm.Left = Me.Left
mainForm.Top = Me.Top
Me.Hide()
mainForm.ShowDialog()
End Sub

For some reason, that doesn't set the mainForm in the same position that subform was in. It puts mainform in a position cascaded from the subform position. Any ideas?
 
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.

 
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.
 
Back
Top