TX Text Control .NET for WPF

Built-In Dialog Boxes

Topic

Formatting a paragraph, setting up the page or editing a table doesn't require dialog boxes of your own: the WPF TextControl includes them. The ribbon tabs open them, and so can a button, a menu item or a command in your window with one method call. Below you find each built-in dialog box, the tab parameter of the tabbed ones, the WPF-specific return values and the separate dialog windows for bookmarks, hyperlinks, comments and tracked changes.

Dialog Methods and Their Results

A built-in dialog box is opened by a method of the TextControl ending in Dialog, for example ParagraphFormatDialog. The dialog box edits the current selection or input position. Most of these methods return a value of the WPF.DialogResult enumeration of TX Text Control, not the bool? that Window.ShowDialog returns:

ValueMeaning
OKThe user left the dialog box with OK
CancelThe user canceled the dialog box
NoneThe user left the dialog box with the Escape key

The ribbon groups use the same dialog boxes, and Built-in Ribbon Tabs shows where they are.

The Built-In Dialog Boxes

MethodDialog boxAvailable when
FontDialogFonts and character attributes
ParagraphFormatDialogAlignment, spacing, indents, frames and pagination
TabDialogTab stops
ListFormatDialogBulleted and numbered lists
FormattingStylesDialogCreate, delete and modify formatting styles
LanguageDialogThe language of the selected text
AddSymbolDialogInsert symbol characters
ForeColorDialog, TextBackColorDialogText color and text background color
FrameFillColorDialog, FrameLineColorDialogBackground color of a paragraph or table cell, frame color of a paragraph or table
PageColorDialogThe page color
SectionFormatDialogPage setup of a section: margins, size, headers and footers, columns, borders
TableFormatDialogLines, colors, distances, cell size, formulas and number formats of a tableThe input position is in a table
ImageAttributesDialogLayout, size and saving options of an imageAn image is selected
TextFrameAttributesDialogLayout, size and appearance of a text frameA text frame is selected
BarcodeLayoutDialogLayout, size, colors, type and text of a barcodeA barcode is selected
DrawingLayoutDialogLayout, size, colors, rotation and transparency of a drawingA drawing is selected
SignatureFieldLayoutDialogLayout, size and image saving options of a signature fieldA signature field is selected
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
SpellCheckDialogCorrect misspelled wordsA spell checker is connected through SpellChecker

Differences in the return values:

  • AddSymbolDialog and SpellCheckDialog return nothing.
  • LanguageDialog returns None when the selected language could not be set.
  • ConditionalInstructionDialog and ManageConditionalInstructionsDialog return bool?: true for OK, false for Cancel and null when the dialog box could not be opened.
  • ListFormatDialog returns Cancel also when nothing was changed.

Find, Replace, Open, Save and Print

CallOpens
Find()The built-in Find dialog box
Replace()The built-in Replace dialog box
Load(), Load(StreamType)A file Open dialog box with the given format filters
Save(), Save(StreamType)A file Save As dialog box
Print(string docName, bool useLastPrinterSettings)The standard print dialog box; true fills it with the settings of the previous call

The overload Save(StreamType, SaveSettings, SaveSettings.DialogSettings) adds options to the Save As dialog box with SaveSettings.DialogSettings: StylesheetOptions for HTML, EnterPassword for Adobe PDF and SaveSelection when text is selected. Search and Replace, Loading and Saving and Printing go into these tasks.

Open a Dialog Box From a Button

Two buttons below the editor open the Paragraph dialog box on its second tab and the table dialog box. Add them to the Grid of MainWindow.xaml, next to the TextControl:

<StackPanel Orientation="Horizontal" VerticalAlignment="Bottom">
    <Button Content="Paragraph..." Click="ParagraphButton_Click" />
    <Button Content="Table..." Click="TableButton_Click" />
</StackPanel>

The handlers go into MainWindow.xaml.cs:

using System.Windows;
using TXTextControl.WPF;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void ParagraphButton_Click(object sender, RoutedEventArgs e)
    {
        // Open the tab for frames and pagination
        TXTextControl.WPF.DialogResult result = textControl1.ParagraphFormatDialog(1);

        if (result == TXTextControl.WPF.DialogResult.OK)
        {
            // Return the keyboard focus to the editor
            textControl1.Focus();
        }
    }

    private void TableButton_Click(object sender, RoutedEventArgs e)
    {
        // The table dialog box needs a table at the input position
        if (textControl1.Tables.GetItem() != null)
        {
            textControl1.TableFormatDialog(0);
        }
    }
}

The code works as follows:

  • ParagraphFormatDialog(1) opens the second tab. Called without a parameter, a tabbed dialog box shows the tab that was open last.
  • The result type is written with its full name, TXTextControl.WPF.DialogResult. Inside a window class, the short name DialogResult refers to the DialogResult property of the Window, a bool?, and the code would not compile.
  • TableCollection.GetItem returns the table at the input position or null. ImageCollection.GetItem and TextFrameCollection.GetItem check for a selected image or text frame.

Tab Indexes

Method0123
FontDialogFont and attributesBaseline and text effects
ParagraphFormatDialogAlignment, line spacing, indents, distancesFrame and pagination
SectionFormatDialogMargins, page size, orientationHeaders and footersPage columnsPage borders
TableFormatDialogFrame lines, color, distancesVertical size and alignmentFormulas and number formats
ImageAttributesDialogWrapping and layoutSize and distancesSaving attributes
TextFrameAttributesDialogWrapping and layoutSize and distancesFrame line and background
BarcodeLayoutDialog, DrawingLayoutDialogWrapping and layoutSize and distancesColors and further attributes
SignatureFieldLayoutDialogWrapping and layoutSize and distancesSaving attributes of a set signature image

Units in Dialog Boxes

DialogUnit chooses the unit of sizes and distances in the dialog boxes: Auto, Centimeter, Inch or Millimeter. Auto takes the unit from the culture of the current thread. It can be set in XAML like any other property:

<WPF:TextControl x:Name="textControl1" DialogUnit="Millimeter" />

Dialog Windows

The dialog boxes for bookmarks, links, navigation, comments, tracked changes and synonyms are classes of their own. Each inherits from System.Windows.Window and is created for a TextControl:

ClassPurposeShown with
BookmarkDialogInsert a bookmark (a document target) or rename the one at the input positionShowDialog
DeleteBookmarksDialogGo to or delete targetsShowDialog
HyperlinkDialogInsert, edit or delete a hyperlink or a link to a targetShowDialog
GotoDialogGo to a page, section, line, table or bookmarkShow
CommentsDialogList, reply to, edit and delete commentsShow
TrackedChangesDialogList, accept and reject tracked changesShow
ThesaurusDialogSynonyms of the selected wordShowDialog

The TX Words sample opens the hyperlink dialog box from the drop-down menu of its Hyperlink button, here for an existing link:

The Edit Hyperlink window of TX Text Control for WPF with Text to display, Address, a Select file button, a Bookmarks list, Show Bookmarks in options and the Delete Hyperlink, OK and Cancel buttons.

BookmarkDialog and HyperlinkDialog throw an exception when nothing can be inserted or edited at the input position; their reference pages name the CanAdd and GetItem checks to make first.

Every item of these windows has a name, listed in the class's DialogItem enumeration, such as BookmarkDialog.DialogItem. FrameworkElement.FindName finds an item by that name. The following code hides the check box for deletable bookmarks and opens the window modally over the main window:

BookmarkDialog dialog = new BookmarkDialog(textControl1) { Owner = this };

// Hide the check box before the window is shown
UIElement deletable = dialog.FindName(BookmarkDialog.DialogItem.TXITEM_Deletable.ToString()) as UIElement;
if (deletable != null)
{
    deletable.Visibility = Visibility.Collapsed;
}

bool? confirmed = dialog.ShowDialog();

Adapt the Comments and Tracked Changes Dialog Boxes

The two modeless windows work the same way. Their items are named after the values of CommentsDialog.DialogItem and TrackedChangesDialog.DialogItem. The WPF classes have no FindItem method of their own: pass the name of the value to FindName, which returns 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 window for a reviewer who may accept single changes but not accept or reject all of them at once:

private void ReviewButton_Click(object sender, RoutedEventArgs e)
{
    TrackedChangesDialog dialog = new TrackedChangesDialog(textControl1) { Owner = this };

    // Hide the buttons that act on all tracked changes
    foreach (TrackedChangesDialog.DialogItem item in new[] {
        TrackedChangesDialog.DialogItem.TXITEM_AcceptAllTrackedChanges,
        TrackedChangesDialog.DialogItem.TXITEM_RejectAllTrackedChanges })
    {
        if (dialog.FindName(item.ToString()) is UIElement element)
        {
            element.Visibility = Visibility.Collapsed;
        }
    }

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

For comments, CommentsDialog.DialogItem.TXITEM_DeleteAllComments names the button that deletes every comment, which you hide the same way.

Dialog Boxes of Fields

FormField.FormFieldDialog edits the formatting of a form field, and PageNumberField.PageNumberDialog the format and numbering of a page number field. Both return a Selection.DialogResult.

Next Steps