The WPF Notepad Project: Select All, Time/Date, Go To, and Input Box (Premium)

In this installment, we’ll implement several more Edit menu items in the WPF version of .NETpad and create a custom Input Box.
Edit > Select All
Implementing Edit > Select All is as easy as was implementing Cut, Copy, and Paste. All we need to do is associate the SelectAllMenu menu item with the SelectAll command in MainWindow.xaml. So open up that file and add a Command property to it so that it looks like so:
<MenuItem Command="SelectAll" Name="SelectAllMenu" Header="Select _All" />
That’s it. The menu and associated keyboard shortcut (CTRL + A) now work and the menu will be disabled automatically unless there is something to select. As good, the keyboard shortcut hint appears in the menu, too.

Edit > Time/Date
As you may recall, implementing Time/Date was easy in Windows Forms. All we had to figure out was how to display the time and date in the right formats.

In WPF, this is a bit more complicated because we’re using a custom command. So we need those three increasingly familiar pieces, too: A custom routed command, an Executed command event handler, and a command binding to associate the command with its event handler.

We create the custom routed command in the Window.Resources section of the MainWindow.xaml file. Add the following line of XAML there:
<RoutedUICommand x:Key="TimeDateCommand" Text="TimeDate"><RoutedUICommand.InputGestures><KeyGesture>F5</KeyGesture></RoutedUICommand.InputGestures></RoutedUICommand>
Then, we need to add a Command property to the TimeDateMenu tag so that it resembles this:
<MenuItem Command="{StaticResource TimeDateCommand}" Name="TimeDateMenu" Header="Time/_Date" />
Now, we need to add the associated command binding in Window.CommandBindings. So find Window.CommandBindings and add the following tag:
<CommandBinding Command="{StaticResource TimeDateCommand}" Executed="TimeDateCommand_Executed"/>
Finally, we have to actually create the event handler in C#. So switch over to MainWindow.xaml.cs and add the following empty Executed command event handler at the bottom of the MainWindow class declaration.
private void TimeDateCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{

}
Add the following code to the event handler:
System.DateTime now = System.DateTime.Now;
TextBox1.SelectedText = now.ToShortTimeString() + " " + now.ToShortDateString();
Now, run the application and test Edit > Time/Date. You’ll see that the keyboard shortcut hint is added to the menu item automatically, which is good. But that when you insert the time and date, it’s selected because we’re using that SelectedText property in the second line.

What we want is for the insert text to not be selected, and for the text caret to be placed after the insertion. So add the following two lines of code to TimeDateCommand_Executed:
TextBox1.SelectionStart += (now.ToShortTimeString() + " " + now.ToShortDateString()).Length;
Text...

Gain unlimited access to Premium articles.

With technology shaping our everyday lives, how could we not dig deeper?

Thurrott Premium delivers an honest and thorough perspective about the technologies we use and rely on everyday. Discover deeper content as a Premium member.

Tagged with

Share post

Please check our Community Guidelines before commenting

Windows Intelligence In Your Inbox

Sign up for our new free newsletter to get three time-saving tips each Friday

"*" indicates required fields

This field is for validation purposes and should be left unchanged.

Thurrott © 2024 Thurrott LLC