Events and Callbacks

Provides hooks for external application logic by notifying when key interactions occur, such as state changes and double-click actions.

Overview

The Events feature in the SiticonePlayPauseButton control offers two primary events—StateChanged and DoubleClickActionExecuted—that enable developers to integrate custom behaviors into their applications. These events fire when the play/pause state changes or when a double-click action is executed, allowing external code to respond immediately to user interactions.


Feature Details

The table below summarizes the key events associated with the control:

Event
Type
Description

StateChanged

EventHandler

Raised whenever the play/pause state (IsPlaying) changes, providing a hook for state-dependent logic.

DoubleClickActionExecuted

EventHandler

Triggered when the double-click action is executed (if enabled), allowing developers to perform custom actions.

Note: These events are designed to work seamlessly with the control’s behavior and are raised automatically as part of the control’s internal event-handling logic.


Code Examples

Basic Event Integration

The following example demonstrates how to subscribe to and handle the StateChanged and DoubleClickActionExecuted events within a WinForms application:

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

namespace EventsDemo
{
    public partial class MainForm : Form
    {
        private SiticonePlayPauseButton playPauseButton;

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

        private void InitializeEventsDemo()
        {
            playPauseButton = new SiticonePlayPauseButton
            {
                Location = new Point(50, 50),
                Size = new Size(80, 80),
                IsPlaying = false, // initial state
                EnableDoubleClickAction = true,
                DoubleClickActionCommand = "Stop"
            };

            // Subscribe to events
            playPauseButton.StateChanged += PlayPauseButton_StateChanged;
            playPauseButton.DoubleClickActionExecuted += PlayPauseButton_DoubleClickActionExecuted;

            this.Controls.Add(playPauseButton);
        }

        private void PlayPauseButton_StateChanged(object sender, EventArgs e)
        {
            // Respond to state changes, such as updating a status label or logging
            Console.WriteLine("State changed: IsPlaying = " + playPauseButton.IsPlaying);
        }

        private void PlayPauseButton_DoubleClickActionExecuted(object sender, EventArgs e)
        {
            // Execute custom logic on double-click, for example resetting a timer or updating UI elements
            MessageBox.Show("Double-click action executed: " + playPauseButton.DoubleClickActionCommand);
        }
    }
}

Advanced Event Handling

In this advanced example, the application dynamically toggles the double-click action and logs state changes to demonstrate integration with additional UI elements:

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

namespace AdvancedEventsDemo
{
    public partial class EventsForm : Form
    {
        private SiticonePlayPauseButton playPauseButton;
        private Button toggleDoubleClickButton;

        public EventsForm()
        {
            InitializeComponent();
            InitializeAdvancedEvents();
        }

        private void InitializeAdvancedEvents()
        {
            playPauseButton = new SiticonePlayPauseButton
            {
                Location = new Point(70, 30),
                Size = new Size(100, 100),
                IsPlaying = false,
                EnableDoubleClickAction = true,
                DoubleClickActionCommand = "Stop"
            };

            toggleDoubleClickButton = new Button
            {
                Text = "Toggle Double-Click Action",
                Location = new Point(70, 150),
                Size = new Size(160, 30)
            };

            toggleDoubleClickButton.Click += (s, e) =>
            {
                playPauseButton.EnableDoubleClickAction = !playPauseButton.EnableDoubleClickAction;
                MessageBox.Show("Double-click action enabled: " + playPauseButton.EnableDoubleClickAction);
            };

            // Subscribe to events with inline handlers
            playPauseButton.StateChanged += (s, e) =>
            {
                Console.WriteLine("Advanced state changed: IsPlaying = " + playPauseButton.IsPlaying);
            };

            playPauseButton.DoubleClickActionExecuted += (s, e) =>
            {
                MessageBox.Show("Advanced double-click executed: " + playPauseButton.DoubleClickActionCommand);
            };

            this.Controls.Add(playPauseButton);
            this.Controls.Add(toggleDoubleClickButton);
        }
    }
}

Key Points

Aspect
Details

Event Triggering

The StateChanged event fires when the IsPlaying property toggles, while DoubleClickActionExecuted fires upon a valid double-click action.

Integration

Events can be subscribed to via standard event handler syntax in .NET, enabling integration with other application components.

Customizability

Developers can add custom logic within event handlers to update UI elements, log actions, or trigger additional processes.

User Feedback

Proper event handling provides immediate and clear feedback to the user, reinforcing the control's interactivity.


Best Practices

Recommendation
Rationale

Always unsubscribe from events when no longer needed

Prevent memory leaks and unintended behavior by detaching event handlers, especially in dynamic or long-running applications.

Use descriptive logging in event handlers

Help diagnose issues and track state changes by logging event data during development.

Validate event data if necessary

Ensure that any custom logic executed within event handlers accounts for potential exceptions or invalid state.

Test events with all supported input methods

Verify that mouse, keyboard, and double-click interactions consistently trigger the appropriate events.


Common Pitfalls

Issue
Explanation
Prevention/Remedy

Missing event subscriptions

Failing to attach event handlers will result in no external response to state changes.

Ensure that events are properly subscribed in your initialization code.

Overlapping event logic

Complex or overlapping logic within event handlers may lead to unexpected behavior.

Keep event handler code modular and well-organized, and use logging for debugging.

Neglecting to manage event unsubscription

Not removing event handlers when controls are disposed can lead to memory leaks.

Unsubscribe events appropriately, especially in dynamic or frequently updated UIs.


Usage Scenarios

Scenario
Description
Sample Code Reference

Media Control Applications

Use the StateChanged event to update media playback indicators and UI elements when toggling play/pause.

Refer to the Basic Event Integration sample.

Interactive Dashboards

Integrate the DoubleClickActionExecuted event to trigger actions like resetting progress or initiating stop commands.

Refer to the Advanced Event Handling sample.

Accessibility-Focused Interfaces

Combine event notifications with alternative feedback mechanisms (e.g., auditory cues) for improved accessibility.

Integrated in both samples through event logging.


Review

When reviewing event integration for the SiticonePlayPauseButton control, consider the following checklist:

Checklist Item
Recommendation

Event subscription correctness

Verify that all necessary events are subscribed and that event handlers are functioning as expected.

Logical separation of event handling

Ensure that event logic is separated and modular, allowing for easier debugging and maintenance.

Adequate logging and feedback

Confirm that events provide sufficient feedback (via logs, UI updates, or alerts) during user interactions.

Proper unsubscription management

Check that event handlers are detached when controls are disposed to prevent memory leaks.


Summary

The Events feature of the SiticonePlayPauseButton control facilitates responsive and interactive behavior by providing two key events—StateChanged and DoubleClickActionExecuted. By subscribing to these events, developers can integrate custom logic that reacts to play/pause state changes and double-click actions, thereby enhancing application functionality and user feedback. Proper implementation of event handling is critical for creating dynamic, interactive, and accessible .NET WinForms applications.


Additional Sections

Integration Tips

Tip
Explanation

Leverage built-in .NET event patterns

Utilize the familiar event subscription model in .NET to integrate these events seamlessly into your application's architecture.

Test event responses under different scenarios

Simulate various user interactions (click, double-click, keyboard inputs) to ensure that events fire correctly.

Document event behavior in your application

Clearly explain within your application’s documentation how these events affect control behavior and UI updates.

Demo Projects

To further illustrate the Events feature, consider developing demo applications such as:

Demo Feature
Description

Media Controller Simulator

A demo that simulates a media player, where state changes and double-click actions trigger additional UI updates.

Interactive Dashboard Widget

An application that responds to various user events by updating real-time data displays, charts, or notifications.

Accessibility Compliance Tool

A tool designed to verify that event-based interactions provide appropriate feedback across multiple input methods.

By following this comprehensive documentation, developers can effectively integrate and utilize the Events feature of the SiticonePlayPauseButton control, ensuring that custom application logic responds correctly to user interactions and enhances the overall interactivity of .NET WinForms applications.

Last updated