?
Solved

Position a form relative to another

Posted on 2009-04-14
9
Medium Priority
?
803 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 2000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

862 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