[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Position a form over another form in C#

[Position a form over another form in C#]

When you click this example's button, the program uses the following code to display a new instance of the main form positioned next to the button's lower left corner (as shown in the picture).

// Position a new instance of this form // just to the right and below the button. private void btnShowForm_Click(object sender, EventArgs e) { // Get the location in this form's coordinate system. Point form_pt = new Point(btnShowForm.Right, btnShowForm.Bottom); // Translate into the screen coordinate system. Point screen_pt = this.PointToScreen(form_pt); // Create and position the form. Form1 frm = new Form1(); frm.StartPosition = FormStartPosition.Manual; frm.Location = screen_pt; frm.Show(); }

The code creates a Point that holds the new form's desired location in this form's coordinate system. It then calls the form's PointToScreen method to convert the point into screen coordinates.

The code finishes by creating, positioning, and displaying a new form. The only real trick here is remembering to set the new form's StartPosition property to Manual so the form is positioned where its Location property says it should be instead of at a default position.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.