Problem with GoGeneralNodePortLabel

Hello,

I tried to remove the label from a GoGeneralNodePort by using

GoGeneralNodePort.Label = null;

However, this creates an exception… To be sure everything was initialized correctly in my object, I modified the demo1 project :

in function AutoLinkingPort.AddSourceLink (AutolinkNode.cs, line 130) :

<div style="margin-left: 40px;">    public override void AddSourceLink(IGoLink link) {
      AutoLinkNode node = this.Parent as AutoLinkNode;
      if (node != null) {
        GoGeneralNodePort p = node.MakePort(true);
        node.AddLeftPort(p);
        link.ToPort = p;
        <span style="color: rgb(255, 0, 0);">p.Label = null; // Added line</span>
      }
    }

In that case, I still got an exception:
Message “Object reference not set to an instance of an object.” string
StackTrace " at Northwoods.Go.GoGeneralNodePort.set_Label(GoGeneralNodePortL abel value)\r\n at Demo1.AutoLinkingPort.AddSourceLink(IGoLink link) in C:\Program Files\Northwoods Software\GoDiagram Win 2.5.1 for .NET 2.0\Samples\Demo1\AutoLinkNode.cs:line 130\r\n at Northwoods.Go.GoLink.set_ToPort(IGoPort value)\r\n at Northwoods.Go.GoLabeledLink.set_ToPort(IGoPort value)\r\n at Northwoods.Go.GoView.CreateLink(IGoPort fromPort, IGoPort toPort)\r\n at Northwoods.Go.GoToolLinking.DoNewLink(IGoPort fromPort, IGoPort toPort)\r\n at Northwoods.Go.GoToolLinking.DoMouseUp()\r\n at Northwoods.Go.GoView.DoMouseUp()\r\n at Northwoods.Go.GoView.OnMouseUp(MouseEventArgs evt)\r\n at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)\r\n at System.Windows.Forms.Control.WndProc(Message& m)\r\n at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(M essage& m)\r\n at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Mes sage& m)\r\n at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)\r\n at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MS G& msg)\r\n at System.Windows.Forms.Application.ComponentManager.System.Win dows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMes sageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)\r\n at System.Windows.Forms.Application.ThreadContext.RunMessageLoo pInner(Int32 reason, ApplicationContext context)\r\n at System.Windows.Forms.Application.ThreadContext.RunMessageLoo p(Int32 reason, ApplicationContext context)\r\n at System.Windows.Forms.Application.Run(Form mainForm)\r\n at Demo1.MainForm.Main(String[] args) in C:\Program Files\Northwoods Software\GoDiagram Win 2.5.1 for .NET 2.0\Samples\Demo1\MainForm.cs:line 1580\r\n at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)\r\n at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)\r\n at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssem bly()\r\n at System.Threading.ThreadHelper.ThreadStart_Context(Object state)\r\n at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n at System.Threading.ThreadHelper.ThreadStart()" &nbs p; string

What should I do ?

Hmmm. That’s a bug in the GoGeneralNodePort.Label setter.
But otherwise the code handles a null Label fine, so if you don’t want the port to have a label, you could do something like:
[Serializable]
public class GoGeneralNode2 : GoGeneralNode {
protected override GoGeneralNodePortLabel CreatePortLabel(bool input) {
return null;
}
}
Or have the override decide which ports aren’t supposed to have labels for your application.

The problem I have is that I want the port to have a label at some time (for instance when trying to link something to the port), but not at other times.

I first tried to use the visible property of the label to that effect, but the label was still taken into account to compute the object bounding box when it was invisible (thus creating a huge margin when the node with hidden labels was into a subgraph).

Maybe I could override the good (which one is it ?) layout function, or maybe I could just override the Label property of GoGeneralNodePort to correct the bug ?

Best regards,

I haven’t tested this, but since the property is virtual you could try:
[Serializable]
public class GoGeneralNode2 : GoGeneralNode {
protected override GoGeneralNodePort CreatePort(bool input) {
GoGeneralNodePort p = new GoGeneralNodePort2();
p.LeftSide = input;
p.IsValidFrom = !input;
p.IsValidTo = input;
return p;
}
}
[Serializable]
public class GoGeneralNodePort2 : GoPort {
public override GoGeneralNodePortLabel Label {
get { return myPortLabel; }
set {
GoGeneralNodePortLabel old = myPortLabel;
if (old != value) {
if (old != null) {
old.Port = null;
if (old.Parent != null)
old.Parent.Remove(old);
}
myPortLabel = value;
if (value != null) {
value.Port = this;
if (this.Parent != null)
this.Parent.Add(value);
}
Changed(ChangedLabel, 0, old, NullRect, 0, value, NullRect);
if (!this.Initializing) {
LayoutLabel();
}
}
}
}
private GoGeneralNodePortLabel myPortLabel;
}

It solved the problem. Thank you.