Right-to-Left UI
Applications for Arabic or Hebrew need two things: text that runs from right to left, and an application window that is mirrored, with the ribbon, the scroll bar and the dialog boxes laid out from right to left. TX Text Control .NET for Windows Forms supports both since version 20.0. The first is a formatting attribute of paragraphs, the second a setting of the form and its controls.
Right-to-Left Text in the Document
Each line of a document can be bidirectional: it can contain parts written from left to right and parts written from right to left. The writing direction of a paragraph decides in which order these parts are arranged. It is the Direction property of the paragraph format, with the values Direction.LeftToRight, the default, and Direction.RightToLeft of the Direction enumeration.
In a right-to-left paragraph, the natural alignment and the default kind of tab stop are right-aligned, tab positions are measured from the right page margin, and the horizontal ruler shows its origin at the right margin.
Users change the direction with two buttons in the Paragraph group of the formatting tab, which the ribbon shows as Home: the ribbon items TXITEM_RightToLeft and TXITEM_LeftToRight. The paragraph formatting dialog box offers the setting as well. In code, Selection.ChangeDirection changes the direction of all selected paragraphs:
private void RightToLeftButton_Click(object sender, EventArgs e)
{
// Mirror the alignment and the tabs of the selected paragraphs, too
bool changed = textControl1.Selection.ChangeDirection(Direction.RightToLeft);
}
The code works as follows:
ChangeDirectionreturnstrueif at least one paragraph was changed.- Besides the direction, it toggles the alignment of the paragraphs from left to right or the other way round, and it toggles left and right tab stops. Centered and justified paragraphs keep their alignment.
InputFormat.RightToLeft reads or sets the direction at the input position, or of the whole selection, as a bool?: null means the selection contains paragraphs of both directions. It does not mirror the alignment.
Justified Arabic text can be stretched with Kashida lines instead of wider spaces. ParagraphFormat.Justification chooses Spaces, the default, Kashida or SpacesAndKashida. Character and Paragraph Formatting describes the other paragraph attributes.
Mirror the Application
Users of right-to-left languages mostly prefer an application whose window is laid out from right to left as well. The controls of TX Text Control support the RightToLeft property of Windows Forms. With RightToLeft.Yes:
- the
TextControlshows its vertical scroll bar at the left border and zooms from the top right corner; - the built-in dialog boxes and context menus have a right-to-left design;
- the
RulerBarshows a right-to-left ruler; - the
ButtonBarand theStatusBararrange their buttons and text parts from right to left.
The following constructor in Form1.cs lays out the whole form from right to left, including the ribbon:
using System.Windows.Forms;
using TXTextControl;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// Mirror the form and every control on it
RightToLeft = RightToLeft.Yes;
RightToLeftLayout = true;
// The ribbon has a layout setting of its own
if (textControl1.Ribbon != null)
{
textControl1.Ribbon.RightToLeftLayout = true;
}
}
}
The code works as follows:
RightToLeftis a property of every Windows Forms control. Like the TX Words sample, the code sets it on the form, and the controls on the form, including theTextControl, follow it.RightToLeftLayoutof the form turns on the mirrored layout of the form itself.- The ribbon, available through the Ribbon property, is derived from
System.Windows.Forms.TabControland has its ownRightToLeftLayoutproperty, which the TX Words sample sets together with that of the form.
The TX Words Sample
The TX Words sample Right To Left switches between both layouts. Its View tab has a Right to Left button in the Application View group. Clicking it opens a message box, and when the user confirms, the sample saves the new layout in the application settings and restarts:

After the restart, the constructor of the main form reads the saved setting and assigns it to RightToLeft, RightToLeftLayout and the RightToLeftLayout of the ribbon, as in the code above. The sample is installed in %USERPROFILE%\Documents\TX Text Control 34.0.NET for Windows Forms\CSharp\TX Words\Right To Left.
Next Steps
- Localization and Resource Kit: texts of the user interface in other languages.
- Built-in Dialog Boxes: the dialog boxes that are mirrored.
- Rulers and Status Bar: the bars around the editor.
- Built-in Ribbon Tabs: the tabs with the direction buttons.