【问题标题】:Save Windows Form Size保存 Windows 窗体大小
【发布时间】:2008-10-22 13:55:32
【问题描述】:

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

【问题讨论】:

    标签: windows vb.net winforms preferences savestate


    【解决方案1】:

    您可以将其保存到设置文件中,并在“关闭”事件时对其进行更新。

    要进行设置,请转到项目属性 -> 设置 -> 然后进行类似 system.drawing.size 类型的“对话框大小”的设置。

    然后在您的对话框中执行此操作:

    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()
    

    【讨论】:

    • 在我的 WinForm 设计器中,我进入属性下,我看到 (PropertyBinding),但我找不到 DialogSize 属性。这是您在代码中还是在设计器中手动设置的?
    • DialogSize 是您需要在设置文件中创建的全局变量。转到项目->'你的项目名称'属性->设置选项卡->添加一个名为“DialogSize”的设置或任何你喜欢的类型 System.Drawing.Size 不幸的是大小不在属性绑定位中
    • 好的 - 我明白你在说什么并设置了它,但是现在当我尝试调用它时,我得到一个 ConfigurationErrorsException,说配置系统无法初始化。我该如何解决这个问题?
    【解决方案2】:

    虽然this is for C#,但它对VB.Net 也有帮助。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

        前面提到的一些解决方案没有像预期的那样对我有用。根据我的表单在关闭时的位置,当我再次加载它时,表单不会重新定位回那个确切的位置。

        这个解决方案似乎通过考虑其他一些因素来达到目的:

        你需要在 Project Properties -> settings 下设置这两个设置:WindowLocation 和 WindowSize 像这样:

        然后创建如下函数:

        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
        

        接下来,您需要向表单的加载和关闭事件添加代码,如下所示:

        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
        

        我希望这会有所帮助。

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            【解决方案6】:

            事实证明,我找到了一种使用 System.IO.IsolatedStorage 的方法

            【讨论】:

            • 有趣——你为什么选择IsolatedStorage?
            猜你喜欢
            • 2010-11-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-12-19
            • 1970-01-01
            • 2013-04-30
            • 2011-10-04
            • 1970-01-01
            相关资源
            最近更新 更多