# Events

## Overview

The provided code exposes two primary events—**LowerValueChanged** and **UpperValueChanged**—that trigger whenever the corresponding slider thumb's value is modified. These events supply developers with event arguments that include the updated value and the current range difference, which is useful for responding immediately to user interactions (such as dragging, keyboard navigation, mouse wheel adjustments, or context menu selections) as well as programmatic changes.

The following sections document the details, usage, best practices, and troubleshooting strategies for integrating and utilizing these events.

***

### Event Details

<table><thead><tr><th width="205">Event Name</th><th>EventArgs Type</th><th>Description</th><th>Triggered By</th></tr></thead><tbody><tr><td>LowerValueChanged</td><td>OnLowerValueChangedEventArgs</td><td>Raised when the lower thumb’s value changes; the event arguments include the new value and the difference between upper and lower values.</td><td>User interaction or programmatic changes</td></tr><tr><td>UpperValueChanged</td><td>OnUpperValueChangedEventArgs</td><td>Raised when the upper thumb’s value changes; the event arguments include the new value and the difference between upper and lower values.</td><td>User interaction or programmatic changes</td></tr></tbody></table>

***

### Key Points

<table><thead><tr><th width="214">Aspect</th><th>Details</th></tr></thead><tbody><tr><td>Triggering Mechanism</td><td>Both events are fired on value changes, either via user actions (drag, mouse wheel, keyboard, context menu) or via code.</td></tr><tr><td>Event Arguments</td><td>The arguments provide the new value (<code>Value</code>) and the current difference between the slider values (<code>ValuesDifference</code>).</td></tr><tr><td>Integration</td><td>Developers can subscribe to these events to update UI elements or trigger additional logic when slider values change.</td></tr><tr><td>Responsiveness</td><td>The events support real-time feedback, essential for interactive applications.</td></tr></tbody></table>

***

### Best Practices

| Practice                    | Description                                                                                            | Example or Recommendation                                                                                 |
| --------------------------- | ------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------- |
| Validate Event Data         | Always check the event arguments to ensure that the values are within expected bounds.                 | Use `e.Value` and `e.ValuesDifference` to update UI elements and validate data before processing further. |
| Keep Handlers Lightweight   | Avoid performing heavy or blocking operations inside the event handlers to maintain UI responsiveness. | Offload complex logic to background threads or tasks, or use asynchronous programming patterns if needed. |
| Unsubscribe When Not Needed | Remove event handlers when the control is disposed to prevent memory leaks.                            | In the control’s Dispose method, unsubscribe using `-=` for both events.                                  |
| Provide User Feedback       | Use the event data to update related UI components immediately, ensuring that users see live changes.  | Update labels, progress bars, or data bindings in response to the events.                                 |

***

### Common Pitfalls

| Pitfall                      | Description                                                                                                | Recommendation                                                                        |
| ---------------------------- | ---------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------- |
| Heavy Processing in Handlers | Performing time-consuming tasks inside the event handlers may cause UI lag.                                | Delegate heavy processing to background workers or use async/await where appropriate. |
| Ignoring Event Data          | Failing to use the provided event arguments can lead to missed opportunities for validation or UI updates. | Always extract and utilize `e.Value` and `e.ValuesDifference` in your logic.          |
| Not Unsubscribing Handlers   | Leaving event subscriptions active when the control is disposed can lead to memory leaks.                  | Unsubscribe event handlers during disposal or when the control is no longer needed.   |

***

### Usage Scenarios

| Scenario                   | Description                                                                                 | Sample Code Example                                                                            |
| -------------------------- | ------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------- |
| Real-Time Display Update   | Update labels or text boxes with the current slider values as the user adjusts the control. | See the code sample below that updates two labels with the new lower and upper values.         |
| Data Synchronization       | Synchronize slider changes with underlying data models or remote services.                  | Trigger data filtering or service calls when either event fires.                               |
| Conditional UI Adjustments | Enable or disable other UI elements based on the current range of values.                   | Use the event data to determine when to enable a “Submit” button if a valid range is selected. |

#### Code Example: Updating UI in Real Time

```csharp
// Assuming you have two labels: lowerValueLabel and upperValueLabel
myHRangeSlider.LowerValueChanged += (sender, e) =>
{
    lowerValueLabel.Text = $"Lower Value: {e.Value}";
    // Optionally, use e.ValuesDifference to update related UI elements
};

myHRangeSlider.UpperValueChanged += (sender, e) =>
{
    upperValueLabel.Text = $"Upper Value: {e.Value}";
    // Optionally, adjust other components based on the new range
};
```

***

### Real Life Usage Scenarios

<table><thead><tr><th width="231">Scenario</th><th>Description</th><th>Use Case Example</th></tr></thead><tbody><tr><td>Financial Dashboard</td><td>Adjusting a date or value range filter in a financial analysis dashboard.</td><td>Use the slider events to update charts or tables with data corresponding to the selected range.</td></tr><tr><td>Media Editing Software</td><td>Selecting a segment of a timeline for editing audio or video.</td><td>Respond to slider events to display the current selection and preview the segment.</td></tr><tr><td>Data Visualization Tools</td><td>Filtering datasets in real time based on user-selected numerical ranges.</td><td>Bind the slider events to refresh or re-query data displayed in charts and graphs.</td></tr></tbody></table>

***

### Troubleshooting Tips

<table><thead><tr><th width="225">Issue</th><th>Possible Cause</th><th>Suggested Fix</th></tr></thead><tbody><tr><td>Event Not Firing</td><td>Handler not attached, or control not properly initialized.</td><td>Ensure that the event handlers are subscribed after control initialization (e.g., in Form_Load).</td></tr><tr><td>Inconsistent UI Updates</td><td>Heavy processing in the event handlers causing delays.</td><td>Move processing logic to a background thread or use asynchronous methods to keep the UI responsive.</td></tr><tr><td>Memory Leaks</td><td>Handlers not unsubscribed when the control is disposed.</td><td>Ensure that event handlers are detached in the control’s Dispose method.</td></tr></tbody></table>

***

### Integration Example

Below is a comprehensive example demonstrating how to integrate the events into a WinForms application:

```csharp
using System;
using System.Windows.Forms;
using SiticoneNetFrameworkUI; // Namespace containing SiticoneHRangeSlider

namespace RangeSliderDemo
{
    public partial class MainForm : Form
    {
        private SiticoneHRangeSlider rangeSlider;
        private Label lowerValueLabel;
        private Label upperValueLabel;

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

        private void InitializeCustomComponents()
        {
            // Initialize the slider control
            rangeSlider = new SiticoneHRangeSlider
            {
                Minimum = 0,
                Maximum = 100,
                LowerValue = 25,
                UpperValue = 75,
                SnapToTick = true,
                Step = 1,
                Dock = DockStyle.Top,
                Height = 100
            };

            // Subscribe to the events
            rangeSlider.LowerValueChanged += RangeSlider_LowerValueChanged;
            rangeSlider.UpperValueChanged += RangeSlider_UpperValueChanged;

            // Initialize labels to display current values
            lowerValueLabel = new Label { Left = 10, Top = 110, Width = 200, Text = "Lower Value: 25" };
            upperValueLabel = new Label { Left = 10, Top = 140, Width = 200, Text = "Upper Value: 75" };

            // Add controls to the form
            Controls.Add(rangeSlider);
            Controls.Add(lowerValueLabel);
            Controls.Add(upperValueLabel);
        }

        private void RangeSlider_LowerValueChanged(object sender, SiticoneHRangeSlider.OnLowerValueChangedEventArgs e)
        {
            lowerValueLabel.Text = $"Lower Value: {e.Value}";
            // Additional logic can be added here
        }

        private void RangeSlider_UpperValueChanged(object sender, SiticoneHRangeSlider.OnUpperValueChangedEventArgs e)
        {
            upperValueLabel.Text = $"Upper Value: {e.Value}";
            // Additional logic can be added here
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new MainForm());
        }
    }
}
```

***

### Review

<table><thead><tr><th width="258">Aspect</th><th>Evaluation</th></tr></thead><tbody><tr><td>Flexibility</td><td>The events provide a flexible mechanism to monitor and react to value changes, covering both user and programmatic inputs.</td></tr><tr><td>Ease of Integration</td><td>With clearly defined event arguments and simple subscription patterns, integration into existing applications is straightforward.</td></tr><tr><td>Performance Considerations</td><td>The control’s event system is optimized for responsiveness, provided that event handlers remain lightweight.</td></tr></tbody></table>

***

### Summary

<table><thead><tr><th width="235">Summary Point</th><th>Details</th></tr></thead><tbody><tr><td>Event Purpose</td><td>To notify developers about changes to the slider’s lower and upper values.</td></tr><tr><td>Trigger Conditions</td><td>Events are fired on user interactions (drag, mouse wheel, keyboard, context menu) as well as programmatic changes.</td></tr><tr><td>Recommended Practices</td><td>Use lightweight handlers, validate event arguments, and unsubscribe when appropriate to maintain performance and memory integrity.</td></tr><tr><td>Integration Benefit</td><td>Enables real-time UI updates, data synchronization, and dynamic application responses based on slider interactions.</td></tr></tbody></table>

***

### Additional Resources

<table><thead><tr><th width="196">Resource</th><th>Description</th><th>Link/Reference</th></tr></thead><tbody><tr><td>Code Comments</td><td>The source code includes inline comments that clarify event triggering and usage patterns.</td><td>Refer to the provided code file.</td></tr><tr><td>API Documentation</td><td>Use this documentation in conjunction with the full API documentation of the SiticoneNetFrameworkUI control.</td><td>N/A (code provided)</td></tr></tbody></table>

***

This comprehensive documentation for the Events feature provides detailed insights into how the SiticoneHRangeSlider control notifies developers of value changes, how to integrate these events effectively into your application, and best practices to ensure smooth operation and maintainable code.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs-siticoneframework.gitbook.io/home/net-framework-or-net-core-ui/sliders-and-range/siticone-hrangeslider/events.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
