It is not too difficult to make a Form's location persistent, so that it can be repositioned where it was on restart:
Private Sub FlatForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Location = My.Settings.Location
End Sub
Private Sub FlatForm_LocationChanged(sender As Object, e As EventArgs) Handles Me.LocationChanged
My.Settings.Location = Location
End Sub
Private Sub FlatForm_Disposed(sender As Object, e As EventArgs) Handles Me.Disposed
My.Settings.Save()
End Sub
However, I have a user control being placed on many of my apps, so I was thinking of intercepting the parent's according events, in order to automatically do this for them as a service.
For example for the Disposed
event:
Private Sub MyUserControl_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler ParentForm.Disposed, AddressOf ParentDisposed
End Sub
But I have no clue on how to access the parent form's Settings
property in the handler:
Private Sub ParentDisposed(sender As Object, e As EventArgs)
?
End Sub
Surely I can not add My.Settings.Save()
, because I do not want to save the user control's location, but the parent form's in its own folder.
Is this possible, or need I do this a completely different way?
Thanks to a comment by user Visual Vincent, this is a working solution.
If you have placed a user control on a Windows Form, you can have automatic form tracking including persistency between calls.
In the control's Load
event, this property is retrieved and applied to the parent form. Note, that we are coding the control's event, not the client form's. This means, that the client form may already be shown (yes, even before the Load
event finishes), depending on what you do in it. This means, that we should place the according code to the event's very top.
It might seem natural, that the parent's location should be updated and saved in the Disposed
event, but upon arriving there, the reference to ParentForm
is Nothing
already. This is also true, if the parent's Disposed
event is handled by the control via AddHandler
. Instead, we must use the parent's intercepted LocationChanged
event to track the form's movements and to keep our property up-to-date.
Private Sub MyControl_Load(sender As Object, e As EventArgs) _
Handles MyBase.Load
'Retrieve the parent form's Location setting and apply it.
ParentForm.Location = My.Settings.Location
'Intercept the parent's LocationChanged event, so that its Location
'property can be updated.
AddHandler ParentForm.LocationChanged, AddressOf ParentLocationChanged
...
End Sub
'This routine is called, when the parent form is moved. It is intercepted in
'order to update the application's Location setting.
Private Sub ParentLocationChanged(sender As Object, e As EventArgs)
My.Settings.Location = ParentForm.Location
End Sub
It is in the control's Dispose
event, in which the settings are stored, including the Location
setting.
You may want to opt out of this and have the client only be saving its own settings.
'When done, the application's settings are saved.
Private Sub MyControl_Disposed(sender As Object, e As EventArgs) _
Handles Me.Disposed
My.Settings.Save()
End Sub
From now on, when such a control is placed on a Form control, it automatically also monitors its movements and stores the form's position persistently between two sessions.
Parent
财产:
YourControle.Parent.Location
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.