Controlling a forms location within another form?

I have an MDI Form (Form1) that calls MDI Child Form (Form2). This Child Form calls another Form (Form3) which in turn calls another Form (Form4).

What I need to be able to do is make Form4 be centered in Form3. Using the "StartPosition" property for Form4 doesn't work because it's not a Child form to least it's "IsMDIContainer" property is set to False. Is there a quick way of doing this using properties or do I need to calculate it?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BlakeMcKennaAuthor Commented:

I'm taking a different approach altogether. I'm not going to use a form, rather an "Inputbox". However, what I need to do is determine the coordinates of a textbox in Form3 relative to where it is on the screen and not it's parent form. I thought using the "Point" Class but I may not be using it correctly and I can't find any examples.
Jacques Bourgeois (James Burger)PresidentCommented:
Actually, it is easier to do with a Form than it is with an InputBox, because you know the size of the form and you do not know the size of an InputBox.

You could always get the size of the InputBox by trial and error, but since it is not documented, this is something that might change in a future version of the framework. Also, the InputBox is part of the Microsoft.VisualBasic namespace. It is usually better to use the framework own features instead of relying on those that were designed to ease the transition from the old VB.

Also, note that Point is not a class, it is a structure. There are differences in the way they behave and the way you use them. You might want to give a look here in order to learn about the differences.

In order for code like the following to work, you must make sure that the StartPosition property of Form4 is set to Manual. Any other setting will override your own Location. The idea is simply to calculate half the difference of Width between the 2 forms, and use that as an offset from the Top and Left of the first form, something like the following, assuming that Form4 is created and shown from the form in which you want to center it:

Dim frm As New Form4
frm.Location = New Point(Me.Location.X + CInt((Me.Width - frm.Width) / 2), Me.Location.Y + CInt((Me.Height - frm.Height) / 2))

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BlakeMcKennaAuthor Commented:
Thanks James..that worked!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.