Solved

Position a form relative to another

Posted on 2009-04-14
9
783 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 39

Expert Comment

by:abel
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:ncw
Comment Utility
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
Comment Utility
You're welcome, glad I could be of some help! ;-)
0
 
LVL 1

Author Comment

by:ncw
Comment Utility
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
Comment Utility
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
Comment Utility
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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 discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now