Select port from a port list

Hi Walter,
I am trying to implement a function that user will pick a port name to connect from a list of port names (multiple ports on the node) when connecting a link to the node.
I created RelinkingNew class and override PickNearestPort method, in which it will populate and show the port list. Depending on the user selection, the method return the corresponding port.
The problem I have is that PickNearestPort method seems to be called multiple times and thus the dialog are prompted up multiple times. In addition, the link does not connect to the port after the close of the dialog.
Could you please help?

public override IGoPort PickNearestPort(PointF dc)
{
GMFConnectionLink link = this.Link.GoObject as GMFConnectionLink;
GMFCxnData cxnData = link.UserObject as GMFCxnData;
IGoPort iport = base.PickNearestPort(dc);
if (this.EndPort != null && this.EndPort.GoObject is GoPort)
{
if (iport != null)
{
// link has been snapped into a port
// list all the qualified ports in the block where port is located
GMFDataModelNodePort port = iport as GMFDataModelNodePort;
GMFDataModelNode node = port.DataModelNode;
IProcessData procdat = node.UserObject as IProcessData;
if (procdat.NumberOfPorts(port.Type) > 1)
{
frmPorts.Instance.Ports.Items.Clear();
foreach (GoPort p in node.Ports)
{
GMFDataModelNodePort nodeport = p as GMFDataModelNodePort;
if (nodeport.Type == cxnData.PortType && nodeport.PortDirection == port.PortDirection)
{
frmPorts.Instance.Ports.Items.Add(nodeport.Name);
}
}
if (frmPorts.Instance.Ports.Items.Count > 1)
{
frmPorts.Instance.Location = new Point((int)dc.X, (int)dc.Y);
System.Windows.Forms.DialogResult result = frmPorts.Instance.ShowDialog();
foreach (GoPort p in node.Ports)
{
GMFDataModelNodePort selport = p as GMFDataModelNodePort;
if (selport.Name == frmPorts.Instance.Selection)
{
iport = selport;
}
}
}
}
myLastNearestPort = (GoPort)this.EndPort.GoObject;
myLastNearestPort.Style = GoPortStyle.Rectangle;
myLastNearestPort.Pen = LinkingNewTool.HighlightPen;
myLastNearestPort.Brush = LinkingNewTool.HighlightBrush;
}
else if (myLastNearestPort != null)
{
myLastNearestPort.Style = GoPortStyle.None;
myLastNearestPort = null;
}
}
return iport;
}

PickNearestPort is called from DoLinking in DoMouseMove and directly in DoMouseUp.
So, PickNearestPort probably isn't the way to get to what you want.
I would override DoNewLink to show the dialog (DoNewLink is called on the DoMouseUp)

If GoToolLinking.Forwards is true, the second argument is the proposed port to be linked to; if .Forwards is false, it's the first argument (i.e. the "fromPort").

From this you can get the node, and thus the list of ports to connect to.

When the dialog is OK'ed, call the base.DoNewLink method with the desired port as the argument.
If the dialog is cancelled, just set GoTool.TransactionResult to null.
Jake

Thanks a lot Jake, It did the trick!