TabStop
A tab stop is a term used to describe the location the caret stops after tab key is pressed. Tab stops are used in words processing to enable users to align text by inserting the Tab symbol. Each paragraph contains a number of tabs, which could be placed wherever you want.
TabStop Overview
The TabStop class is immutable, meaning you should set its properties when initializing an instance.
Position: The position of the tab stop. The value is in device independent pixels (1/96 inch).
-
Type: The type of the tab stop, defines the behavior of the tab stop. All possibilities are described with the TabStopType enumeration:
- Left: The text following the tab stop will be left aligned with respect to the tab stop position. This is the default value.
- Center: The text following the tab stop will be centered around the tab stop position.
- Right: The text following the tab stop will be right aligned with respect to the tab stop position.
- Decimal: Text before the decimal point will be positioned to the left and text after the decimal point will be positioned to the right side of the tab stop.
- Bar: A vertical bar is shown at the tab position.
- Clear: Clears an inherited tab stop.
-
Leader: Specifies the character which shall be used to fill the space in front of a tab. All possibilities are described with the TabStopLeader enumeration:
- None: The space before the tab will be left empty. This is the default value.
- Dot: The space before the tab will be filled with dots.
- Hyphen: The space before the tab will be filled with hyphens.
- Underscore: The space before the tab will be filled with underscores.
- MiddleDot: The space before the tab will be filled with middle dots.
The distance between automatic tab stops is determined by the RadFlowDocument.DefaultTabStopWidth property. Automatic tab stops refer to the tab stop location which occurs after all custom tab stops in the current paragraph have been surpassed.
TabStopCollection Overview
This class derives from System.Collections.Generic.IEnumerable
The TabStopCollection class exposes the following members:
- Count: The count of TabStop elements in the collection.
- Insert(): This method will return a new instance of TabStopCollection with the specified tab stop inserted in it.
- Remove(): This method will return a new instance of TabStopCollection with the specified tab stop removed.
Working with TabStopCollection
Create a TabStopCollection
Excluding the default constructor, the TabStopCollection class exposes an overload allowing you to directly pass a collection of TabStop objects:
Example 1: Create a TabStopCollection
List<TabStop> tabStops = new List<TabStop>();
tabStops.Add(new TabStop(Unit.InchToDip(1), TabStopType.Left));
tabStops.Add(new TabStop(Unit.InchToDip(2), TabStopType.Center, TabStopLeader.Dot));
tabStops.Add(new TabStop(Unit.InchToDip(3), TabStopType.Right, TabStopLeader.Hyphen));
tabStops.Add(new TabStop(Unit.InchToDip(5.5), TabStopType.Bar));
TabStopCollection collection = new TabStopCollection(tabStops);
Insert Item in a TabStopCollection
In Example 2 is illustrated how to insert items in the TabStopCollection created in Example 1. Keep in mind that due to the fact that this collection is immutable, the Insert() method will return a new instance of the class.
Example 2: Insert item in a TabStopCollection
collection = collection.Insert(new TabStop(Unit.InchToDip(4)))
.Insert(new TabStop(Unit.InchToDip(5.5), TabStopType.Right));
Remove Item from a TabStopCollection
The snippet below shows how to remove an item from the TabStopCollection created in Example 1. Keep in mind that due to the fact that this collection is immutable, the Remove() method will return new instance of the class.
Example 3: Remove item from a TabStopCollection
TabStop tabStopToRemove = collection.First();
collection = collection.Remove(tabStopToRemove);
Working with TabStop
In RadWordsProcessing the tab stops are stored as a collection in the Paragraph. This section will show you how to work with the TabStop element.
Create a TabStop
The code from Example 4 demonstrates how to create a tab stop.
Example 4: Create a TabStop
TabStop tabStop = new TabStop(Unit.InchToDip(2), TabStopType.Center);
Adding a TabStop
Example 5 shows how to add the tab stop created in Example 1 to an existing Paragraph through the TabStops property of type TabStopCollection.
Example 5: Insert a TabStop
paragraph.TabStops = paragraph.TabStops.Insert(tabStop);
Remove a TabStop
The following code-snippet illustrates how to remove the created in Example 4 TabStop:
Example 5: Remove a TabStop
paragraph.TabStops = paragraph.TabStops.Remove(tabStop);
Using TabStop In the Content
Once you have applied the desired tab stops to a paragraph, you need to insert tabs (\t) so that the content can be aligned to the specified tab stops. The code in Example 6 inserts tab stops at three positions with different properties and aligns three words on the tab stop positions using tabs.
Example 6: Add tabs to align to the tab stops
private RadFlowDocument CreateDocumentWithTabStops()
{
RadFlowDocument document = new RadFlowDocument();
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
List<TabStop> tabStops = new List<TabStop>();
tabStops.Add(new TabStop(Unit.InchToDip(1), TabStopType.Left));
tabStops.Add(new TabStop(Unit.InchToDip(3), TabStopType.Center, TabStopLeader.Dot));
tabStops.Add(new TabStop(Unit.InchToDip(5), TabStopType.Right, TabStopLeader.Hyphen));
TabStopCollection tabStopCollection = new TabStopCollection(tabStops);
Paragraph paragraph = editor.InsertParagraph();
paragraph.TabStops = tabStopCollection;
paragraph.Inlines.AddRun("\tleft\tcenter\tright");
return document;
}