How to show and hide ports on mouse hover in WinForms Diagram?
In the WinForms Diagram Control, you can control the visibility of ports when the mouse hovers over a node. By default, the ports added to nodes are set to Visible
false. To make the ports visible when the mouse is hovering over the node, and to hide the ports when the mouse leaves the node you can utilize the MouseMove event.
Code snippet:
Node globalNode;
diagram1.MouseMove += Diagram1_MouseMove;
//Method to visible the port when mouse hover on node.
private void Diagram1_MouseMove(object sender, MouseEventArgs e)
{
Node node1 = (Node)this.diagram1.Controller.GetNodeUnderMouse(new Point(e.X, e.Y));
if (node1 != null)
{
globalNode = node1;
int x = e.X - (int)node1.BoundingRectangle.X;
int y = e.Y - (int)node1.BoundingRectangle.Y;
for (int i = 0; i < node1.Ports.Count; i++)
{
node1.Ports[i].Visible = true;
if (Math.Abs(x - node1.Ports[i].GetPosition().X) < 15
&& Math.Abs(y - node1.Ports[i].GetPosition().Y) < 15)
{
diagram1.Controller.ActivateTool("OrgLineConnectorTool");
}
}
diagram1.Refresh();
}
else
{
if (globalNode != null)
{
for (int i = 0; i < globalNode.Ports.Count; i++)
globalNode.Ports[i].Visible = false;
globalNode = null;
if (!MouseLeft)
diagram1.Controller.ActivateTool("SelectTool");
diagram1.Refresh();
}
}
}
Conclusion:
I hope you enjoyed learning about how to show and hide ports on mouse hover in WinForms Diagram?
You can refer to our WinForms Diagram feature tour page to learn about its other groundbreaking feature representations. You can also explore our WinForms Diagram documentation to understand how to create and manipulate data.
You can check out our components from the License and Downloads page for current customers. If you are new to Syncfusion®, try our 30-day free trial to check out our other controls.
Please let us know in the comments section below if you have any queries or require clarifications. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!