A window can be positioned alongside another. It must be positioned automatically. It should appear in the correct place regardless of where the first window is. We see an example of how to manipulate the coordinates of Windows Forms in the C# language.

We want to solve the problem with some methods in the C# programming language. Our example will position itself to the bottom. Let's look at some useful properties we need to use to solve this problem and make your program much more graceful and useful.

WorkingArea.Height
This returns the height of the screen minus the taskbar in pixels.
WorkingArea.Width
Width of screen, minus the Vista sidebar or anything else.
Height
Height of your window in pixels.
Width
Width of window in pixels.
Location
Top left corner of the form in pixels relative to the screen.
X
Y
Horizontal and vertical coordinates of Point.
Here we need that adjusts the position of the window before it is shown. Look at the Screen.PrimaryScreen.WorkingArea.Height and Width properties above. These are the most important trick to this project. Here is the approach we will use to position windows like a palette relative to another window. This will position to the bottom.
Window that positions a sub-window [C#]
/// <summary>
/// A Windows Form.
/// </summary>
public partial class MainWindow : Form
{
/// <summary>
/// The Windows Form we want to position.
/// </summary>
ReporterWindow _reporterWin = new ReporterWindow();
public MainWindow ()
{
InitializeComponent();
}
private void reportMoveToolStripMenuItem_Click(object sender, EventArgs e)
{
// This is an event handler triggered by a menu item (or button).
PositionReporterEdge(); // Position the window.
_reporterWin.Show();
}
/// <summary>
/// Position the "Reporter" form next to the current form.
/// </summary>
private void PositionReporterEdge()
{
int screenHeight = Screen.PrimaryScreen.WorkingArea.Height;
int screenWidth = Screen.PrimaryScreen.WorkingArea.Width;
Point parentPoint = this.Location;
int parentHeight = this.Height;
int parentWidth = this.Width;
int childHeight = _reporterWin.Height;
int childWidth = _reporterWin.Width;
int resultX;
int resultY;
if ((parentPoint.Y + parentHeight + childHeight) > screenHeight)
{
// If we would move off the screen, position near the top.
resultY = parentPoint.Y + 50; // move down 50
resultX = parentPoint.X;
}
else
{
// Position on the edge.
resultY = parentPoint.Y + parentHeight;
resultX = parentPoint.X;
}
// set our child form to the new position
_reporterWin.Location = new Point(resultX, resultY);
}
}
Overview. The example shows how to get the sizes of both windows. Obviously, for our calculations to work we must know the positions and sizes of both windows we want to position.
Details. Get the dimensions. In our example, we must get the size of the "working area", meaning the size of the user's desktop minus the taskbar (and sidebar in Vista). We use the WorkingArea class for this. Before we place the form, we need to make sense that our new position makes sense. After we make that check of the desired Location, we place the new Form.
Call the PositionReporterEdge method before you show the ReporterWin Windows Form. The above function supports positioning a new window to the bottom, which is a fairly common position for palettes and similar windows. You can add left, right and top if you need them.
I have found that the FormBorderStyle of "FixedToolWindow" looks best for palettes. This will mean that your form will look like a palette in Photoshop. On the other hand, I have found that a "FixedSingle" style looks good for a "Find and Replace" window.

I have used this method or one similar to it for creating windows like palettes that show next to the side of a window. The example code here is for a reporting window for a move on a game board (think chess or a board game). I have also used it for a "Find in this page" window, similar to Microsoft Word's.

We saw how you can position a window on the edge of another window, using Windows Forms. You can achieve a really awesome user experience with a palette window or a find window, or other types of windows. It is easy to do and really enhances the interface for your users.
Windows Forms