Line with text?

Oct 4, 2012 at 6:52 AM

Hi,

Is it possible to draw text near the Line?

Imagine two Shapes connected with an arrowed Line. Let's say that the 1st Shape (where the arrow starts) represents a "Sender", the 2nd one - a "Receiver", and the Line represents some message flow from Sender to Receiver. It would be nice to place some hint text over the line indicating e.g. type of the message being sent; this is a rather common scenario, and, for example, MS Visio supports it. I wonder whether it is possible in Your framework as well.

Coordinator
Oct 4, 2012 at 9:04 AM

Attach a "Label" shape to the line.
"Label" shapes are these text shapes without border and a line with pin at the end. You can connect the pin with the line and place the label where you want the text to be. The label will remain its position relative to the partner shape.

Oct 5, 2012 at 9:04 AM

So if I want to always have text over a line, then I should create aggregation "MyLine = Line + Label" and use it everywhere instead of a standard Line, right?

Coordinator
Oct 5, 2012 at 9:55 AM

I think aggregating a label into a line will not work as fine as attaching the label to a line because the ShapeAggregation class (that handles all the parent/child stuff) is implemented rather generic and therefore cannot take advantage of the label shape's special capabilities.

I would recommend to write a method that creates a line with an attached label.

Mar 27, 2013 at 10:00 AM
How can i attach a lable to a line in code ?
Coordinator
Mar 27, 2013 at 10:47 AM
// Attach the label to the middle of a single-segment line:
//
// Calculate the middle of the line
PolylineBase myLine = ...
ControlPointId targetPtId = ControlPointId.Reference;
Point pos1 = myLine.GetControlPointPosition(ControlPointId.FirstVertex);
Point pos2 = myLine.GetControlPointPosition(ControlPointId.LastVertex);
Point targetPos = Point.Round(new PointF((pos1.X + pos2.X) / 2f, (pos1.Y + pos2.Y) / 2f));
//
// Get ControlPointId of the label's glue point
LabelBase myLabel = ...
ControlPointId gluePtId = ControlPointId.None;
foreach (ControlPointId id in myLabel.GetControlPointIds(ControlPointCapabilities.Glue))
    gluePtId = id;
Debug.Assert(gluePointId != ControlPointId.None);
// Move glue point to desired position and connect with the line (Point-To-Shape connection with reference point)
myLabel.MoveControlPointTo(gluePtId, targetPos.X, targetPos.Y, ResizeModifiers.None);
myLabel.Connect(gluePtId, myLine, targetPtId);
myLabel.MaintainOrientation = true; // Label will rotate when the line's angle changes
Jun 19, 2013 at 12:27 PM
KurtHolzinger wrote:
// Attach the label to the middle of a single-segment line:
//
// Calculate the middle of the line
PolylineBase myLine = ...
ControlPointId targetPtId = ControlPointId.Reference;
Point pos1 = myLine.GetControlPointPosition(ControlPointId.FirstVertex);
Point pos2 = myLine.GetControlPointPosition(ControlPointId.LastVertex);
Point targetPos = Point.Round(new PointF((pos1.X + pos2.X) / 2f, (pos1.Y + pos2.Y) / 2f));
//
// Get ControlPointId of the label's glue point
LabelBase myLabel = ...
ControlPointId gluePtId = ControlPointId.None;
foreach (ControlPointId id in myLabel.GetControlPointIds(ControlPointCapabilities.Glue))
    gluePtId = id;
Debug.Assert(gluePointId != ControlPointId.None);
// Move glue point to desired position and connect with the line (Point-To-Shape connection with reference point)
myLabel.MoveControlPointTo(gluePtId, targetPos.X, targetPos.Y, ResizeModifiers.None);
myLabel.Connect(gluePtId, myLine, targetPtId);
myLabel.MaintainOrientation = true; // Label will rotate when the line's angle changes
This Code i write in library or design project and create method or what kurt please help kurt.Please explain detail i still newbie



Best Regards
Coordinator
Jun 19, 2013 at 1:27 PM
The code snippet above is meant to be executed by some method of your application that creates a shape and needs to attach a label to it.

Imagine for example an application like the one in the Tutorial (Documents\Public Document\NShape\Demo Programs\Tutorials\Basic\4 Connecting Shapes), that creates diagrams dynamically. The tutorial program creates shapes and connects them with lines.
With the code above, you could add labels ("Label"s are the text shapes that can be pinned to other shapes and move along with them) to the lines between the shapes that are representing the web pages.
In this case you would have to paste the code into the method "fileLoadStatisticsToolStripMenuItem_Click" (right after connecting the shapes with a polyline).