TX Text Control .NET for Windows Forms

Built-In Dialog Boxes

Topic

TX Text Control .NET for Windows Forms brings the dialog boxes of a word processor with it: font, paragraph, tabs, lists, styles, page setup, tables, images, find and replace and more. The ribbon tabs open them for the user, and your code opens each one with a single method call. Here you find which dialog boxes exist, how to open them on a certain tab, how to tell whether the user clicked OK, and how to change the separate dialog classes for bookmarks, hyperlinks and comments.

How Built-In Dialog Boxes Work

Most dialog boxes are methods of the TextControl whose names end in Dialog, such as FontDialog. A dialog box works on the current selection or input position of the control and applies the user's settings when the user clicks OK. The method returns a System.Windows.Forms.DialogResult: DialogResult.Cancel when the user left with Cancel, otherwise DialogResult.OK.

The following screenshot shows the Tabs dialog box, which TabDialog opens, in the TX Words sample:

The Tabs dialog box in TX Words with a list of tab stop positions, the Leader list opened, the alignment options, a Right most tab check box and the buttons OK, Cancel, Set, Clear and Clear All.

In the ribbon, groups such as Font and Paragraph show a small button in their lower right corner, visible in the screenshot. It is the dialog box launcher of the group, a DialogBoxLauncher object with a Click event. Built-in Ribbon Tabs describes the tabs.

Dialog Boxes of the TextControl

MethodDialog boxCondition
FontDialogFonts and character attributes
ParagraphFormatDialogAlignment, spacing, indents, frames and pagination of paragraphs
TabDialogTab stops
ListFormatDialogBulleted and numbered lists
FormattingStylesDialogCreate, delete and modify formatting styles
LanguageDialogThe language of the selected text
AddSymbolDialogInsert symbol characters
ForeColorDialog, TextBackColorDialogThe text color and the background color of text
FrameFillColorDialog, FrameLineColorDialogThe background color of a paragraph or table cell, and the frame color of a paragraph or table
PageColorDialogThe page color
SectionFormatDialogPage setup of a section: margins, size, headers and footers, columns and borders
TableFormatDialogTable lines, colors, distances, cell size, formulas and number formatsA table at the input position
ImageAttributesDialogLayout, size and saving options of an imageA selected image
TextFrameAttributesDialogLayout, size and appearance of a text frameA selected text frame
ChartLayoutDialogLayout and size of a chartA selected chart
BarcodeLayoutDialogLayout, size, colors, type and text of a barcodeA selected barcode
DrawingLayoutDialogLayout, size, colors, rotation and transparency of a drawingA selected drawing
SignatureFieldLayoutDialogLayout, size and image saving options of a signature fieldA selected signature field
TableOfContentsDialogInsert a table of contents, or change the one at the input position
ConditionalInstructionDialog, ManageConditionalInstructionsDialogCreate, edit and manage conditional instructions of form fieldsIsFormFieldValidationEnabled is true; for ConditionalInstructionDialog, the document also has form fields and EditMode isn't ReadAndSelect
SpellCheckDialogCorrect misspelled wordsA spell checker connected through SpellChecker

A few methods don't follow this pattern or return something else:

  • AddSymbolDialog and SpellCheckDialog return nothing.
  • LanguageDialog returns DialogResult.None when the selected language could not be set.
  • The two conditional instruction methods return DialogResult.Abort when the dialog box could not be opened because a condition of the table above isn't met.
  • ListFormatDialog also returns DialogResult.Cancel when nothing was changed.

Find, Replace, Open, Save and Print

Other methods open dialog boxes when they are called without the parameters that would otherwise do the work:

MethodDialog box
Find()The built-in Find dialog box, searching the text part with the input focus
Replace()The built-in Replace dialog box
Load(), Load(StreamType)A file Open dialog box, offering the given file formats
Save(), Save(StreamType)A file Save As dialog box
Print(string docName)The standard print dialog box
PrintPreview(string docName)A print preview of the document

Search and Replace, Loading and Saving and Printing cover these tasks in full.

Open a Dialog Box From Code

The following handlers open the Font dialog box on its second tab and the table dialog box only when the input position is in a table. Connect them to the Click event of two buttons or menu items in Form1.cs:

using System;
using System.Windows.Forms;
using TXTextControl;

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void FontButton_Click(object sender, EventArgs e)
    {
        // Open the tab for baseline position and text effects
        if (textControl1.FontDialog(1) == DialogResult.OK)
        {
            // Return the keyboard focus to the editor
            textControl1.Focus();
        }
    }

    private void TableButton_Click(object sender, EventArgs e)
    {
        // The table dialog box needs a table at the input position
        if (textControl1.Tables.GetItem() != null)
        {
            textControl1.TableFormatDialog(2);
        }
        else
        {
            MessageBox.Show("Place the input position in a table first.");
        }
    }
}

The code works as follows:

  • FontDialog(1) opens the dialog box on the tab with index 1. Without a parameter, a tabbed dialog box opens on the tab that was open last.
  • The result is compared with DialogResult.OK, which means the user confirmed the changes.
  • TableCollection.GetItem without parameters returns the table at the input position, or null. ImageCollection.GetItem and TextFrameCollection.GetItem test the same way whether an image or a text frame is selected.
  • TableFormatDialog(2) opens the tab for formulas and number formats.

Tabs of the Tabbed Dialog Boxes

The activeTab parameter is the zero-based index of the tab:

Method0123
FontDialogFont and its attributesBaseline position and text effects
ParagraphFormatDialogAlignment, line spacing, indents and distancesFrame and pagination
SectionFormatDialogMargins, page size and orientationHeaders and footersPage columnsPage borders
TableFormatDialogFrame lines, color and distancesVertical size and alignmentFormulas and number formats
ImageAttributesDialogText wrapping and layoutSize and distancesSaving attributes
TextFrameAttributesDialogText wrapping and layoutSize and distancesFrame line and background color
ChartLayoutDialogText wrapping and layoutSize and distances
BarcodeLayoutDialog, DrawingLayoutDialogText wrapping and layoutSize and distancesFurther attributes such as colors
SignatureFieldLayoutDialogText wrapping and layoutSize and distancesSaving attributes of the signature image, if one is set

SectionFormatDialog(1) opens the page setup on the Headers and Footers tab:

The Page Setup dialog box on its Headers and Footers tab, with the Header list opened, Different header odd and even options, distances from the edge in inches and an Apply to list.

Measuring Units

The DialogUnit property sets the unit for sizes and distances in all dialog boxes: Auto, Centimeter, Inch or Millimeter. With Auto, the unit depends on the culture of the current thread.

// Show sizes and distances in millimeters
textControl1.DialogUnit = DialogUnit.Millimeter;

Options in the Save As Dialog Box

An overload of Save takes a SaveSettings.DialogSettings value that adds options to the Save As dialog box, such as a password button for Adobe PDF, stylesheet options for HTML or a check box that saves only the selected text. Loading and Saving Documents shows the options and the code.

Dialog Classes

Some dialog boxes are classes of their own that inherit from System.Windows.Forms.Form. You create them for a TextControl and show them modally with ShowDialog or modelessly with Show:

ClassDialog boxShown with
BookmarkDialogInsert a bookmark (a document target) at the input position, or rename the one thereShowDialog
DeleteBookmarksDialogList all targets, go to one or delete themShowDialog
HyperlinkDialogInsert, edit or delete a hyperlink or a link to a target in the documentShowDialog
GotoDialogMove the input position to a page, section, line, table or bookmarkShow
CommentsDialogList all comments, reply to, edit or delete themShow
TrackedChangesDialogList all tracked changes, accept or reject themShow
ThesaurusDialogSynonyms of the selected wordShowDialog

This is the hyperlink dialog box, opened for an existing link:

The Edit Hyperlink dialog box with Text to display, Address with a Select file button, a Bookmarks list, the Show Bookmarks in options and the buttons Delete Hyperlink, OK and Cancel.

BookmarkDialog and HyperlinkDialog throw an exception if nothing can be inserted or edited at the input position. The reference names the checks: DocumentTargets.CanAdd or a target at the input position for bookmarks, and HypertextLinks.CanAdd or a link at the input position for hyperlinks.

Each item of these dialog boxes has a name from the class's DialogItem enumeration, and FindItem returns the item as a System.Windows.Forms.Control so that you can move, change or remove it. The following code removes the check box that lets users mark a bookmark as deletable, then shows the dialog box:

BookmarkDialog dialog = new BookmarkDialog(textControl1);

// Remove the check box before the dialog box is shown
Control deletable = dialog.FindItem(BookmarkDialog.DialogItem.TXITEM_Deletable);
deletable.Parent.Controls.Remove(deletable);

dialog.ShowDialog(this);

BookmarkDialog.FindItem and BookmarkDialog.DialogItem list the items of this dialog box; the other classes have their own enumerations.

Adapt the Comments and Tracked Changes Dialog Boxes

The two modeless dialog boxes work the same way. CommentsDialog.FindItem takes a value of CommentsDialog.DialogItem, and TrackedChangesDialog.FindItem a value of TrackedChangesDialog.DialogItem. Both return null when the item isn't found:

Dialog boxIts items
CommentsDialogTXITEM_CommentsCount (the label with the number of comments), TXITEM_CommentsRefresh, TXITEM_CommentsViewer (the list of comments), TXITEM_DeleteComment, TXITEM_DeleteAllComments, TXITEM_EditComment, TXITEM_ReplyComment and TXITEM_Close
TrackedChangesDialogTXITEM_TrackedChangesCount (the label with the number of changes), TXITEM_TrackedChangesRefresh, TXITEM_TrackedChangesViewer (the list of changes), TXITEM_AcceptTrackedChange, TXITEM_AcceptAllTrackedChanges, TXITEM_RejectTrackedChange, TXITEM_RejectAllTrackedChanges and TXITEM_Close

The following handler opens the tracked changes dialog box for a reviewer who may accept single changes but not accept or reject all of them at once:

private void reviewButton_Click(object sender, EventArgs e)
{
    TrackedChangesDialog dialog = new TrackedChangesDialog(textControl1);

    // Hide the buttons that act on all tracked changes
    Control acceptAll = dialog.FindItem(TrackedChangesDialog.DialogItem.TXITEM_AcceptAllTrackedChanges);
    Control rejectAll = dialog.FindItem(TrackedChangesDialog.DialogItem.TXITEM_RejectAllTrackedChanges);

    if (acceptAll != null) acceptAll.Visible = false;
    if (rejectAll != null) rejectAll.Visible = false;

    // Modeless: the user goes on working in the document
    dialog.Show(this);
}

The name of each DialogItem value is the Name of the control it identifies. For comments, CommentsDialog.DialogItem.TXITEM_DeleteAllComments finds the button that deletes every comment, which you hide the same way.

Dialog Boxes of Fields

Two field classes open their own dialog boxes, which return a Selection.DialogResult:

Next Steps