Chart events vs. annotation events

A forum dedicated to WinForms version of LightningChart Ultimate.

Moderator: Queue Moderators

Post Reply
Niels Decker
Posts: 12
Joined: Mon Nov 22, 2021 8:12 am

Chart events vs. annotation events

Post by Niels Decker » Mon Dec 20, 2021 10:03 am

Hi, I have a chart with several annotations (among other stuff), and I want to catch the events of right-clicking on the chart and right-clicking on an annotation. Each right-click should be handled by its own event handler function. This works for the right-click on the chart, but does not work for the event handler of the annotations. Their click event is not handled by their own event handler, but by the charts event handler too.

Is there a way to manipulate the order of events, or use other types of events (MouseClick, MouseDown, MouseUp etc.) in order to be able to handle a right-click event of an annotation without getting "overriden" by the charts events? Many thanks for help!

Code: Select all

private void Chart_MouseDown(object sender, MouseEventArgs e)
{
    // handle the right-click event of the chart
}

private void Annot_MouseClick(object sender, MouseEventArgs e)
{
     // handle the right-click event of the annotation
}

Arction_LasseP
Posts: 141
Joined: Wed Mar 27, 2019 1:05 pm

Re: Chart events vs. annotation events

Post by Arction_LasseP » Mon Dec 20, 2021 11:28 am

Hello Niels,

If you have subscribed to MouseDown/MouseClick events for both the chart and the annotation, they will both trigger. This cannot be prevented. However, there are several ways to prevent the code inside the chart's event to execute when an Annotation is clicked.

-If you are using the same event type (MouseClick, MouseDown etc.) for the chart and the Annotations, the event for the Annotations triggers first. You can add a simple boolean variable, which is set true inside the Annotation event. Then use this to prevent the chart event.

Code: Select all

private void Anno_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        annoClicked = true;
        // Do something
    }
}

private void _chart_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        if (!annoClicked)
        {
            // Do something
        }
        else
        {
            annoClicked = false;
        }
    }
}
-If your chart uses MouseDown event while the Annotation use MouseClick event as is seen in your code sample, the above will not work since MouseDown is always triggered before MouseClick. In this case, you can check inside the chart's event if the current location is over an Annotation.

Code: Select all

private void _chart_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        bool isOver = false;
        foreach (var anno in _chart.ViewXY.Annotations)
        {
            if (anno.IsPositionOver(e.X, e.Y))
            {
                isOver = true;
            }
        }

        if (!isOver)
        {
            // Do something
        }
    }
}
With the above, it is actually possible to not subscribe to any events for the Annotations. You could execute the Annotation click code if IsPositionOver hits true.

Best regards,
Lasse

Niels Decker
Posts: 12
Joined: Mon Nov 22, 2021 8:12 am

Re: Chart events vs. annotation events

Post by Niels Decker » Mon Jan 10, 2022 7:30 am

Hi Lasse, the second approach works very well (except that it is IsMouseOver() instead of IsPositionOver()), thanks very much! I didn't test the first approach.

Best regards
Niels

ArctionKestutis
Posts: 552
Joined: Mon Mar 14, 2016 9:22 am

Re: Chart events vs. annotation events

Post by ArctionKestutis » Mon Jan 10, 2022 8:56 am

Method IsMouseOver() was renamed to IsPositionOver() in version 10.0.1.4001.
There are more changes of names in that version, because we made UWP compatible charts.

Post Reply