保存Windows窗体大小
-
03-07-2019 - |
题
我正在用VB.NET开发一篇文章。在我的主要表单中,我正在创建一个用作对话框的新表单。我想知道是否有办法在新对话框结束时保存每个用户的大小设置(可能是在他们的机器上的文件中,通过XML或其他东西?)
解决方案
您可以将其保存到设置文件中,并在'onclosing'事件中更新它。
进行设置转到项目属性 - >设置 - >然后进行类似system.drawing.size的“dialogsize”设置。
然后在对话框中执行此操作:
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
执行以下操作以检查并使用设置:
Dim dlg As MyDialogWindow
If My.Settings.DialogSize.IsEmpty Then
dlg = New MyDialogWindow()
Else
dlg = New MyDialogWindow(My.Settings.DialogSize)
End If
dlg.ShowDialog()
其他提示
尽管这是针对C#的,它也有助于VB.Net。
您还可以向应用程序添加新设置(大小)并将其设置为 system.drawing.size
然后,确保将当前大小保存到关闭时的设置。
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
并在加载时应用您在设置中保存的大小
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
您也可以使用VB.NET IDE本身提供的UI来执行此操作。在表单的属性窗格中,查看名为“(应用程序设置)”的项目。然后在“Property Binding”下。您可以将表单的每个属性(包括大小和位置)绑定到该应用程序的设置值。
事实证明,我找到了一种使用 System.IO.IsolatedStorage
不隶属于 StackOverflow