How to Set the StartPosition of a Windows Forms Form Using Code
Автор: vlogize
Загружено: 2025-02-18
Просмотров: 4
Описание:
Learn how to effectively set the `StartPosition` of a Windows Forms form with C# code, ensuring your form displays correctly in your applications.
---
This video is based on the question https://stackoverflow.com/q/225741/ asked by the user 'mattruma' ( https://stackoverflow.com/u/1768/ ) and on the answer https://stackoverflow.com/a/225854/ provided by the user 'PersistenceOfVision' ( https://stackoverflow.com/u/6721/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, comments, revision history etc. For example, the original title of the Question was: How do you set the StartPosition of a Windows Forms form using code?
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 2.5' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 2.5' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Understanding the StartPosition in Windows Forms
When working with Windows Forms in C#, developers often want to control where their forms open on the screen. One commonly used property is StartPosition. However, many face challenges when attempting to set this property programmatically instead of through the Property Editor. In this guide, we will address this issue in detail and provide a solution.
The Problem
You may find yourself in a situation where you've set the StartPosition of a form, yet it doesn't display in the position you expect. For example, you might have attempted to set it to FormStartPosition.CenterParent in your constructor like this:
[[See Video to Reveal this Text or Code Snippet]]
But when you show the form with Show(), it opens in a default position. This can be frustrating and leave you wondering why the code isn't working as intended.
Analyzing the Code
Let's take a closer look at how you're displaying the form. You might have a method that looks something like this:
[[See Video to Reveal this Text or Code Snippet]]
The issue here is that when you call frm.Show(this), the Parent of frm is set to null. This means that the CenterParent position will not be applied as expected.
The Discovery
A workaround found by other developers is to use a modal dialog with ShowDialog():
[[See Video to Reveal this Text or Code Snippet]]
However, in your case, this solution isn't ideal since you want your form to show as a regular window and not as a dialog box.
The Solution
To achieve the desired result while displaying your Windows Forms correctly, you can follow these steps:
1. Modify the Constructor
First, modify the DealsForm constructor to not set the StartPosition to CenterParent. Instead, you'll set it to Manual in the display routine:
[[See Video to Reveal this Text or Code Snippet]]
2. Use the Load Event
You'll then handle the Load event of your form to set its location based on the parent form:
[[See Video to Reveal this Text or Code Snippet]]
3. Updating the Show Method
Lastly, when displaying the form, set StartPosition to Manual before showing it:
[[See Video to Reveal this Text or Code Snippet]]
Conclusion
By utilizing the Load event and adjusting the StartPosition of your form, you can effectively control where your Windows Forms appear on the screen when displayed. This approach provides the functionality you require without turning your forms into modal dialogs, maintaining the seamless user experience you desire.
Final Thoughts
If you find yourself struggling with StartPosition in Windows Forms, remember this solution. It not only resolves the positioning problem but also allows you to maintain control over how your application interacts with users.
Повторяем попытку...

Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: