Export (0) Print
Expand All

TextField Class

Lightweight object that represents a part of a shape that can display text labels or areas.

Namespace:  Microsoft.VisualStudio.Modeling.Diagrams
Assembly:  Microsoft.VisualStudio.Modeling.Sdk.Diagrams.12.0 (in Microsoft.VisualStudio.Modeling.Sdk.Diagrams.12.0.dll)

public class TextField : ShapeField

The TextField type exposes the following members.

  NameDescription
Public methodTextFieldInitializes a new instance of the TextField class.
Top

  NameDescription
Public propertyAnchoringBehaviorDescribes how the content of the field should position and size itself in relation to the parent ShapeElement. (Inherited from ShapeField.)
Public propertyDefaultAccessibleDescriptionGets or sets the default accessible description. (Inherited from ShapeField.)
Public propertyDefaultAccessibleNameGets or sets the default name of the shape field for the accessible object. (Inherited from ShapeField.)
Public propertyDefaultAccessibleStateGets the default state of the shape field for the accessible object. (Inherited from ShapeField.)
Public propertyDefaultAutoSizeIf true, the text field's minimum reported size will grow to accomodate the length of the diplay text. Autosizable text fields will still respect the minimum size specified in the AnchoringBehavior.
Public propertyDefaultBackgroundBrushIdGets or sets the ID of the default brush to paint the background of the shape field. (Inherited from ShapeField.)
Public propertyDefaultCommitOnEscapeGets or sets a value indicating whether the Escape key commits the edit. If true, the Enter key will be used as a CRLF for in-place editing.
Public propertyDefaultFocusableGets the shape field and verifies whether it can receive focus, or gives the shape field the ability to receive focus. (Inherited from ShapeField.)
Public propertyDefaultFontIdGets or sets the default font id.
Public propertyDefaultInactiveSelectedBackgroundBrushIdGets or sets the ID of the default background brush for the shape field. (Inherited from ShapeField.)
Public propertyDefaultInactiveSelectedTextBrushIdGets or sets the default brush id for the inactive selected text.
Public propertyDefaultIsHorizontalGets or sets the default horizontal state for the text.
Public propertyDefaultMultipleLineIf true, the text automatically wraps over more than one line to fit into the width available.
Public propertyDefaultPenIdGets or sets the ID of the default pen to use for the shape field. (Inherited from ShapeField.)
Public propertyDefaultReflectParentFocusedStateGets the child shape field and verifies whether it should receive focus when its parent shape receives focus or sets the ability to reflect the focus state of its parent shape. (Inherited from ShapeField.)
Public propertyDefaultReflectParentSelectedStateGets the child shape field and verifies whether it should be selected when its parent shape is selected or sets the ability to reflect the selection state of its parent shape. (Inherited from ShapeField.)
Public propertyDefaultSelectableGets the shape field and verifies whether it can be selected by default or sets the default selection state of the shape field. (Inherited from ShapeField.)
Public propertyDefaultSelectedBackgroundBrushIdGets or sets the ID of the default background brush for the shape field. (Inherited from ShapeField.)
Public propertyDefaultSelectedTextBrushIdGets or sets the default brush id for the selected text.
Public propertyDefaultStringFormatGets or sets the default string format for text drawing.
Public propertyDefaultTextGets or sets the default text to draw.
Public propertyDefaultTextBrushIdGets or sets the default brush id for the text.
Public propertyDefaultVisibilityGets the shape field and verifies whether it appears by default or sets the default visibility of the shape field. (Inherited from ShapeField.)
Public propertyDisplayMemberGets or sets the property name that the display text is associated with.
Public propertyDrawBorder(Do not use - will be changed)
Public propertyFillBackground(Do not use - will be changed)
Public propertyNameGets the name of the shape field. (Inherited from ShapeField.)
Top

  NameDescription
Public methodAccessibleDoDefaultActionPerforms the default action for the accessible object. (Inherited from ShapeField.)
Public methodAllowInPlaceEditorAutoSizeBy default, allow in-place editor to size automatically if the field supports auto-size. (Overrides ShapeField.AllowInPlaceEditorAutoSize(ShapeElement).)
Public methodAssociateValueWith(Store, AssociatedPropertyInfo)Associates the shape field with a domain property that is assigned to a shape element. (Inherited from ShapeField.)
Public methodAssociateValueWith(Store, Guid)Associates the shape field with a domain property that is assigned to a shape element. (Inherited from ShapeField.)
Public methodAssociateValueWith(Store, Guid, AssociatedPropertyInfo)Associates the shape field with a domain property that is assigned to a shape element. (Inherited from ShapeField.)
Public methodAssociateVisibilityWith(Store, AssociatedPropertyInfo)Associates the visibility of the shape field with a domain property that is assigned to a shape element. (Inherited from ShapeField.)
Public methodAssociateVisibilityWith(Store, Guid)Associates the visibility of the shape field with a domain property that is assigned to a shape element. (Inherited from ShapeField.)
Public methodAssociateVisibilityWith(Store, Guid, AssociatedPropertyInfo)Associates the visibility of the shape field with a domain property that is assigned to a shape element. (Inherited from ShapeField.)
Public methodCanEditValueReturns a value indicating whether the value of this field can be edited. (Overrides ShapeField.CanEditValue(ShapeElement, DiagramClientView).)
Public methodCommitPendingEditCommits the pending edit. (Overrides ShapeField.CommitPendingEdit(ShapeElement, DiagramClientView).)
Public methodDoHitTestPerforms a hit test on a specified point in the diagram to determine whether the point is in the bounds of the shape field. (Inherited from ShapeField.)
Public methodDoKeyboardNavigationNavigates in the shape field by using the keyboard. (Inherited from ShapeField.)
Public methodDoPaintDraws the text field. (Overrides ShapeField.DoPaint(DiagramPaintEventArgs, ShapeElement).)
Public methodEditValue(ShapeElement, DiagramClientView)Invokes the in-place editor. (Overrides ShapeField.EditValue(ShapeElement, DiagramClientView).)
Public methodEditValue(ShapeElement, DiagramClientView, PointD)Invokes the in-place editor, setting the caret using the specified mouse position. (Overrides ShapeField.EditValue(ShapeElement, DiagramClientView, PointD).)
Public methodEditValue(ShapeElement, DiagramClientView, String)Invokes the in-place editor, replacing the text with the specified text.
Public methodEqualsDetermines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodFindFirstChildLocates the first child shape field in the parent shape field. (Inherited from ShapeField.)
Public methodFindLastChildLocates the last child shape field in the parent shape field. (Inherited from ShapeField.)
Public methodFindNextChildLocates the next child shape field in the parent shape field. (Inherited from ShapeField.)
Public methodFindNextInChildSubFieldsLocates the next child shape field in the navigation sequence. (Inherited from ShapeField.)
Public methodFindPreviousChildLocates the previous child shape field in the parent shape field. (Inherited from ShapeField.)
Public methodFindPreviousInChildSubFieldsLocates the previous child shape field in the navigation sequence. (Inherited from ShapeField.)
Public methodFocusedGets the shape field and verifies whether it has focus. (Inherited from ShapeField.)
Public methodGetAccessibilityObjectGets the accessibility object that is assigned to the shape field. (Inherited from ShapeField.)
Public methodGetAccessibleChildGets the Accessibility object for the child shape field. (Inherited from ShapeField.)
Public methodGetAccessibleChildCountGets the number of Accessibility objects that are assigned to the parent shape field and its child shape fields. (Inherited from ShapeField.)
Public methodGetAccessibleDefaultActionDescriptionGets the default purpose of the shape field for the accessible object. (Inherited from ShapeField.)
Public methodGetAccessibleDescriptionReturns the default accessible description for a text field. (Overrides ShapeField.GetAccessibleDescription(ShapeElement).)
Public methodGetAccessibleHelpGets the shape field Help for the accessible object. (Inherited from ShapeField.)
Public methodGetAccessibleHelpTopicFileNameGets a topic in the Help for the accessible object. (Inherited from ShapeField.)
Public methodGetAccessibleHelpTopicIdGets the ID that is assigned to the Help topic for the accessible object. (Inherited from ShapeField.)
Public methodGetAccessibleNameReturns the display text as the accessible name. (Overrides ShapeField.GetAccessibleName(ShapeElement).)
Public methodGetAccessibleRoleRetrieves the accessible role of this ShapeField. (Overrides ShapeField.GetAccessibleRole(ShapeElement).)
Public methodGetAccessibleStateRetrieves the accessible state of this TextField. By default, the TextField is invisible to accessibility clients if the field is not focusable and there is no text to display. (Overrides ShapeField.GetAccessibleState(ShapeElement, DiagramClientView).)
Public methodGetAccessibleValueReturns the accessible value. (Overrides ShapeField.GetAccessibleValue(ShapeElement).)
Public methodGetActiveInPlaceEditorReturns the control for the active in-place editor. This method may return null, in which case no in-place editor is active for this field. (Overrides ShapeField.GetActiveInPlaceEditor(ShapeElement, DiagramClientView).)
Public methodGetAutoSizeIf true, the text field's minimum required size will grow to accomodate the length of the diplay text. Autosizable text fields will still respect the minimum size specified in the AnchoringBehavior.
Public methodGetBackgroundBrushGets the brush that draws the background for the shape element to which this shape field is assigned. (Inherited from ShapeField.)
Public methodGetBackgroundBrushIdGets the ID of the brush that draws the background for the shape element to which this shape field is assigned. (Inherited from ShapeField.)
Public methodGetBoundsGets the bounds for the shape field. (Inherited from ShapeField.)
Public methodGetCursorGets the pointer that appears when the mouse is pointing to the shape field. (Inherited from ShapeField.)
Public methodGetDisplayTextGets the text to be displayed by this field.
Public methodGetFocusableGets the shape field and verifies whether it can receive focus. (Inherited from ShapeField.)
Public methodGetFontGets the font to draw with for the specified ShapeElement instance.
Public methodGetFontHeightGets the line spacing of this font (i.e., the height of a line of text) in world units. Line spacing is the ascent + the descent + some recommended extra. The ascent is the height of the character above the baseline, and the descent is the height of the character below the baseline. The extra is below the descent.
Public methodGetFontIdGets the id of the font to draw with for the specified ShapeElement instance.
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetInPlaceEditorBoundsGets the bounds for the in-place editor. (Inherited from ShapeField.)
Public methodGetMaximumInPlaceEditorSizeBy default, we allow the in-place edit control to resize up to 75 characters. (Overrides ShapeField.GetMaximumInPlaceEditorSize(ShapeElement).)
Public methodGetMinimumInPlaceEditorSizeMinimum in-place editor size is based on minimum width in characters and height in lines. (Overrides ShapeField.GetMinimumInPlaceEditorSize(ShapeElement).)
Public methodGetMinimumSizeGets the minimum width and heightfor this ShapeField in world units (Overrides ShapeField.GetMinimumSize(ShapeElement).)
Public methodGetMultipleLineIf true, the text can wrap onto more than one line to fit the width available.
Public methodGetPenGets the pen that draws the shape element to which the shape field is assigned. (Inherited from ShapeField.)
Public methodGetPenIdGets the ID of the pen that draws the shape element to which the shape field is assigned. (Inherited from ShapeField.)
Public methodGetPotentialMouseActionGets the mouse action to activate when the user next clicks a specific point on the diagram. (Inherited from ShapeField.)
Public methodGetSelectableGets the shape field and verifies whether it can be selected. (Inherited from ShapeField.)
Public methodGetStringFormatGets the string format to draw the text with for the specified ShapeElement instance.
Public methodGetTextBrushGets the brush to draw the text with for the specified ShapeElement instance.
Public methodGetTextBrushIdGets the id of the brush to draw the text with for the specified ShapeElement instance.
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodGetValueGets the value for this TextField. (Overrides ShapeField.GetValue(ShapeElement).)
Public methodGetValueDomainPropertyInfoGets the domain property that is assigned to the value in the shape field. (Inherited from ShapeField.)
Public methodGetVisibleGets the shape field and verifies whether it appears. (Inherited from ShapeField.)
Public methodHasFocusedAppearanceGets the shape field and verifies whether it should appear to have the focus. (Inherited from ShapeField.)
Public methodHasPendingEditReturns a value indicating whether in-place editing is active, with a commit pending. (Overrides ShapeField.HasPendingEdit(ShapeElement, DiagramClientView).)
Public methodHasSelectedAppearanceGets the shape field and verifies whether it appears to be selected. (Inherited from ShapeField.)
Public methodIsNavigationKey(Char)Gets the key that the user pressed and verifies whether it can be used to navigate between shape fields. (Inherited from ShapeField.)
Public methodIsNavigationKey(Keys)Gets the keys that the user pressed and verifies whether they can be used to navigate between shape fields. (Inherited from ShapeField.)
Protected methodMeasureDisplayTextHelper method to measure given text.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodNavigateAscendMoves the focus to the previous shape field in the navigation sequence for the selected shapes. (Inherited from ShapeField.)
Public methodNavigateDescendMoves the focus to the next shape field in the navigation sequence for the selected shapes. (Inherited from ShapeField.)
Public methodNavigateToFirstMoves the focus to the first shape field in the navigation sequence for the selected shapes. (Inherited from ShapeField.)
Public methodNavigateToLastMoves the focus to the last shape field in the navigation sequence for the selected shapes. (Inherited from ShapeField.)
Public methodNavigateToNextMoves the focus to the next shape field in the navigation sequence for the selected shapes. (Inherited from ShapeField.)
Public methodNavigateToPreviousMoves the focus to the previous shape field in the navigation sequence for the selected shapes. (Inherited from ShapeField.)
Protected methodOnBeginEditAlerts listeners when in-place editing has begun. (Inherited from ShapeField.)
Public methodOnClickAlerts listeners when the user has clicked a shape field. (Inherited from ShapeField.)
Public methodOnDoubleClickInvokes in-place editing by default. (Overrides ShapeField.OnDoubleClick(DiagramPointEventArgs).)
Protected methodOnEndEditAlerts listeners when in-place editing has completed. (Inherited from ShapeField.)
Public methodOnKeyDownAlerts listeners when the user has held down a key when pointing to a shape field. (Inherited from ShapeField.)
Public methodOnKeyPressInvokes the editor if the field is editable and any alpha-numeric character is typed. (Overrides ShapeField.OnKeyPress(DiagramKeyPressEventArgs).)
Public methodOnKeyUpAlerts listeners when the user has released a key when pointing to a shape field. (Inherited from ShapeField.)
Public methodOnMouseDownAlerts listeners when the user is holding down the mouse over a shape field. (Inherited from ShapeField.)
Public methodOnMouseMoveAlerts listeners when the mouse has been moved over a shape field. (Inherited from ShapeField.)
Public methodOnMouseUpAlerts listeners when the user has released the mouse button over a shape field. (Inherited from ShapeField.)
Public methodOnMouseWheelAlerts listeners when the mouse has been rotated over a shape field. (Inherited from ShapeField.)
Public methodSelectedGets the shape field and verifies whether it is selected. (Inherited from ShapeField.)
Public methodSetSelectionRangeSets the selection to a range of child shape fields. (Inherited from ShapeField.)
Public methodSetValueAssigns the specified value to a shape field. (Inherited from ShapeField.)
Public methodSetVisibleSets the visibility of the shape field. (Inherited from ShapeField.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

When you define a text decorator in a shape, it is represented by a TextField. For examples of the initialization of TextFields and other ShapeFields, inspect Dsl\GeneratedCode\Shapes.cs in your DSL solution.

A TextField is an object that manages an area within a shape, such as the space assigned to a label. One TextField instance is shared between many shapes of the same class. The TextField instance does not store the text of the label separately for each instance: instead, the GetDisplayText(ShapeElement) method takes the shape as a parameter, and can look up the text dependent on the current state of the shape and its model element.

How the appearance of a text field is determined

The default DoPaint() method performs these tasks. This is a simplified presentation of its code:

// Simplified version:
public override void DoPaint(DiagramPaintEventArgs e, ShapeElement parentShape)
{ 
  string text = GetDisplayText(shape); 
  StringFormat format = GetStringFormat(parentShape);
  Brush brush = GetTextBrush(e.View, shape);
  using (Font font = GetFont(shape))
  {
    e.Graphics.DrawString(text, font, brush, format);
  }
}
// StringFormat determines whether the string is centered etc.
// To customize statically for all instances of this shape field, 
// assign to DefaultStringFormat.
// To customize dynamically or per shape, override this:  
public virtual StringFormat GetStringFormat(ShapeElement shape)
{ return DefaultStringFormat; }

// Override to customize the displayed string:
public virtual string GetDisplayText(ShapeElement shape)
{ return this.GetValue(shape).ToString(); }

// Brush determines the text color.
// To change the brush for every field, change the shape’s styleset. 
// To customize to a brush in the style set, override GetTextBrushId.
// To change the brush to non-standard color, override this.
// Should take account of whether selected. 
public virtual Brush GetTextBrush(DiagramClientView view, ShapeElement shape)
{ return shape.StyleSet.GetBrush(this.GetTextBrushId(view, shape)); }

// Brush ID selects a brush from a StyleSet.
// Either return a member of DiagramBrushes 
// or add your own brush to the shape or application’s styleset.
// Override this to change dynamically or per instance.
// To change statically, just assign to default values. 
public virtual StyleSetResourceId GetTextBrushId(DiagramClientView view, ShapeElement shape)
{ return IsSelected(view, shape) ? (view.Focused ? DefaultSelectedTextBrushId
: DefaultInactiveSelectedTextBrushId ) : DefaultTextBrushId ;
}

// Font determines the shape and size of the text.
// To change the font for every field, change the shape’s styleset. 
// To customize to a font in the style set, override GetFontId.
// To change the font to a non-standard font, override this. 
public virtual Font GetFont(ShapeElement shape)
{ return shape.StyleSet.GetFont(GetFontId(shape)); }

// Selects a font from a styleset.
// Either return a member of DiagramFonts or 
// add your own font to the shape or application’s styleset.
// To change statically for all instances of this field, 
// assign to DefaultFontId.
// To change per shape or dynamically, override this. 
public virtual StyleSetResourceId GetFontId(ShapeElement parentShape)
{ return DefaultFontId; }

There are several other pairs of Get methods and Default properties, such as DefaultMultipleLine/GetMultipleLine(). You can assign a value to the Default property to change the value for all instances of the shape field. To make the value vary from one shape instance to another, or dependent on the state of the shape or its model element, override the Get method.

Static customizations

If you want to change every instance of this shape field, first find out whether you can set the property in the DSL Definition. For example, you can set font size and style in the Properties window.

If not, then override the InitializeShapeFields method of your shape class, and assign a value to the appropriate Default... property of the text field.

Caution noteCaution

To override InitializeShapeFields(), you must set the Generates Double Derived property of the shape class to true in the DSL Definition.

In this example, a shape has a text field that will be used for user comments. We want to use the standard comment font. Because it is a standard font from the style set, we can set the default font id:

 partial class ExampleShape
{   protected override void InitializeShapeFields(IList<ShapeField> shapeFields)
    {
      // Fields set up according to DSL Definition:
      base.InitializeShapeFields(shapeFields);
      // Find and update comment field:
      TextField commentField = ShapeElement.FindShapeField(shapeFields, "CommentDecorator") as TextField;
      // Use the standard font for comments:
      commentField.DefaultFontId = DiagramFonts.CommentText;

Dynamic customizations

To make the appearance vary dependent on the state of a shape or its model element, derive your own subclass of TextField and override one or more Get... methods. You must also override your shape’s InitializeShapeFields method, and replace the instance of the TextField with an instance of your own class.

The following example makes the font of a text field dependent on the state of a Boolean domain property of the shape’s model element.

To run this example code, create a new DSL solution using the Minimal Language template. Add a Boolean domain property AlternateState to the ExampleElement domain class. Add an icon decorator to the ExampleShape class, and set its image to a bitmap file. Click Transform All Templates. Add a new code file in the DSL project, and insert the following code.

To test the code, press F5 and, in the debugging solution, open a sample diagram. The default state of the icon should appear. Select the shape and in the Properties window, change the value of the AlternateState property. The font of the element name should change.

using Microsoft.VisualStudio.Modeling;
using Microsoft.VisualStudio.Modeling.Diagrams;
...

  partial class ExampleShape
  {
    /// <summary>
    /// Compose a list of the fields in this shape.
    /// Called once for each shape class.
    /// </summary>
    protected override void InitializeShapeFields(IList<ShapeField> shapeFields)
    {
      // Fields set up according to DSL Definition:
      base.InitializeShapeFields(shapeFields);
      // Replace the text field for NameDecorator:
      TextField oldField = ShapeElement.FindShapeField(shapeFields, "NameDecorator") as TextField;
      shapeFields.Remove(oldField);
      // Replace with my text field based on DSL Definition values:
      MyTextField newField = new MyTextField(oldField);
      shapeFields.Add(newField);
    }
  }
  /// <summary>
  /// Dynamic font depends on state of model element.
  /// </summary>
  public class MyTextField : TextField
  {
    public MyTextField(TextField prototype)
      : base(prototype.Name)
    {
      DefaultText = prototype.DefaultText;
      DefaultFocusable = prototype.DefaultFocusable;
      DefaultAutoSize = prototype.DefaultAutoSize;
      AnchoringBehavior.MinimumHeightInLines = prototype.AnchoringBehavior.MinimumHeightInLines;
      AnchoringBehavior.MinimumWidthInCharacters = prototype.AnchoringBehavior.MinimumWidthInCharacters;
      DefaultAccessibleState = prototype.DefaultAccessibleState;
    }

    public override System.Drawing.Font GetFont(ShapeElement parentShape)
    {
      // Access the Boolean domain property of the model element:
      if ((parentShape.ModelElement as ExampleElement).AlternateState)
        return new System.Drawing.Font("Callisto", 14.0f,
               System.Drawing.FontStyle.Italic | 
               System.Drawing.FontStyle.Bold);
      else
        return base.GetFont(parentShape);
    }

  }

Style sets

The preceding example shows how you can change the text field to any font that is available. However, a preferable method is to change it to one of a set of styles that is associated with the shape or with the application. To do this, you override GetFontId or GetTextBrushId().

Alternatively, consider changing the style set of your shape by overriding InitializeResources. This has the effect of changing the fonts and brushes for all of the shape fields.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft