Getting rid of weird HTML indentation in Visual Studio Code

When editing HTML files in Visual Studio Code I have always been driven away by the weird auto-indentation when typing out closing tags. Since I am used to writing opening and closing tags before adding any further child elements, I have refrained from using VS Code for this purpose.

In the latest versions there seems to be certain improvement, though it still isn’t perfect. Therefore I decided to dig into the topic a little more and believe I found a solution to satisfy one’s needs. We shall achieve this by adding a simple line of JSON into user settings within Visual Studio.

How…?

We’ll start off by launching a new instance of Visual Studio and opening an HTML file. When adding some content as described above, we end up with something like this.

How I don't like it
How I don’t like it!

You can clearly see that the body tag is indented to the right when it shouldn’t be.   Though this might be a thing of personal preference I wanted to help those, who are feeling the same as me.

The workaround here is rather simple. Just bring up Command Palette using the Control + P (on Windows) or Command + P (on macOS) key combo and search for “user settings”. Another approach is to hit Control + , (on Windows) or Command + , (on macOS). This will open two JSON files. The one on the right will be empty. Here we will set the editor.formatOnSave setting to true. So make your file look like this.

Format on save
Format on save

Now you can go back to your HTML file and hit save. VS Code will automatically format the file as to your desire.

Edited file
Edited file

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.

Adding Visual Studio Code to PATH

In connection with the last article regarding the basic concepts of working with ASP.NET Core MVC, I came up with a little tweak to speed up your development by at least a small margin. To make things clear enough I won’t go into much technical detail.

Consider the situation in which we were last time. We successfully created a new project and went ahead to open it in Visual Studio Code. What we did and always can do is to launch a new instance of Code and open the project folder by navigating to it in the Finder dialog window. While this approach works, I will show you a quicker way to do it.

Setup

Essentially, all that is needed to be done is to invoke a command that will do all the work for us.

  1. Launch Visual Studio
  2. Press Command + ShiftP, which will bring up the Command Palette.
  3. Enter “shell”.

    Command Palette within Visual Studio Code
    Command Palette within Visual Studio Code
  4. You should be presented with two results, one of them being “Shell Command: Install “code” command in PATH”. Click on this option and wait until the operation has completed. This usually takes no more that a second.

All is now setup. Let’s head to the usage!

Usage

So far, all seems a little easy, doesn’t it? Well…this part will be of no exception. Simply navigate to your project folder in a Terminal window and enter code . . This will instantly open a new Visual Studio Code instance with the project loaded. No further hassle.

This tweak helped me and I hope it will make your workflow a bit snappier, as well.