Open second form, right next first form

I'am using winforms.

I created an application which is nearly finished. Consider the following: I have two forms, the first form starts at application startup, the second form needs to be opened right next to the first form.

Example:

How can I access the location of the first form at the second form? Should I send "this" to the constructor of the second form?

EDIT

following code helped me out:

private void changelogToolStripMenuItem_Click(object sender, EventArgs e)
{
     if (_changelog.IsDisposed)
     {
            _changelog = new Changelog();
     }
            _changelog.Location = new Point((Left + Width), Top);
            _changelog.Show();
}

Answers


A basic rule to keep in mind when designing one's constructor: Never give any unnecessary information to the constructor.

So, what you need here is not the other window, but rather it's position. Even better, you need the position where your new window should be located at.

This means that you shouldn't let the second form know about the first form, instead it's constructor should take either:

  1. One parameter Point location
  2. Two parameters int x, int y

Depending on your preferance. You could (should) of course have both constructors, so you can decide whether to give Point location or int x, int y.

This all being said, forget what you read. Better than using a constructor at all, I would just set the property manually when creating the second form:

SecondForm form = new SecondForm()
{
    Location = new Point(this.Right, this.Top)
};

Which is just an other way of saying:

SecondForm form = new SecondForm();
form.Location = new Point(this.Right, this.Top);

Need Your Help

Why is this element moving?

html css

I have an entry form on my website and after you click submit it moves in google chrome but not in firefox or ie. Is there a way to make it stationary in chrome also? I've tried so many different

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.