Solved

Position a form relative to another

Posted on 2009-04-14
9
793 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 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

691 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