GreenKawasaki Posted December 27, 2003 Posted December 27, 2003 (edited) Hello, How do I setup my forms so I can use that 'CenterParent' option for forms? I have a mainform and a bunch of smaller forms that I want to popup in the middle of the main form no matter where it is on the screen. Any help is greatly appreciated! I currently have my mainform's IsMDIContainer set to true. Dim MyForm As New SmallForm() MyForm.StartPosition = FormStartPosition.CenterParent MyForm.ShowDialog() Unfortunately, this doesnt work. Best regards, VB Newbie Edited December 27, 2003 by GreenKawasaki Quote
Leaders dynamic_sysop Posted December 27, 2003 Leaders Posted December 27, 2003 you need to tell your form ( MyForm ) what it's MdiParent is. then after showing the form, you need to set it's new location. for some reason using the CenterScreen start position doesnt work. here's a quick example of how i got one running... Dim MyForm As New SmallForm() MyForm.MdiParent = DirectCast(Me, Form1) '/// set MyForm's parent here. MyForm.Show() Dim x As Integer = (Me.Width / 2) - (MyForm.Width / 2) Dim y As Integer = (Me.Height / 2.2) - (MyForm.Height / 2) '/// allow extra for the toolbox ( hence 2.2 ) MyForm.Location = New Point(x, y)'///Center the form in it's parent. Quote
GreenKawasaki Posted January 2, 2004 Author Posted January 2, 2004 Dynamic_sysop, Thanks for the fast reply! This code finally solves my problem with centering forms. (I'm not really sure why .net doesn't make this easier) I really appreciate your help. Best Regards, GreenKawasaki Quote
AlexCode Posted January 2, 2004 Posted January 2, 2004 Or it's me who is missunderstanding something here or... I don't know... If you have a MDIChild and you want it to show in the center of the MDIParent you have to set: ChildForm.MDIParent = MDIParent ChildForm.StartPosition = FormStartPosition.CenterScreen ChildForm.Show Cause the work screen for this child form it's the MDIParent WorkSpace... If, in the other hand, you have a FormA and whant to display a FormB with ShowDialog, not MDIChild, centered in FormA you'll have to use: FormB.StartPosition = FormStartPosition.CenterParent FormB.ShowDialog(FormA) This is the StartPosition Logic... Alex :D Quote Software bugs are impossible to detect by anybody except the end user.
mcfarljw Posted January 7, 2009 Posted January 7, 2009 I had problems with VB not consistently centering my form in its parent too. So I created the follow bit and stuck it in a module for reusage. Sub CenterInParent(ByVal parent As Form, ByVal child As Form) Dim frmA As Form = parent Dim frmB As Form = child Dim x As Integer = (parent.Width / 2) - (child.Width / 2) Dim y As Integer = (parent.Height / 2.2) - (child.Height / 2) child.Location = New Point(x, y) End Sub Quote
benpaul Posted January 7, 2009 Posted January 7, 2009 I think the issue is that .NET centres the window to the MDI parent based on the 'size' values that you set for the MDIParent properties. When you maximise the MDIParent, the 'size' values do not change, therefore 'CenterParent' will not give the desired result. I have always used CenterScreen with no problems so far. Sub CenterInParent(ByVal parent As Form, ByVal child As Form) Dim frmA As Form = parent Dim frmB As Form = child Dim x As Integer = (parent.Width / 2) - (child.Width / 2) Dim y As Integer = (parent.Height / 2.2) - (child.Height / 2) child.Location = New Point(x, y) End Sub This will also solve the problem, and will always find the middle of the Parent in its current state (maximised or not). Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.