The WinForms Notepad Project: Icons and Open Files (Premium)

For the third installment of this starter project, we’ll add an app icon and program our Notepad clone to open text files.

First, the application icon, since the default Windows Forms icon is obviously pretty ugly. Here, we have a number of choices: We could make our own icon, which would be preferable. We could extract an icon from an existing application and use that. Or we could download an icon from the web.

For now, let’s just use Notepad’s icon, since that’s what we’re cloning. But in the future, we’ll want to replace it with a unique icon of our own.

There are lots of utilities that will help you extract an icon from an EXE, but I used Nirsoft IconsExtract. You just point it at the EXE file---in this case, C:\Windows\System32\Notepad.exe---and then dump it into a folder somewhere on your PC; I used the desktop. The file it creates is called notepad_2.ico.

In Visual Studio, you set the application icon in Project Properties: Navigate to Project > [Application Name] Properties… to see this interface.

In the Application page, select the drop-down under Icon, browse to the desktop, and choose the icon file you just extracted. Then, run the application by clicking Start in the toolbar and … what the heck. That doesn’t change the icon in the application’s title bar or in the taskbar.

After a bit of research, I discovered something fairly obvious: You need to change the form’s Icon property as well. So select the form in the visual designer (by clicking its title bar). Then, find the Icon property in the Properties pane, select the “…” next to it, and select the icon file on your desktop. When you do, the icon appears on the form in the designer. And when you run it, the icon appears in the application’s taskbar button too. Nice.

If you run the real notepad, you’ll see that it opens with a blank, unsaved document and that the title bar of the application reads “Untitled - Notepad.” For appearance’s sake, let’s change the default text in our application’s title bar to match that.

As before, select the form in the designer (after making sure the application isn’t running) and then scroll through the properties list in the Properties pane. Find the Text property and change its value to “Untitled - NotepadWF” (no quotes; you should use whatever application name you prefer). It’s a little thing, but now, when you do run the application, it more closely resembles Notepad.

OK, now for something more substantive. Let’s write the code needed for our application to actually open and display text files. This is going to introduce several new concepts.

First, we’ll need an Open menu item in the File menu. And unlike the previous menu items we’ve added, this one will need a shortcut key as well. That way, the user can just type CTRL + O to open a file too.

Next, we’ll need to display a system File Open dialog in response to the Open menu item being sel...

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