Adding markers

The markers of a chart are located as SubMembers under the respective axes.
In order to add a marker, an object of the Marker class is instanced and the axis is added using the "AddMember" method.
After creating the marker, method events are added in order to update the tables on the interface.
The new cursor, including its color, is then added in the table. Furthermore, the Marker_NewMarkerValues method is called in order to update the values in the table.

The created marker is always inserted at position '0' by default. In order for the marker to appear in the visible axis area, the currently visible area can be queried at the associated axis in order to be able to calculate the position of the marker. The currently visible range can be queried with the delegate "GetDisplayRange". The two transferred parameters return the minimum and maximum value of the visible axis after the call.

private void btnAddXMarker_Click(object sender, EventArgs e)
{
    try
    {
        Marker marker = new Marker();
        marker.Position = 0;
        marker.Orientation = TwinCAT.Measurement.Scope.API.CursorAlignment.Vertical;
        marker.Locked = false;
        axisGroup.SubMember.OfType<MarkerContainer>().First().AddMember(marker);

        marker.NewMarkerValues += Marker_NewMarkerValues;
        marker.PositionChanged += XMarker_PositionChanged;
        dataGridView1.Rows.Insert(axisGroup.SubMember.OfType<MarkerContainer>().First().SubMember.OfType<Marker>().Where(x => x.Orientation == TwinCAT.Measurement.Scope.API.CursorAlignment.Vertical).Count() - 1, marker.Position, marker.Title);
        dataGridView1.Rows[axisGroup.SubMember.OfType<MarkerContainer>().First().SubMember.OfType<Marker>().Where(x => x.Orientation == TwinCAT.Measurement.Scope.API.CursorAlignment.Vertical).Count() - 1].Cells[1].Style.ForeColor = marker.DisplayColor;

        IndexAxis indexAxis = axisGroup.FirstOrDefault<IndexAxis>();
        if(indexAxis != null && indexAxis.GetDisplayRange != null)
        {
            indexAxis.GetDisplayRange.Invoke(out double min, out double max);
            marker.Position = min + ((max - min) / 2);
        }

        Marker_NewMarkerValues(marker, null);
    }
    catch (Exception) { }
}

private void btnAddYMarker_Click(object sender, EventArgs e)
{
    try
    {
        Marker marker = new Marker();
        marker.Orientation = TwinCAT.Measurement.Scope.API.CursorAlignment.Horizontal;
        marker.Locked = false;
        axisGroup.SubMember.OfType<MarkerContainer>().First().AddMember(marker);
        marker.PositionChanged += YMarker_PositionChanged;
        dataGridView2.Rows.Insert(axisGroup.SubMember.OfType<MarkerContainer>().First().SubMember.OfType<Marker>().Where(x => x.Orientation == TwinCAT.Measurement.Scope.API.CursorAlignment.Horizontal).Count() - 1, marker.Title, marker.Position);
        dataGridView2.Rows[axisGroup.SubMember.OfType<MarkerContainer>().First().SubMember.OfType<Marker>().Where(x => x.Orientation == TwinCAT.Measurement.Scope.API.CursorAlignment.Horizontal).Count() - 1].DefaultCellStyle.ForeColor = marker.DisplayColor;

        ValueAxis valueAxis = axisGroup.FirstOrDefault<ValueAxis>();
        if(valueAxis != null && valueAxis.GetDisplayRange != null)
        {
            valueAxis.GetDisplayRange.Invoke(out double min, out double max);
            marker.Position = min + ((max - min) / 2);
        }
    }
    catch (Exception) { }
}