---
description: >-
  A feature that enables developers to subscribe to system theme and accent
  color changes to dynamically update their application's UI.
---

# Theme & Accent Events

## Overview

This feature exposes the events `ThemeChanged`, `AccentColorChanged`, and `HighContrastChanged` from the `SiticoneThemeTracker` component, allowing the application to react when the system's theme or accent color changes. By subscribing to these events, developers can implement custom logic to update controls, refresh layouts, or trigger other UI adjustments as needed.

***

### Key Points

<table><thead><tr><th width="162">Aspect</th><th>Details</th></tr></thead><tbody><tr><td>Events</td><td><code>ThemeChanged</code> (provides updated <code>SystemTheme</code>), <code>AccentColorChanged</code> (provides updated <code>Color</code>), <code>HighContrastChanged</code> (indicates high contrast mode changes)</td></tr><tr><td>Data Provided</td><td><code>ThemeChangedEventArgs</code>, <code>AccentColorChangedEventArgs</code>, <code>HighContrastChangedEventArgs</code></td></tr><tr><td>Usage</td><td>Use events to execute custom UI update logic when the system theme or accent color changes</td></tr><tr><td>Integration</td><td>Events can be easily integrated by subscribing within the form or control initialization code</td></tr></tbody></table>

***

### Best Practices

<table><thead><tr><th width="280">Practice</th><th>Description</th></tr></thead><tbody><tr><td>Always Unsubscribe</td><td>Unsubscribe from events during disposal or form closure to avoid memory leaks or unexpected behavior</td></tr><tr><td>Centralize UI Updates</td><td>Implement a centralized method for handling UI refreshes in response to these events to ensure consistency across the application</td></tr><tr><td>Validate Event Data</td><td>Always validate the event arguments to ensure correct handling of the theme or accent changes (e.g., checking for valid colors or theme states)</td></tr><tr><td>Use Event Handlers for Logging</td><td>Use event handlers to log theme or accent changes for debugging or analytics purposes</td></tr></tbody></table>

***

### Common Pitfalls

<table><thead><tr><th width="298">Pitfall</th><th>Explanation</th></tr></thead><tbody><tr><td>Not Unsubscribing</td><td>Failing to remove event handlers on form close may lead to memory leaks or null reference exceptions when events are triggered post-disposal.</td></tr><tr><td>Overcomplicating the Event Logic</td><td>Embedding too much logic in the event handler can lead to performance issues; it is better to call lightweight methods that update the UI accordingly.</td></tr><tr><td>Ignoring High Contrast Mode</td><td>Overlooking the <code>HighContrastChanged</code> event might result in UI elements that are not accessible for users with high contrast requirements.</td></tr></tbody></table>

***

### Usage Scenarios

<table><thead><tr><th width="260">Scenario</th><th>Description</th></tr></thead><tbody><tr><td>Dynamic Theme Adaptation</td><td>Subscribe to <code>ThemeChanged</code> to automatically refresh the UI when the user switches between dark and light modes.</td></tr><tr><td>Accent Color Integration</td><td>Use the <code>AccentColorChanged</code> event to update the styling of interactive elements (e.g., buttons, links) to match the new system accent color.</td></tr><tr><td>Accessibility Enhancement</td><td>Handle the <code>HighContrastChanged</code> event to adjust UI elements for improved accessibility, such as increasing font sizes or changing contrast ratios.</td></tr></tbody></table>

***

### Real Life Usage Scenarios

<table><thead><tr><th width="296">Scenario</th><th>Description</th></tr></thead><tbody><tr><td>Responsive UI for Enterprise Apps</td><td>In an enterprise application where branding and accessibility are crucial, events can trigger a re-rendering of dashboards or forms when system themes change.</td></tr><tr><td>Customizable User Interfaces</td><td>Applications that allow user-customizable interfaces can listen for these events to automatically adjust their color schemes and layout configurations dynamically.</td></tr><tr><td>Analytics &#x26; Debugging</td><td>Log events such as <code>ThemeChanged</code> or <code>AccentColorChanged</code> to track user behavior or debug issues related to UI responsiveness across different system settings.</td></tr></tbody></table>

***

### Troubleshooting Tips

<table><thead><tr><th width="206">Issue</th><th>Suggested Resolution</th></tr></thead><tbody><tr><td>Event Not Firing</td><td>Verify that the component is properly initialized and that the operating system settings are correctly triggering the events (e.g., changing themes in Windows settings).</td></tr><tr><td>Memory Leaks</td><td>Ensure that all event subscriptions are removed during the disposal of the form or component to prevent lingering references.</td></tr><tr><td>Incorrect Event Data</td><td>Debug the event arguments (<code>ThemeChangedEventArgs</code>, <code>AccentColorChangedEventArgs</code>, <code>HighContrastChangedEventArgs</code>) to confirm that they contain the expected values.</td></tr></tbody></table>

***

### Integration Example

Below is an example demonstrating how to integrate and use the Theme & Accent Events feature in a .NET WinForms application.

```csharp
using System;
using System.Drawing;
using System.Windows.Forms;
using SiticoneNetFrameworkUI;

namespace ThemeEventsDemo
{
    public partial class MainForm : Form
    {
        private SiticoneThemeTracker themeTracker;

        public MainForm()
        {
            InitializeComponent();
            InitializeThemeEvents();
        }

        private void InitializeThemeEvents()
        {
            // Instantiate the theme tracker
            themeTracker = new SiticoneThemeTracker();

            // Subscribe to theme change events
            themeTracker.ThemeChanged += ThemeTracker_ThemeChanged;
            themeTracker.AccentColorChanged += ThemeTracker_AccentColorChanged;
            themeTracker.HighContrastChanged += ThemeTracker_HighContrastChanged;

            // Optionally display initial theme and accent color
            UpdateUIBasedOnTheme();
        }

        private void ThemeTracker_ThemeChanged(object sender, ThemeChangedEventArgs e)
        {
            // Update the UI when the theme changes
            Console.WriteLine("Theme changed to: " + e.CurrentTheme);
            UpdateUIBasedOnTheme();
        }

        private void ThemeTracker_AccentColorChanged(object sender, AccentColorChangedEventArgs e)
        {
            // Update the UI with the new accent color
            Console.WriteLine("Accent color changed to: " + e.AccentColor);
            UpdateUIBasedOnTheme();
        }

        private void ThemeTracker_HighContrastChanged(object sender, HighContrastChangedEventArgs e)
        {
            // Adjust the UI for high contrast mode
            Console.WriteLine("High contrast mode is " + (e.IsHighContrast ? "enabled" : "disabled"));
            UpdateUIBasedOnTheme();
        }

        private void UpdateUIBasedOnTheme()
        {
            // Use the current theme and accent color to update UI elements
            this.BackColor = themeTracker.CurrentTheme == SystemTheme.Dark ? Color.FromArgb(32, 32, 32) : SystemColors.Window;
            labelThemeInfo.Text = "Theme: " + themeTracker.CurrentTheme;
            labelAccentInfo.Text = "Accent: " + themeTracker.AccentColor;
        }

        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            // Unsubscribe from events and dispose of the theme tracker
            themeTracker.ThemeChanged -= ThemeTracker_ThemeChanged;
            themeTracker.AccentColorChanged -= ThemeTracker_AccentColorChanged;
            themeTracker.HighContrastChanged -= ThemeTracker_HighContrastChanged;
            themeTracker.Dispose();
            base.OnFormClosed(e);
        }
    }
}
```

***

### Code Sample Explanation

<table><thead><tr><th width="163">Section</th><th>Explanation</th></tr></thead><tbody><tr><td>Initialization</td><td>The <code>SiticoneThemeTracker</code> is instantiated and event handlers for <code>ThemeChanged</code>, <code>AccentColorChanged</code>, and <code>HighContrastChanged</code> are attached.</td></tr><tr><td>Event Handling</td><td>Each event handler logs changes and updates the UI by calling <code>UpdateUIBasedOnTheme</code>, ensuring that the user interface reflects the current system settings.</td></tr><tr><td>UI Update</td><td>The method <code>UpdateUIBasedOnTheme</code> adjusts the form's background and updates labels based on the current theme and accent color.</td></tr><tr><td>Disposal</td><td>Event handlers are unsubscribed and the theme tracker is disposed of in the <code>OnFormClosed</code> method to prevent resource leaks and ensure proper cleanup.</td></tr></tbody></table>

***

### Review

<table><thead><tr><th width="223">Aspect</th><th>Review Comments</th></tr></thead><tbody><tr><td>Ease of Integration</td><td>Event-based integration allows for seamless updates to the UI without polling for changes.</td></tr><tr><td>Responsiveness</td><td>Quick reaction to system theme or accent color changes enhances the user experience by providing an adaptive UI.</td></tr><tr><td>Resource Management</td><td>Proper subscription and unsubscription practices are critical to avoid memory leaks, as demonstrated in the sample integration.</td></tr></tbody></table>

***

### Summary

<table><thead><tr><th width="192">Summary Aspect</th><th>Summary Details</th></tr></thead><tbody><tr><td>Functionality</td><td>Provides event notifications for changes in system theme, accent color, and high contrast mode, enabling dynamic and responsive UI updates.</td></tr><tr><td>Developer Benefits</td><td>Simplifies the process of reacting to system changes with minimal code overhead and clear event-driven patterns.</td></tr><tr><td>Integration Ease</td><td>Straightforward integration through event subscriptions and lightweight event handlers that can be easily extended to include custom UI logic.</td></tr></tbody></table>

***

### Additional Useful Sections

#### Integration Checklist

<table><thead><tr><th width="273">Checklist Item</th><th>Status/Notes</th></tr></thead><tbody><tr><td>Component Instantiation</td><td>Ensure <code>SiticoneThemeTracker</code> is instantiated at the start of the form or application.</td></tr><tr><td>Event Subscriptions</td><td>Subscribe to <code>ThemeChanged</code>, <code>AccentColorChanged</code>, and <code>HighContrastChanged</code> events appropriately.</td></tr><tr><td>Centralized UI Update Method</td><td>Create a dedicated method (e.g., <code>UpdateUIBasedOnTheme</code>) to handle UI refresh logic uniformly.</td></tr><tr><td>Unsubscribe on Disposal</td><td>Unsubscribe from all events in the <code>OnFormClosed</code> or <code>Dispose</code> method to prevent memory leaks.</td></tr><tr><td>Logging and Debugging</td><td>Optionally add logging within event handlers to monitor system changes for debugging purposes.</td></tr></tbody></table>

***

#### FAQ

| Question                                        | Answer                                                                                                                                                     |
| ----------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| How do I subscribe to theme changes?            | Instantiate the `SiticoneThemeTracker` and subscribe to its `ThemeChanged` event using a suitable event handler.                                           |
| What should I do if my UI does not update?      | Ensure that your event handlers are correctly attached, the UI update method is being called, and that no custom code overrides the event-driven changes.  |
| How can I handle high contrast mode changes?    | Subscribe to the `HighContrastChanged` event and adjust your UI elements (e.g., font size, control borders) to be more accessible based on the event data. |
| Is it necessary to unsubscribe from the events? | Yes, unsubscribing from events when the form or component is disposed of is important to prevent memory leaks and unintended behavior after disposal.      |

***

This comprehensive documentation for the Theme & Accent Events feature provides detailed guidance, examples, and best practices to help developers integrate and utilize the event-driven capabilities of the `SiticoneThemeTracker` component in their .NET WinForms applications.
