无法以编程方式在 CenterScreen 中获取表单
Doesn't get a form in CenterScreen programmatically
我正在尝试以编程方式在中心屏幕中获取表单。
在Else部分的代码中完美执行。
Dim X%, Y%
Call FrmCommonCodes.FormLocationXYValues(X, Y)
If X = 100 And Y = 100 Then
'Me.StartPosition = FormStartPosition.CenterScreen
Me.StartPosition = Windows.Forms.FormStartPosition.CenterScreen
Else
Me.Location = New System.Drawing.Point(X, Y)
End If
''FrmCommonCodes.FormLocationXYValues()
Public Sub FormLocationXYValues(ByRef X As Double, ByRef Y As Double)
Using MyConnection As OleDb.OleDbConnection = GetConnection(), MyCommand As New OleDb.OleDbCommand("SELECT * FROM options", MyConnection)
If MyConnection.State = ConnectionState.Closed Then MyConnection.Open()
Using MyDataReader As OleDb.OleDbDataReader = MyCommand.ExecuteReader
While MyDataReader.Read
X = MyDataReader("formlocation_x")
Y = MyDataReader("formlocation_y")
Return
End While
End Using
End Using
End Sub
查看您的代码,您应该在设计器中将表单的 StartPosition
属性 设置为 CenterScreen
。然后,如果 X 和 Y 坐标与您的默认值匹配,则表单将已经位于正确的位置。这将简化代码,并解决如果表单已经开始,则为表单设置起始位置无效的问题:
Dim X As Integer, Y As Integer
Call FrmCommonCodes.FormLocationXYValues(X, Y)
If X <> 100 OrElse Y <> 100 Then
'Default is "CenterScreen", if this code does not run
Me.Location = New System.Drawing.Point(X, Y)
End If
此外,您可能希望将此代码移动到窗体的构造函数中,或者至少在显示窗体之前调用一个方法。否则,您可以看到表单加载到 CenterScreen 然后跳转到所需的位置。
只有一行:
Me.CenterToScreen()
我正在尝试以编程方式在中心屏幕中获取表单。 在Else部分的代码中完美执行。
Dim X%, Y%
Call FrmCommonCodes.FormLocationXYValues(X, Y)
If X = 100 And Y = 100 Then
'Me.StartPosition = FormStartPosition.CenterScreen
Me.StartPosition = Windows.Forms.FormStartPosition.CenterScreen
Else
Me.Location = New System.Drawing.Point(X, Y)
End If
''FrmCommonCodes.FormLocationXYValues()
Public Sub FormLocationXYValues(ByRef X As Double, ByRef Y As Double)
Using MyConnection As OleDb.OleDbConnection = GetConnection(), MyCommand As New OleDb.OleDbCommand("SELECT * FROM options", MyConnection)
If MyConnection.State = ConnectionState.Closed Then MyConnection.Open()
Using MyDataReader As OleDb.OleDbDataReader = MyCommand.ExecuteReader
While MyDataReader.Read
X = MyDataReader("formlocation_x")
Y = MyDataReader("formlocation_y")
Return
End While
End Using
End Using
End Sub
查看您的代码,您应该在设计器中将表单的 StartPosition
属性 设置为 CenterScreen
。然后,如果 X 和 Y 坐标与您的默认值匹配,则表单将已经位于正确的位置。这将简化代码,并解决如果表单已经开始,则为表单设置起始位置无效的问题:
Dim X As Integer, Y As Integer
Call FrmCommonCodes.FormLocationXYValues(X, Y)
If X <> 100 OrElse Y <> 100 Then
'Default is "CenterScreen", if this code does not run
Me.Location = New System.Drawing.Point(X, Y)
End If
此外,您可能希望将此代码移动到窗体的构造函数中,或者至少在显示窗体之前调用一个方法。否则,您可以看到表单加载到 CenterScreen 然后跳转到所需的位置。
只有一行:
Me.CenterToScreen()