Integrating Existing Menus and Toolbars
Many Windows Forms applications already have a MenuStrip and a ToolStrip when a word processor is added to them. Their items can drive the TextControl directly: each item calls a method of the editor, and a few event handlers keep the enabled and checked states in step with the text. What follows builds an Edit menu, a Format menu and a formatting toolbar for an existing form, and shows how the keyboard shortcuts of the menus fit together with the keys the editor handles itself.
If you start a new form instead, the ready-made bars of TX Text Control may serve better: the ButtonBar, the rulers and the status bar are described in Add Rulers, Toolbars, and a Status Bar, and the ribbon tabs in Built-in Ribbon Tabs. Both can also be combined with your own menus.
Write the Code
The following Form1.cs connects the items, fills the font list and updates the states. Keep the namespace Visual Studio created for your project:
using System;
using System.Windows.Forms;
namespace MyWinFormsApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Load += Form1_Load;
// Edit menu
editToolStripMenuItem.DropDownOpening += EditToolStripMenuItem_DropDownOpening;
undoToolStripMenuItem.Click += (s, e) => textControl1.Undo();
redoToolStripMenuItem.Click += (s, e) => textControl1.Redo();
cutToolStripMenuItem.Click += (s, e) => textControl1.Cut();
copyToolStripMenuItem.Click += (s, e) => textControl1.Copy();
pasteToolStripMenuItem.Click += (s, e) => textControl1.Paste();
findToolStripMenuItem.Click += (s, e) => textControl1.Find();
// Format menu
fontToolStripMenuItem.Click += (s, e) => textControl1.FontDialog();
paragraphToolStripMenuItem.Click += (s, e) => textControl1.ParagraphFormatDialog();
// Toolbar
boldToolStripButton.Click += BoldToolStripButton_Click;
italicToolStripButton.Click += ItalicToolStripButton_Click;
fontToolStripComboBox.SelectedIndexChanged += FontToolStripComboBox_SelectedIndexChanged;
// Keep the toolbar in step with the text
textControl1.InputPositionChanged += (s, e) => UpdateToolbar();
textControl1.CharFormatChanged += (s, e) => UpdateToolbar();
}
private void Form1_Load(object sender, EventArgs e)
{
// The list of fonts is available once the control is initialized
fontToolStripComboBox.Items.AddRange(textControl1.GetSupportedFonts());
UpdateToolbar();
}
private void EditToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
{
// Enable only what the editor can do right now
undoToolStripMenuItem.Enabled = textControl1.CanUndo;
redoToolStripMenuItem.Enabled = textControl1.CanRedo;
cutToolStripMenuItem.Enabled = textControl1.CanCopy && textControl1.CanEdit;
copyToolStripMenuItem.Enabled = textControl1.CanCopy;
pasteToolStripMenuItem.Enabled = textControl1.CanPaste;
}
private void BoldToolStripButton_Click(object sender, EventArgs e)
{
textControl1.Selection.Bold = !textControl1.Selection.Bold;
textControl1.Focus();
}
private void ItalicToolStripButton_Click(object sender, EventArgs e)
{
textControl1.Selection.Italic = !textControl1.Selection.Italic;
textControl1.Focus();
}
private void FontToolStripComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (fontToolStripComboBox.SelectedItem is string fontName
&& fontName != textControl1.Selection.FontName)
{
textControl1.Selection.FontName = fontName;
textControl1.Focus();
}
}
private void UpdateToolbar()
{
TXTextControl.Selection selection = textControl1.Selection;
boldToolStripButton.Checked = selection.Bold;
italicToolStripButton.Checked = selection.Italic;
fontToolStripComboBox.Text = selection.FontName;
}
}
}
The code works as follows:
- The
Clickhandlers call one method each.FontDialogandParagraphFormatDialogreturn aDialogResult, which you can check when the application has to know whether the user clicked OK. DropDownOpeningoccurs just before the Edit menu opens. TheCan...properties say at that moment whether there is something to undo, redo, copy or paste, so the items never offer a command that does nothing. Cut also checksCanEdit, because cutting removes text, which a read-only or protected document doesn't allow. Clipboard Operations and Undo and Redo describe these properties in detail.- The toolbar buttons read and set Selection.Bold and Selection.Italic, and the combo box Selection.FontName.
Focusreturns the keyboard focus to the editor, so the user goes on typing. - InputPositionChanged occurs when the input position moves, and CharFormatChanged when the character formatting of the selection or the input position changes. Both call
UpdateToolbar, which checks the buttons and shows the font where the user is typing. - GetSupportedFonts returns
nulluntil the control is completely initialized, so the list is filled in theLoadevent of the form.
Run the application, select a word and click B: the word turns bold, and the button is checked whenever the caret is in bold text. The Edit menu offers Paste only when the clipboard holds a format the editor can paste and the document may be changed.
Bind Instead of Handling Events
The states of a toolbar can also come from data binding. A CheckBox with the Appearance Button or a ComboBox placed on the form binds to the InputFormat of the editor and follows the formatting without InputPositionChanged handlers, as shown in Data Binding and Database Storage.
Next Steps
- Load and Save Documents: the File menu with New, Open, Save and Save As.
- Built-in Dialog Boxes: every dialog box a menu item can open.
- Custom Tabs and Commands: your commands in the ribbon instead of a menu.
- Toolbars and Mini Toolbars: the
ButtonBarand the pop-up toolbars.