This is a migrated thread and some comments may be shown as answers.

Creation of a custom tool and utilization of custom RadDiagramConnection along with the ConnectorTool

1 Answer 75 Views
Diagram, DiagramRibbonBar, DiagramToolBox
This is a migrated thread and some comments may be shown as answers.
Mohammad
Top achievements
Rank 1
Mohammad asked on 11 Nov 2020, 11:01 PM

Dear Telerik staff,

I am trying to insert a customized connection using the default ConnectorTool into my RadDiagram. However, this seems near impossible because the library does not expose a method to do that, as WPF does (https://www.telerik.com/forums/using-connection-tool-to-draw-custom-connection). Consequently, I decided to create a custom tool, but it is not working. I need a tool that provides the same functionality as the ConnectorTool, but that employs a customized connection that contains its own visual traits and a set of extra properties/variables.
My current code is as follows:

public class CustomTool : ToolBase, IMouseListener
    {
        public CustomTool(string name) : base(name)
        {
            this.Name = name;
        }
        IConnection currentConnection = null;
        Point lastPoint;
        public virtual bool MouseDown(PointerArgs e)
        {
            try
            {
                if (this.ToolService.ActiveTool != null && this.ToolService.ActiveTool.Name.Equals(this.Name))
                {
                     
                    HitTestService hitTestService = MainMenu.getDiagram().ServiceLocator.GetService<IHitTestService>() as HitTestService;
                    
                    if (this.currentConnection == null)
                    {
                        this.currentConnection = new CustomRadDiagramConnection(System.Drawing.Color.FromArgb(150, 45, 90), "test");
                         
                        if (hitTestService.ShapeUnderMouse != null)
                        {
                            this.currentConnection.Source = hitTestService.ShapeUnderMouse; 
                        }
                        this.currentConnection.StartPoint = e.TransformedPoint;
                         
                        this.currentConnection.EndPoint = this.lastPoint = e.TransformedPoint;
                        this.Graph.Items.Add((RadElement)this.currentConnection);
                        hitTestService = null;
                        return true;
                    }
                }
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("Not working");
            }
            return true;
        }
 
        protected override void OnActivated()
        {
            base.OnActivated();
            this.lastPoint = new Point(0, 0);
            this.currentConnection = null;
        }
 
        protected override void OnDeactivated()
        {
            base.OnDeactivated();
            this.ToolService.ActivateTool("Pointer Tool");
            this.Graph.AddConnection(currentConnection);
            //this.Graph.Items.Remove((RadElement)this.currentConnection);
            this.currentConnection = null;
        }
 
        public virtual bool MouseMove(PointerArgs e)
        {
 
            if (this.currentConnection != null && this.IsActive)
            {
                this.currentConnection.EndPoint = this.lastPoint = e.TransformedPoint;
                this.currentConnection.Update();
                return true;
            }
            return false;
        }
        public virtual bool MouseUp(PointerArgs e)
        {
            HitTestService hitTestService = MainMenu.getDiagram().ServiceLocator.GetService<IHitTestService>() as HitTestService;
            if (IsActive)
            {
                if (hitTestService.ShapeUnderMouse != null)
                {
                    this.currentConnection.Target = hitTestService.ShapeUnderMouse;
                }
                this.currentConnection.EndPoint = this.lastPoint;
                this.currentConnection.Update();
                this.DeactivateTool();
                hitTestService = null;
                return true;
            }
            return false;
        }
        public bool MouseDoubleClick(PointerArgs e)
        {
            return false;
        }
    }

 

Thank you

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 12 Nov 2020, 09:50 AM

Hello, Mohammad,   

Please have in mind that RadDiagram in the Telerik WPF and WinForms suites are two different controls each with its specific internal implementation. Hence, the provided API is not expected to be the same due to the product's specifics.

RadDiagram in the Telerik UI for WinForms suite provides an easy way to replace the default RadDiagramConnection with a custom one which I believe is your main goal. This can be achieved by using the following custom implementation by RadDiagram

this.radDiagram1.ActiveTool = MouseTool.ConnectorTool;
        public class CustomRadDiagramConnection : RadDiagramConnection
        {   
            public CustomRadDiagramConnection()
            {
                this.Content = DateTime.Now;
            }
        }

        public class CustomDiagramElement : RadDiagramElement
        {
            protected override IConnection GetConnectionContainerForItemOverride(object item)
            {
                return new CustomRadDiagramConnection( );
            }
        }

        public class CustomDiagram :  Telerik.WinControls.UI.RadDiagram
        {
            protected override RadDiagramElement CreateDiagramElement()
            {
                return new CustomDiagramElement();
            }
        }

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Diagram, DiagramRibbonBar, DiagramToolBox
Asked by
Mohammad
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or