The first post in this mini series can be found here:
Preserve Size and Location of Windows Forms – Part I
Here I will discuss how we must do to preserve the size and location of Windows Forms that are part of managed COM Add-in and VSTO Add-ins. We can still use the approach that was discussed in the first post however we need to add the following code into the closing event of the Windows Form:
Private Sub frmMain_FormClosing(ByVal sender As Object, _ ByVal e As _ System.Windows.Forms.FormClosingEventArgs) _ Handles Me.FormClosing My.Settings.Save() End Sub
As already pointed out by Mike Rosenblum; if we use C# then we need to save the settings for the Windows Form also in standalone applications.
Just for fun I made some additional solutions where we grab a Windows Form’s location and size and use them:
Imports System.Text
'...
Private Sub btnLocation_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnLocation.Click
Dim LocationStrBuilder As New StringBuilder("XY : " + _
Me.Location.X.ToString + _
" " + _
Me.Location.Y.ToString)
LocationStrBuilder.AppendLine()
LocationStrBuilder.Append("TopLeft: " + _
Me.Top.ToString + _
" " + _
Me.Left.ToString)
LocationStrBuilder.AppendLine()
LocationStrBuilder.Append("Desktop: " + _
Me.DesktopLocation.X.ToString + _
" " + _
Me.DesktopLocation.Y.ToString)
MessageBox.Show(LocationStrBuilder.ToString)
End Sub
Private Sub btnSize_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnSize.Click, btnSize.Click
Dim SizeStrBuilder As New StringBuilder("Height: " + _
Me.Height.ToString)
SizeStrBuilder.AppendLine()
SizeStrBuilder.Append("Width : " + _
Me.Width.ToString)
MessageBox.Show(SizeStrBuilder.ToString)
End Sub
Private Sub btnDesktop_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnDesktop.Click
'Of course, each variable can be set on its own.
Me.SetDesktopBounds(x:=Me.Location.X + 10, _
y:=Me.Location.Y + 5, _
width:=Me.Width + 50, _
height:=Me.Height + 100)
End Sub
'...
That’s all for now and in an upcoming post I will discuss anchoring and docking controls on Windows Forms.
Kind regards,
Dennis