Vb.net 无法将窗体定位到中心

Vb.net 无法将窗体定位到中心,vb.net,winforms,Vb.net,Winforms,我有一张表格叫Form1。它被设置为startup Position=Center,但当执行时,它会在其他地方打开(每次在一个随机位置) 我在WindowsXPSP3下工作,使用IDEVisualStudio-2010。请提供此问题的解决方法 我上传了一个显示上述问题的示例项目 下载链接: 您必须设置: Form1.StartPosition = FormStartPosition.Manual 编辑: 以下是一个工作示例: Dim X As Integer = (Screen.Primary

我有一张表格叫Form1。它被设置为startup Position=Center,但当执行时,它会在其他地方打开(每次在一个随机位置)

我在WindowsXPSP3下工作,使用IDEVisualStudio-2010。请提供此问题的解决方法

我上传了一个显示上述问题的示例项目

下载链接:

您必须设置:

Form1.StartPosition = FormStartPosition.Manual
编辑: 以下是一个工作示例:

Dim X As Integer = (Screen.PrimaryScreen.Bounds.Width - Me.Width) / 2
Dim Y As Integer = (Screen.PrimaryScreen.Bounds.Height - Me.Height) / 2
Me.StartPosition = FormStartPosition.Manual
Me.Location = New System.Drawing.Point(X, Y)
编辑2: 以下是基于Hans Passant评论的改进代码(更好):


在您的问题中,不太清楚您实际尝试了什么,因为表单的StartPosition属性没有“Center”这样的选项

但是,将StartPosition设置为CenterScreen或
Me.StartPosition=FormStartPosition.CenterScreen
如果您是以编程方式进行的,那么您应该能够得到您所需要的


参考:

在调整屏幕大小后尝试使用此选项

Me.Size = New System.Drawing.Size(800, 436)
Me.CenterToScreen()
以下是解决方案:

    Dim screen__1 As Screen = Screen.FromControl(frm)
    Dim workingArea As Rectangle = screen__1.WorkingArea
    frm.Location = New Point() With { _
     .X = Math.Max(workingArea.X, workingArea.X + (workingArea.Width - frm.Width) / 2), _
     .Y = Math.Max(workingArea.Y, workingArea.Y + (workingArea.Height - frm.Height) / 2) _
    }
第二条:

    'frm = is the form object
    Dim X As Integer = (Screen.PrimaryScreen.Bounds.Width - frm.Width) / 2
    Dim Y As Integer = (Screen.PrimaryScreen.Bounds.Height - frm.Height) / 2
    frm.StartPosition = FormStartPosition.Manual
    frm.Location = New System.Drawing.Point(X, Y)
对于VB.net 2010 放置代码以形成加载事件

呼叫中心屏幕()


这是VS提供的内置方法,如何帮助我将表单居中?您必须设置该值,然后将form1.startPosition值设置为所需的起始位置。如果startposition未设置为手动,则它将选择从何处开始。如果是手动的,则由您决定从何处开始。现在,你可以手动计算起始位置,将其放置在屏幕的中心。但是,如果我希望我的表单位于中心,我如何计算x,y坐标(位置)?你能用一个例子更新你的答案吗?假设主屏幕是0,0。不一定是这样。将Bounds.Left和Bounds.Top添加到X和Y值。你应该使用工作区域,而不是边界。并且表单可能不一定在主屏幕上打开,请使用screen.FromPoint。并且应该在Load事件中完成,以处理AutoScaleMode。细节,细节。@Hans Passant:如果你能把上面的内容写进一个答案,这样像我这样的新手就可以清楚地理解它,避免任何误解,那就更好了。(只是一个建议:D)如果“StartupPosition”属性在我将其设置为“中心屏幕”后仍无法在中心打开,那么它可能与“StartupPosition”属性的需求重复请注意我对您的答案所做的编辑。我在代码行前面放了4个空格,表示它们是代码,并且对它们应用了格式。
    'frm = is the form object
    Dim X As Integer = (Screen.PrimaryScreen.Bounds.Width - frm.Width) / 2
    Dim Y As Integer = (Screen.PrimaryScreen.Bounds.Height - frm.Height) / 2
    frm.StartPosition = FormStartPosition.Manual
    frm.Location = New System.Drawing.Point(X, Y)