Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 150
  • Last Modified:

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 Form3...at 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?

  • 2
1 Solution
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

BlakeMcKennaAuthor Commented:
Thanks James..that worked!

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now