How can I update/redraw a Microsoft Powerpacks canvas in C#?

134 Views Asked by At

I have a method which finds relationships between two objects, if it exists, I would like to draw two Lineshapes to the link. I have started to implement the first lineshape, however whenever I test the code the lines persist. I have tried multiple methods (as you can see) and these do not refresh the canvas for the new lines to be drawn.

private void DrawRelationshipLines()
            {
                _canvas = new ShapeContainer {Parent = panelCredentialsVisualisation};
                //These methods below do not redraw the canvas
                _canvas.Shapes.Remove(_tableinfoLine);
                _canvas.Shapes.Clear();
                _canvas.Refresh();
                _canvas.Update();
                //

                List<string> relationships = lvSelectedTableInfoCredentialsIntersection.GetAllRelationships();

                if (relationships.Capacity == 0)
                    return;

                foreach (string context in relationships)
                {
                    Label contextLabelName = GetLabelByName(context);
                    _tableinfoLine = new LineShape
                    {
                        Parent = _canvas,
                        BorderWidth = 2,
                        BorderColor = Color.BlueViolet,
                        StartPoint = new Point(lblselectedTableinfo.Right, lblselectedTableinfo.Top + 10),
                        EndPoint = new Point(contextLabelName.Left, contextLabelName.Top + 10)
                    };
    }

The code works fine in searching for relationships and drawing them, however I'd like to be able to clear the canvas before drawing a different relationship, is this possible?

Thanks if anyone can help.

1

There are 1 best solutions below

0
On BEST ANSWER

Moving _canvas = new ShapeContainer {Parent = panelCredentialsVisualisation}; outside of the method made this work. Seems like initializing a new ShapeContainer each time was causing issues.