C# 如何使用 showdialog (.NET Compact Framework) 将表单居中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2076607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:18:39  来源:igfitidea点击:

How to center a form using showdialog (.NET Compact Framework)

c#.netcompact-framework

提问by Gopinath

I want to centre a popup form launched using Form.ShowDialog() in .NET Compact Framework. I dont see any property like StartPosition in .NET CF Form object.

我想将使用 Form.ShowDialog() 在 .NET Compact Framework 中启动的弹出窗体居中。我在 .NET CF Form 对象中没有看到任何像 StartPosition 这样的属性。

Can someone please suggest me how to centre popups in .NET CF 3.5?

有人可以建议我如何在 .NET CF 3.5 中居中弹出窗口吗?

采纳答案by Fredrik M?rk

You can make an extension method that does the work for you:

您可以制作一个为您完成工作的扩展方法:

public static class FormExtensions
{
    public static void CenterForm(this Form theForm)
    {
        theForm.Location = new Point(
            Screen.PrimaryScreen.WorkingArea.Width / 2 - theForm.Width / 2,
            Screen.PrimaryScreen.WorkingArea.Height / 2 - theForm.Height / 2);
    }
}

You call it like this:

你这样称呼它:

TheDialogForm f = new TheDialogForm();
f.CenterForm();            
f.ShowDialog();

回答by Vibin Jith

Set the left and Top properties on the of the form in 'frmDialog_Activated event

在 'frmDialog_Activated 事件中设置窗体的 left 和 Top 属性

Private Sub frmDialog_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        Me.Left = (frmMain.Width - Me.Width) / 2 ' AS Your Wish
        Me.Top = (frmMain.Height - Me.Height) / 2 + 165 '' AS Your Wish
    End Sub

回答by hoodoos

If you want your popup form appear in center of screen by default you can just set a starting position for it in form properties, it should sound like 'Center parent'.

如果您希望您的弹出表单默认显示在屏幕中央,您可以在表单属性中为其设置一个起始位置,它应该听起来像“中心父级”。

Something like this:

像这样的东西:

form1.StartPosition = FormStartPosition.CenterScreen;

回答by Saurav Bhowmick

If Parent is not defined for the Dialog then use

如果没有为对话框定义父级,则使用

login.StartPosition = FormStartPosition.CenterScreen;
login.ShowDialog(); 

where login is the FormObject

其中 login 是Form对象

or you can also use if you are calling on top of an existing Parent Form

或者,如果您在现有父级之上调用,也可以使用 Form

login.StartPosition = FormStartPosition.CenterParent;
login.ShowDialog();

This property can also be set in the Property dialog of the Form, if you think that the property is always the same for you. By default it should be set to CenterParent, which will not work in case you are invoking your Formbefore the Parent Formin some cases, like Login screen for the first time etc.

Form如果您认为该属性对您来说总是相同的,也可以在 的属性对话框中设置该属性。默认情况下,它应设置为 CenterParent,如果您在某些情况下(例如第一次登录屏幕等)Form在 Parent 之前调用它,则它不起作用Form

回答by Amr Mausad

this is the easiest way

这是最简单的方法

Form f= new AmrDealForm();
f.CenterToScreen();
f.ShowDialog();

回答by cristobalhdez

I know this is old post, but I had the same problem and I solved with this way:

我知道这是旧帖子,但我遇到了同样的问题,我用这种方式解决了:

I create an Interface:

我创建了一个接口:

public interface FormExtensions
    {
        void CenterForm(Form forma);
    }

After I did implements the interface on my class:

在我确实在我的班级上实现了接口之后:

    public partial class frmFirma : Form, FormExtensions
    {
        public frmFirma()
        {
            InitializeComponent();
        }
        public void CenterForm(Form forma)
        {
            forma.Location = new Point(
            Screen.PrimaryScreen.WorkingArea.Width / 2 - forma.Width / 2,
            Screen.PrimaryScreen.WorkingArea.Height / 2 - forma.Height / 2);
        }
    }

Then I can crate a instance of the: "frmFirma" an call the method "CenterForm":

然后我可以创建一个实例:“frmFirma”并调用方法“CenterForm”:

private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            Formas.frmFirma firma = new Formas.frmFirma();
            firma.CenterForm(firma);
            firma.ShowDialog();     
        }

I hope this works for someone.

我希望这对某人有用。