?
Solved

Position a form relative to another

Posted on 2009-04-14
9
Medium Priority
?
799 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

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!

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

765 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