Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > UserPreferenceChangedEventHandler
 

UserPreferenceChangedEventHandler

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

element109  Thursday, January 11, 2007 8:17 AM

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

tkerns  Monday, January 15, 2007 12:55 AM

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

tkerns  Thursday, January 11, 2007 1:55 PM

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

element109  Thursday, January 11, 2007 11:06 PM

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

tkerns  Friday, January 12, 2007 4:02 AM

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

element109  Sunday, January 14, 2007 8:38 AM

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

tkerns  Monday, January 15, 2007 12:55 AM

It stated onthe 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 registryvalue 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.

element109  Monday, January 15, 2007 2:25 AM

You can use google to search for other answers

Custom Search

More Threads

• databinding custom objects - lookup combobox
• Databinding textBoxes
• GetSchema() returns wrong value of IsUnique field.
• how to compare input to a value in a database
• syntax help for textbox.databindings
• how to set the SqlParameter as field reference ?
• Converting ObjectCollection to CheckedItemCollection
• Checkbox column in DataGridView
• equivalent of Datagrid.CurrentRowIndex in DatagridView 2.0?
• where to open my connection object?