Custom TextBox in WinForms

Just recently I had to deal with WinForms for the first time in a long while and I stumbled upon an interesting thought. How would I go about making a custom WinForms TextBox with some kind of ‘uncommon behaviour’? I dug deeper into this subject, did some research on it and I think I have come up with something that is worth sharing.

Little intro

As far as simple WinForms projects are concerned, customising behaviour of their controls can be done easily. However, if your project requires some TextBoxes to work traditionally and others to have been inbuilt with some validation logic, I suggest you follow the steps described in this piece.

I am going to demonstrate this on a project, where every a’ entered into a text box will become a ‘b’.

Let’s roll

To kick things off, create a WinForms project and name it whatever you want (I named mine CustomWinFormsTextBox).

Create new project
Create new project

Then right-click your project in the Solution Explorer and add a new class. I have chosen CustomTextBox as it’s name.

Set the class name
Set the class name

After the class is successfully created, make it a child of TextBox. Then in order to add the described behaviour, override the OnKeyPress method of the TextBox class. This method is called everytime we press a key on the keyboard while the TextBox is focused.

Edit OnKeyPress method
Edit OnKeyPress method

We are essentially checking if an ‘a’ has been pressed, in which case we would replace it with a ‘b’, that would then get displayed in the text box.

Now all that’s left to do is to add our CustomTextBox to the Toolbox window so that we can drag-and-drop it onto our form. The Toolbox won’t offer you custom control just yet. First you have to build your solution. Then if you search for CustomTextBox you should see it appear in the search results.

Search Results
Search Results

Now just simply drag and drop it onto the form, save your project and hit Start. If you try to enter ‘maniac’, the text box will show ‘mbnibc’.

Place CustomTextBox and Start the app
Place CustomTextBox and Start the app

Although I know this won’t come to good use to everyone, I still think it is worth knowing about it.

Dialogs in C# 101 – Opening and saving files

This tutorial will introduce you with the basics of using dialogs in C#.

These file operations are done using two classes:

  • OpenFileDialog – good for opening files using File Explorer dialog within the program
  • SaveFileDialog – good for saving files using File Explorer dialog within the program
Opening a file

To open a file, we will be using the OpenFileDialog class. This class allows you, as the programmer, to display a File Explorer dialog window. The user will be able to pick a file or multiple files and open them.

To open a file dialog in your program, you can use this snippet.

Let’s just briefly look at the code. The dialog variable represents an object of the OpenFileDialog class. The ShowDialog is a method that returns the dialog state after being closed. DialogResult is just an enumeration that represents each of the dialog states returned by the ShowDialog method. Then if the condition on the second line is true, our little program will display a MessageBox with the selected file’s path.

Saving file

To save a file, use this piece of code.

For the sake of demonstration, we created a variable dateAndTime that holds today’s date and current time. On the second line, we created an object of the SaveFileDialog class. In the if statement, we are checking if a directory and a file name is set. If that is true, then the current date and time is saved to a file.

Conslusion

The OpenFileDialog and SaveFileDialog classes are extremely powerful. In this part, we’ve shown how to open and close files using the File Explorer dialog window. In the next part, we will learn how to add file filters to the dialog.