I’m a rather novice programmer who recently came up with a solution that works for my project, however I’m always looking for ways to improve my code.
So essentially, I have a settings form that pop’s up and I was looking for a way to put it next to my main form but not covering it nor appearing partially off of the screen the main form is on. I came up with this but it’s not very dynamic because it only checks 4 different locations and if none of them work it uses the default, which is center screen.
Here is what I have:
private void Place_Form(Form formToPlaceNextTo, Form formToPlace)
{
Point alignRightTop = new Point(m_parent.Location.X + m_parent.Width, m_parent.Location.Y);
Point alignRightBottom = new Point(m_parent.Location.X + m_parent.Width, (m_parent.Location.Y + m_parent.Height) - this.Height);
Point alignLeftTop = new Point(m_parent.Location.X - this.Width, m_parent.Location.Y);
Point alignLeftBottom = new Point(m_parent.Location.X - this.Width, (m_parent.Location.Y + m_parent.Height) - this.Height);
if (Screen.FromControl(formToPlace).WorkingArea.Contains(new Rectangle(alignRightTop.X, alignRightTop.Y, this.Width, this.Height)))
{
this.Location = alignRightTop;
return;
}
if (Screen.FromControl(formToPlace).WorkingArea.Contains(new Rectangle(alignRightBottom.X, alignRightBottom.Y, this.Width, this.Height)))
{
this.Location = alignRightBottom;
return;
}
if (Screen.FromControl(formToPlace).WorkingArea.Contains(new Rectangle(alignLeftTop.X, alignLeftTop.Y, this.Width, this.Height)))
{
this.Location = alignLeftTop;
return;
}
if (Screen.FromControl(formToPlace).WorkingArea.Contains(new Rectangle(alignLeftBottom.X, alignLeftBottom.Y, this.Width, this.Height)))
{
this.Location = alignLeftBottom;
return;
}
}
Any suggestions or preferred coding techniques?
As suggested by Snowbear, I used another SE site to ask the same question and got a good response.
https://codereview.stackexchange.com/questions/8881/is-there-a-more-elegant-way-of-arranging-my-forms