Page 1 of 1

Programmatically Creating DrawLineItem Error Null Handles Exception

Posted: Mon Nov 09, 2020 7:52 am
by 13049545
I'm adding DrawLineItems to the Lines collection and that's fine --

Until the chart tries to refresh and throws an exception because all the "Handles" fields such as EndHandle, MidlleBottomHandle, MiddleTopHandle, and Starthandle are Null so a NullReferenceException is thrown.

My question is how to either populate the Handle values or avoid the exception when programmatically adding DrawLineItems to a .Net TeeChart?

Error
Error.gif
Error.gif (52.58 KiB) Viewed 1298 times
My populate code:
howtopopulate.gif
howtopopulate.gif (51.04 KiB) Viewed 1298 times
Null "Handle's" in DrawLineItems
nullDrawLineItems.gif
nullDrawLineItems.gif (48.27 KiB) Viewed 1298 times
Also when I add the line to the DrawLines Lines collection it doesn't throw the newline event like when I draw the line on the screen by hand with the mouse.

Thanks for your help.

Joseph

Re: Programmatically Creating DrawLineItem Error Null Handles Exception

Posted: Mon Nov 09, 2020 10:05 am
by Christopher
Hello Joseph,

I think the reason is that you are using the parameterless constructor of DrawLineItem, which means that EndHandle will throw an error (following code is from the sourcecode):

Code: Select all

        public Rectangle EndHandle
        {
            get
            {
                return RectangleFromPoint(tool.AxisPoint(EndPos));
            }
        }
It will throw an error because the tool variable is null - this is the parameter you can pass in the other constructor of DrawLineItem, e.g.

Code: Select all

        private void InitializeChart(TChart chart)
        {
            chart.Series.Add(typeof(Line)).FillSampleValues();

            var drawLine = new DrawLine(chart.Chart);

            var item = new DrawLineItem(drawLine); //<-- pass in the 'tool' here
            item.Pen.Width = 3;
            item.Pen.Color = Color.Red;
            item.StartPos = new PointDouble(1, 1);
            item.EndPos = new PointDouble(3, chart.Series[0].YValues.Maximum);

            drawLine.Lines.Add(item);
        }

Re: Programmatically Creating DrawLineItem Error Null Handles Exception

Posted: Mon Nov 09, 2020 6:16 pm
by 13049545
Christopher - u r correct! I feel dumb for missing the constructor that takes the drawline object as a param.
fixed.gif
fixed.gif (60.87 KiB) Viewed 1285 times
Thanks soooooo much!!!

Re: Programmatically Creating DrawLineItem Error Null Handles Exception

Posted: Tue Nov 10, 2020 8:51 am
by Christopher
Hello Joseph!
jzarech wrote:
Mon Nov 09, 2020 6:16 pm
Thanks soooooo much!!!
You are very welcome! Don't forget that debugging such issues is considerably easier with the source code :)