保存 Windows 窗体大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/225825/
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
Save Windows Form Size
提问by Joe Morgan
I'm developing a piece in VB.NET. Inside my primary form, I'm creating a new form to use as a dialog. I was wondering if there was a way to, upon the close of the new dialog, save it's size settings for each user (probably in a file on their machine, through XML or something?)
我正在 VB.NET 中开发一块。在我的主窗体中,我正在创建一个新窗体用作对话框。我想知道是否有办法在新对话框关闭时保存每个用户的大小设置(可能在他们机器上的文件中,通过 XML 或其他方式?)
采纳答案by Hath
you can save it to the settings file, and update it on the 'onclosing' event.
您可以将其保存到设置文件中,并在“onclosure”事件中更新它。
to make a setting goto Project Properties ->settings -> then make a setting like 'dialogsize' of type system.drawing.size.
要进行设置,请转到项目属性 -> 设置 -> 然后进行类似 system.drawing.size 类型的“dialogsize”的设置。
then do this in your dialog form:
然后在您的对话框中执行此操作:
Public Sub New()
InitializeComponent()
End Sub
Public Sub New(ByVal userSize As Size)
InitializeComponent()
Me.Size = userSize
End Sub
Protected Overrides Sub OnClosing(ByVal e As System.ComponentModel.CancelEventArgs)
MyBase.OnClosing(e)
My.Settings.DialogSize = Me.Size
My.Settings.Save()
End Sub
do something like this to check and use the setting:
做这样的事情来检查和使用设置:
Dim dlg As MyDialogWindow
If My.Settings.DialogSize.IsEmpty Then
dlg = New MyDialogWindow()
Else
dlg = New MyDialogWindow(My.Settings.DialogSize)
End If
dlg.ShowDialog()
回答by Kris Erickson
Although this is for C#, it will help with VB.Net as well.
尽管这是针对 C# 的,但它对 VB.Net 也有帮助。
回答by Waleed El-Safty
You can also add a new setting to your application (size) and set it to system.drawing.size
您还可以为您的应用程序添加一个新设置(大小)并将其设置为 system.drawing.size
Then, you make sure you save the current size to settings on close.
然后,确保在关闭时将当前大小保存到设置中。
Private Sub myForm_FormClosing(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles MyBase.FormClosing
My.Settings.size = Me.Size
My.Settings.Save()
End Sub
and on load you apply the size you have saved in settings
并在加载时应用您在设置中保存的大小
Private Sub myForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
' if this is the first time to load the form
' dont set the size ( the form will load with the size in the designe)
If Not My.Settings.size.IsEmpty Then
Me.Size = My.Settings.size
End If
End Sub
回答by Aaron
Here's a solution that I found onlinethat seems to work rather well for me.
这是我在网上找到的一个解决方案,似乎对我来说效果很好。
Some of the previously mentioned solutions weren't working for me as expected. Depending on where my form was positioned at the time of closing the form wouldn't get repositioned back to that exact location when I would load it again.
前面提到的一些解决方案没有按预期对我有用。取决于我的表单在关闭时的位置,当我再次加载它时,它不会重新定位回那个确切的位置。
This solution seems to do the trick by taking into account some other factors as well:
这个解决方案似乎通过考虑其他一些因素来解决问题:
You need to set up these two setting under Project Properties -> settings: WindowLocation and WindowSize like so:
您需要在 Project Properties -> settings: WindowLocation 和 WindowSize 下设置这两个设置,如下所示:
Then create the following function:
然后创建以下函数:
Private Sub LoadWindowPosition()
'Get window location/position from settings
Dim ptLocation As System.Drawing.Point = My.Settings.WindowLocation
'Exit if it has not been set (X = Y = -1)
If (ptLocation.X = -1) And (ptLocation.Y = -1) Then
Return
End If
'Verify the window position is visible on at least one of our screens
Dim bLocationVisible As Boolean = False
For Each S As Screen In Screen.AllScreens
If S.Bounds.Contains(ptLocation) Then
bLocationVisible = True
Exit For
End If
Next
'Exit if window location is not visible on any screen
If Not bLocationVisible Then
Return
End If
'Set Window Size, Location
Me.StartPosition = FormStartPosition.Manual
Me.Location = ptLocation
Me.Size = My.Settings.WindowSize
End Sub
Next, you'll need to add code to your form's load and closing events like so:
接下来,您需要将代码添加到表单的加载和关闭事件中,如下所示:
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LoadWindowPosition()
End Sub
Private Sub frmMain_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
My.Settings.WindowLocation = Me.Location
My.Settings.WindowSize = Me.Size
End Sub
I hope that helps.
我希望这有帮助。
回答by Keithius
You can also do this using the UI provided by the VB.NET IDE itself. In the properties pane for a form, look under the item called "(Application Settings)" and then under "Property Binding." You can bind just about every property of the form (including size and location) to a settings value for that application.
您也可以使用 VB.NET IDE 本身提供的 UI 来执行此操作。在表单的属性窗格中,查看名为“(应用程序设置)”的项目下,然后查看“属性绑定”下。您可以将表单的几乎每个属性(包括大小和位置)绑定到该应用程序的设置值。
回答by Joe Morgan
As it turns out, I found a way to do this using the System.IO.IsolatedStorage
事实证明,我找到了一种使用 System.IO.IsolatedStorage