Solved

Position a form relative to another

Posted on 2009-04-14
9
789 Views
Last Modified: 2012-05-06
I seem to be having difficulties positioning a form using left and top properties in VB2008 Express. The main form loads and is centered using the form's StartPosition property, then I've tried to set another form's left and top properties as follows:
form2.left = form1.left+form1.width
form2.top = form1.top
But form2 left and top properties are being reset by the startposition centering and these new positions are not reflected in the left and top properties which are fixed at design time, they are not current positions.
Any ideas please?
0
Comment
Question by:ncw
  • 5
  • 4
9 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24139725
Not sure if I understand you here. You have one form that is centered. That is form1 and that works. You have another form and you want that positioned to the right side of form1. But it uses the position used before repositioning. Is that correct? And do you want the form2 form to always go with the other form when it is moved? Or only on startup?
0
 
LVL 1

Author Comment

by:ncw
ID: 24139831
Yes that's correct.
At the moment I was only trying to position the form2 at startup, but it would be a bonus if form2 could follow form1 to where ever form 1 is repositioned.
0
 
LVL 39

Accepted Solution

by:
abel earned 500 total points
ID: 24140303
I go for the bonus!

Assuming two forms, Form1 and Form2, you can do the following to get the Form2 being always on the right side (aligned on top side) of the Form1, whatever happens to Form1 or where you drag it to (select the event though the Properties Window):

    Private Sub Form1_LocationChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LocationChanged
        Form2.Left = Me.Left + Me.Width
        Form2.Top = Me.Top
    End Sub

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 39

Expert Comment

by:abel
ID: 24140318
If you load the form2 at some point, say in the form1_load event, you also need to reposition it once just after the form2.show, using the same function.
0
 
LVL 1

Author Comment

by:ncw
ID: 24141370
That works well, even seems to handle the minimised state and restore. The secret seems to be to set the let and top properties after the show() command. Thanks!
0
 
LVL 39

Expert Comment

by:abel
ID: 24141998
You're welcome, glad I could be of some help! ;-)
0
 
LVL 1

Author Comment

by:ncw
ID: 24183646
This worked when referencing the form by it's class name, but if I set eg ofrm = new Form2 and try ofrm.left = me.left then it causes an error.
Any ideas please?
0
 
LVL 39

Expert Comment

by:abel
ID: 24183668
That's a new question and a different technique (and remember, you closed this q. already...;).

But without knowing what the error is I cannot really help, I'm afraid. Can you create a "related question" with all the details?
0
 
LVL 1

Author Comment

by:ncw
ID: 24184135
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question