保存 Windows 窗体大小

发布于 2024-07-07 21:46:34 字数 117 浏览 12 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

机场等船 2024-07-14 21:46:34

您可以将其保存到设置文件中,并在“onclose”事件时更新它。

要进行设置,请转到项目属性 -> 设置 -> 然后进行类似 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()

you can save it to the settings file, and update it on the 'onclosing' event.

to make a setting goto Project Properties ->settings -> then make a setting like 'dialogsize' of type system.drawing.size.

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()
离笑几人歌 2024-07-14 21:46:34

虽然这是针对C#,它对 VB.Net 也有帮助。

Although this is for C#, it will help with VB.Net as well.

ま昔日黯然 2024-07-14 21:46:34

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

You can also add a new setting to your application (size) and set it to 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
很快妥协 2024-07-14 21:46:34

这是我在网上找到的解决方案 这似乎对我来说效果很好。

前面提到的一些解决方案没有按预期为我工作。 根据我的表单在关闭时所处的位置,当我再次加载表单时,该表单不会重新定位回该确切位置。

该解决方案似乎还通过考虑其他一些因素来解决问题:

您需要在“项目属性”->“项目属性”下设置这两个设置。 设置: 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

我希望有所帮助。

Here's a solution that I found online that 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:

enter image description here

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.

怎樣才叫好 2024-07-14 21:46:34

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

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.

非要怀念 2024-07-14 21:46:34

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

As it turns out, I found a way to do this using the System.IO.IsolatedStorage

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文