element109

Here's my code, what am I missing

Dim UserPref As New Microsoft.Win32.UserPreferenceChangedEventHandler(AddressOf HandlePrefChange)

Public Sub New()

InitializeComponent()

UserPref. Don't know how to begin receiving events

End Sub

Private Sub HandlePrefChange(ByVal sender As Object, ByVal e As Microsoft.Win32.UserPreferenceChangedEventArgs)

If e.Category = Microsoft.Win32.UserPreferenceCategory.Screensaver Then

MsgBox("Screensaver Settings Changed")

End If

End Sub



Re: Windows Forms Data Controls and Databinding UserPreferenceChangedEventHandler

tkerns

All you should need is an AddHandler statement:

AddHandler Microsoft.Win32.SystemEvents.UserPreferenceChanged, Me.UserPref

Note that the documentation states:

"Because this is a static event, you must detach your event handlers when your application is disposed, or memory leaks will result."

It's probably why the built-in controls that handle this event override OnHandleCreated and OnHandleDestroyed to add and remove the EventHandlers, respectively.

Tony





Re: Windows Forms Data Controls and Databinding UserPreferenceChangedEventHandler

element109

I had already tried that, it does not work. I am assuming the message box should appear if I change the default screensaver

Dim UserPref As New Microsoft.Win32.UserPreferenceChangedEventHandler(AddressOf HandlePrefChange)

Public Sub New()

InitializeComponent()

AddHandler Microsoft.Win32.SystemEvents.UserPreferenceChanged, Me.UserPref

End Sub

Private Sub HandlePrefChange(ByVal sender As Object, ByVal e As Microsoft.Win32.UserPreferenceChangedEventArgs)

If e.Category = Microsoft.Win32.UserPreferenceCategory.Screensaver Then

MsgBox("Screensaver Settings Changed")

End If

End Sub

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

RemoveHandler Microsoft.Win32.SystemEvents.UserPreferenceChanged, Me.UserPref

End Sub





Re: Windows Forms Data Controls and Databinding UserPreferenceChangedEventHandler

tkerns

Here is my code:

Option Explicit On

Option Strict On

Imports Microsoft.Win32

Imports System.Windows.Forms

Public Class MyForm1

Protected Overrides Sub OnHandleCreated(ByVal e As System.EventArgs)

MyBase.OnHandleCreated(e)

AddHandler SystemEvents.UserPreferenceChanged, New UserPreferenceChangedEventHandler(AddressOf Me.UserPreferenceChangedHandler)

End Sub

Protected Overrides Sub OnHandleDestroyed(ByVal e As System.EventArgs)

RemoveHandler SystemEvents.UserPreferenceChanged, New UserPreferenceChangedEventHandler(AddressOf Me.UserPreferenceChangedHandler)

MyBase.OnHandleDestroyed(e)

End Sub

Private Sub UserPreferenceChangedHandler(ByVal sender As Object, ByVal e As UserPreferenceChangedEventArgs)

If (e.Category = UserPreferenceCategory.Screensaver) Then

MessageBox.Show(Me, "Screensaver Settings Changed")

End If

End Sub

End Class 'MyForm1

What's funny is that the MessageBox never pops up when I change a screensaver setting. But if I set a breakpoint at the If statement, it gets hit. It must have something to do with the application not having the focus.

Tony





Re: Windows Forms Data Controls and Databinding UserPreferenceChangedEventHandler

element109

I tried your code as well, it did not work for me either.

About the form not having focus suggestion, I don't think that's the case because my form modifies the registry setting directly.

HKEY_CURRENT_USER\Control Panel\Desktop\SCRNSAVE.EXE

I also tried changing the screensaver from display options just to be sure, but the event did not fire.

I still think we are missing a call to a sub or function.

Dim UserPref As New Microsoft.Win32.UserPreferenceChangedEventHandler(AddressOf UserPreferenceChangedHandler)

UserPref.

I'm going to look it up at the MSDN2 library and see if there is a sample.

In the MSDN2 library I found these two samples and neither worked:

'One

Public Delegate Sub UserPreferenceChangedEventHandler(ByVal sender As Object, ByVal e As UserPreferenceChangedEventArgs)

Dim instance As New UserPreferenceChangedEventHandler(AddressOf UserPreference)

Protected Overrides Sub OnHandleCreated(ByVal e As System.EventArgs)

MyBase.OnHandleCreated(e)

End Sub

Protected Overrides Sub OnHandleDestroyed(ByVal e As System.EventArgs)

MyBase.OnHandleDestroyed(e)

End Sub

Private Sub UserPreference(ByVal sender As Object, ByVal e As UserPreferenceChangedEventArgs)

MessageBox.Show(Me.Text, "Settings Changed")

End Sub

 

 

'Two

Public Shared Event UserPreferenceChanged As UserPreferenceChangedEventHandler

Dim handler As UserPreferenceChangedEventHandler

Protected Overrides Sub OnHandleCreated(ByVal e As System.EventArgs)

MyBase.OnHandleCreated(e)

AddHandler SystemEvents.UserPreferenceChanged, handler

End Sub

Protected Overrides Sub OnHandleDestroyed(ByVal e As System.EventArgs)

RemoveHandler SystemEvents.UserPreferenceChanged, handler

MyBase.OnHandleDestroyed(e)

End Sub

Private Sub UserPreference(ByVal sender As Object, ByVal e As UserPreferenceChangedEventArgs) Handles Me.UserPreferenceChanged

MessageBox.Show(Me.Text, "Settings Changed")

End Sub

 

I even removed the screensaver category and still nothing





Re: Windows Forms Data Controls and Databinding UserPreferenceChangedEventHandler

tkerns

After a little more testing, the code that I posted previously worked when I changed the category from Screensaver to General in the If..Then statement. For some reason, changing the Screensaver or the Wait time raises the event with a Category of General.

Tony





Re: Windows Forms Data Controls and Databinding UserPreferenceChangedEventHandler

element109

It stated on the MSDN2 libray that the events value would be nothing in VB (but not in C#), that was what prompted me to remove the category check, but I only tried that with the two approaches they recommended. I tried your approach with the category check removed and it worked how I hoped it would. When the registry value is changed from my program the event does not fire but when the screensaver is changed from the display options dialog the event fires.

Thanks for your help.