Moving Forms

  • Thread starter Thread starter bwadley
  • Start date Start date
B

bwadley

Hi,

Im fairly new to VB.NET and am hoping someone can help me with what is
probably a simple problem.

I have an MDI app, when I open a child form, I want it centered to the
parent. So I put some code in the FormLoad procedure which works out
the centre of the screen and moves the child appropraitely.

This is pretty much the code I have:

MdiParent = frmMain
Me.Top = ((frmMain.Height - Me.Height) / 2)
Me.Left = ((frmMain.Width - Me.Width) / 2)
Refresh_VehicleList()

The problem is that when this window opens, you can actually see the
form move. It seems that if i remove the call to refresh_vehiclelist
that the problem isnt as bad, but is still visible. This procedure
makes SQL server calls so tends to pause briefly as it opens the
connection. Its not a speed issue with the hardware as its a current
Core 2 Duo system.

My question is, is there anyway to hide the form before moving it (I
tried the Hide method but it didnt move) so that the user can not see
the form moving every time they open it?

Many thanks in advance!

Brad.
 
Just put the code under Sub New After the InitializeCompenent call. As
for the answer to the me.hide question, you can just set me.visible =
false.

Thanks,

Seth Rowe
 
What about using the StartPosition property before you actually open the
form? Will that help?

Dim frm As New Form2

frm.StartPosition = FormStartPosition.CenterParent

frm.Visible = True


Steve
 
What about using the StartPosition property before you actually open the
form? Will that help?

Dim frm As New Form2

frm.StartPosition = FormStartPosition.CenterParent

frm.Visible = True


Steve
 
I believe that mdi child forms ignore the StartPosition property. Not
100% sure however.

Thanks,

Seth Rowe
 
Hi Seth, thanks for your help, but as i'm fairly new to vb.net, can you
please explain a little further what you mean by putting the code under
"Sub New" and where the InitializeComponent call is?

I have tried some other suggestions here but it seems unless the form
is visible (not hidden) setting the Top and Left positions have no
effect and the form opens in the top left corner of the MDI Parent.

Thanks!
 
Sub New() is the construnctor for for an object - in this case the
form. If you type in "Sub New" and press enter the ide will change that
to the following:

Sub New()

' This call is required by the Windows Form Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.

End Sub

The InitializeComponent is a method that contains the code generated by
the designer. Any time you add a control or change it's properties it
is writen to this method. Generally, you want this call to occur before
anything else happens, that why I (and the comment line in the sub)
tell you to place the code after this call. Unfortunately, mdichildren
apperantly ignore changes to the size properties here also, so my
advice won't help. I'll do some research to find out why, or perhaps
someone else will shed some light on this.

Thanks,

Seth Rowe
 
Brad, try this:
Dim frm As frmMDIChild1

Dim size As Size

size.Width = Me.Width / 2

size.Height = Me.Height / 2

frm = New frmMDIChild1

frm.MdiParent = Me ' this is the MDI parent

frm.StartPosition = FormStartPosition.Manual

frm.Left = 20

frm.Top = 20

frm.Size = size

'frm.StartPosition = FormStartPosition.CenterParent

frm.Show()



HTH

Steve
 
Hi Steve thanks for helping however your code simply opens up an mdi
child in the top left, even if I try and center it. I also am trying to
center a form designed in the IDE... I don't uderstand why this is so
hard, surely what I am trying to do is not uncommon?

Thanks again for your help.
 
Thanks Seth for explaining that, I understand now. Shame it is still
ignoring the properties. Appreciate any thing you come up with.
 
Brad,
if you set the form's StartPosition to Manual, as I have in the code below,
you can then size the form and position it prior to opening it. I tried this
in a MDI application on my own system so I know that it works. However, if
you fail to set the StartPosition to Manual, it will mearly open up a form
in the top, left position. The whole trick is setting the StartPosition to
Manual first.

Steve
 
Opps, I forgot to take out the line:
'frm.StartPosition = FormStartPosition.CenterParent
which was commented out anyway. Make sure you take that line out.

Steve
 
I still haven't found an answer for "why", but for now you should just
call me.visible = false before you set the location of the child. Also,
the form shouldn't show in the form_load event unless you explicitly
tell it to (me.show(), me.bringtofront, etc). If you're using any of
these methods before setting the location, removing them may solve the
problem.

Sorry I'm not much more help,

Seth Rowe
 
Hi Rowe,
I have run up against this before, in a non-MDI application and the only way
I could set a form's start up position to a custom location was to first set
the form's StartPosition property to manual before setting the x and y,
alternatively the location property, and then opening up the form. It works
in a MDI application as well, I just tested it.

BTW, loved your post about the Excel column hiding. It's better to teach a
man to fish and feed him for life than to give him a fish and feed him for a
day. I think you did that same type of thing with an Access question a few
days ago too. Nice.

Steve
 
Steve said:
Brad,
if you set the form's StartPosition to Manual, as I have in the code below,
you can then size the form and position it prior to opening it. I tried this
in a MDI application on my own system so I know that it works. However, if
you fail to set the StartPosition to Manual, it will mearly open up a form
in the top, left position. The whole trick is setting the StartPosition to
Manual first.

I didn't know about that trick, I'll have to try that. :)

I've been dealing with this a different way. Following is a copy of my
response to another current thread. If nothing else, notice that I
take into account the possibility that the Child is wider/taller than
the Parent's client area, and never set the Top/Left less than zero;
which would hide part or all of the title bar:

-----

MDI children ignore the StartPosition property, as well as any other
attempt to set their position prior to .Show.

Put this in the Load event of every MDI child form you want centered:

CenterInParent(Me)

And this in a module:

Sub CenterInParent(ByRef frm As Object)
frm.Left = Higher((frm.Parent.ClientSize.Width - frm.Width) / 2, 0)

frm.Top = Higher((frm.Parent.ClientSize.Height - frm.Height) / 2,
0)
End Sub

Function Higher(ByRef x, ByRef y)
Higher = IIf(x > y, x, y)
End Function
 
Back
Top