0

i'm building a C# winform application to display points on a chart. I'm using a combobox to select the ChartType of each Serie. When trying to put uncompatible ChartType together (point and doughnut for example), the application is crashing.

I'm trying to catch this exception with a try-catch syntax but i can't make it work.

private void Cb_curve_type_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                _chart.Series[_serie_name].ChartType = (SeriesChartType)cb_curve_type.SelectedIndex;
                _chart.Update();
            } catch (InvalidOperationException ex)
            {
                MessageBox.Show("Impossible to mix chart type", "Error", MessageBoxButtons.OK);
                cb_curve_type.SelectedIndex = (int)_chart.Series[_serie_name].ChartType;
                return;
            }            
        }

I tried to remove the (InvalidOperationException ex) to catch all exceptions but it still not working, and the program is throwing a System.InvalidOperationException on _chart.Update()

Here's the stack trace :

   à System.Windows.Forms.DataVisualization.Charting.ChartTypes.PieChart.Paint(ChartGraphics graph, CommonElements common, ChartArea area, Series seriesToDraw)
   à System.Windows.Forms.DataVisualization.Charting.ChartArea.Paint(ChartGraphics graph)
   à System.Windows.Forms.DataVisualization.Charting.ChartPicture.Paint(Graphics graph, Boolean paintTopLevelElementOnly)
   à System.Windows.Forms.DataVisualization.Charting.Chart.OnPaint(PaintEventArgs e)
   à System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
   à System.Windows.Forms.Control.WmPaint(Message& m)
   à System.Windows.Forms.Control.WndProc(Message& m)
   à System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   à System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   à System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
TmZn
  • 399
  • 5
  • 16
  • if you just make it a catch(Exception ex), does that catch it? – CarCar Sep 06 '19 at 18:35
  • @CarCar Nop, still throwing the same ```InvalidOperationException```. I also tried to add the line ```System.Windows.Forms.Application.SetUnhandledExceptionMode(System.Windows.Forms.UnhandledExceptionMode.CatchException);``` in my main.cs but it's opening a debugger window and not my custom my MessageBox – TmZn Sep 06 '19 at 18:40
  • Can we see the stack trace from the exception? You'll have to paste it as text into your question. – 15ee8f99-57ff-4f92-890c-b56153 Sep 06 '19 at 18:42
  • @EdPlunkett I've had the stack trace – TmZn Sep 06 '19 at 18:44
  • It's not being thrown from within your try/catch. Looks like it's happening on the next paint message. I'd put some care into validating that `cb_curve_type.SelectedIndex` and `_chart.Series[_serie_name]` make as much sense as you think they do. Also look into calling `Refresh()` instead of `Update()`. I think Update() may not be what you want. In fact, try that first. – 15ee8f99-57ff-4f92-890c-b56153 Sep 06 '19 at 18:46
  • @EdPlunkett I've came to the same conclusion, using ```Refresh()`` doesn't seem to work either. What do you mean by "validating" selected index ? – TmZn Sep 06 '19 at 18:54
  • Put in a breakpoint and make sure `cb_curve_type.SelectedIndex` is a valid value for `chart.Series[_serie_name].ChartType`. – 15ee8f99-57ff-4f92-890c-b56153 Sep 06 '19 at 18:55
  • @EdPlunkett since users can add as much series as they want, I have an unknown amount of curves, and there's no way for me to check if this particular SelectedIndex is compatible with all of the others Series, or maybe there's a solution I'm not seeing right now... – TmZn Sep 06 '19 at 18:57
  • OK, so all you can reasonably do is just catch the exception? – 15ee8f99-57ff-4f92-890c-b56153 Sep 06 '19 at 19:08
  • Exactly, but it seems like I can't catch it here in my code, and I'm running out of ideas to solve this problem. – TmZn Sep 06 '19 at 19:11
  • 2
    You went through [all the stuff here for unhandled exceptions](https://stackoverflow.com/a/5762806/424129)? – 15ee8f99-57ff-4f92-890c-b56153 Sep 06 '19 at 19:19
  • Unfortunately that's exactly the same as only adding ```Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);``` and it's not handling the error as I want to do it – TmZn Sep 06 '19 at 20:00

1 Answers1

0

You can create a derived class from Chart control to handle the exception:

public class ChartEx : Chart
{
    public event EventHandler CustomEvent;

    protected override void OnPaint(PaintEventArgs e)
    {
        try
        {
            base.OnPaint(e);
        }
        catch (InvalidOperationException ex)
        {
            if (CustomEvent != null)
            {
                CustomEvent(this, e);
            }
        }
    }
}

Handle the event in your main class.

qian
  • 11
  • 2