保存Windows表单大小

问题描述 投票:7回答:6

我正在VB.NET中开发一个片段。在主表单中,我正在创建一个新表单以用作对话框。我想知道是否有办法在新对话框关闭时为每个用户保存其大小设置(可能是通过XML或其他方式保存在其计算机上的文件中?)

windows vb.net winforms preferences savestate
6个回答
7
投票

您可以将其保存到设置文件,并在'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()

2
投票

尽管this is for C#,它也会对VB.Net有所帮助。


2
投票

您还可以将新设置添加到应用程序(大小)并将其设置为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

0
投票

您还可以使用VB.NET IDE本身提供的UI来执行此操作。在表单的属性窗格中,在“(应用程序设置)”项下,然后在“属性绑定”下。您可以将表单的几乎每个属性(包括大小和位置)绑定到该应用程序的设置值。


0
投票

事实证明,我找到了一种使用System.IO.IsolatedStorage的方法来进行此操作>


0
投票

这是I found online的解决方案,对我来说似乎效果很好。

© www.soinside.com 2019 - 2024. All rights reserved.