Image

Let’s Code a Tax Calculator

Hello again! This time we will create a tax calculator, an application that will be useful for any business owner. Sounds cool? Let’s get started!


You already know the drill: start Visual Studio, create a new project, choose to create a Windows Forms App, and then click the “Next” button.


648c0ecad16ad.png


Pick a relevant name for the project, and then press “Next” once again. Go with the .NET 6.0 (Long Term Support) platform, and then click “Create”.


Wait until Visual Studio 2022 sets up the project, and then you will see an empty form once again.


648c0edc7706c.png


It’s time to design the user interface! Drag and drop on the form a ComboBox from the “Toolbox”, which is usually located on the left side of the screen. We will use this ComboBox to choose the desired tax percentage. Let’s name it “comboTaxPercentage”, shall we?


Then, add a TextBox, which will be used to enter the amount of money and name it “txtAmount”.


Add the Button that will trigger the calculation, naming it “buttonCalculate”.


We will also use a Label to display the result; let’s name it “labelResult”. Since the content of our label will be empty when we start the program, let’s set its BorderStyle to “FixedSingle” in the Solution Explorer. This way, we will add a border around the label, increasing its visibility.


To make the application look better, let’s add a few more labels. Unlike labelResult, they won’t be driven by code, so we can give them any name; their role is to show the end user how to input data, etc.


Here’s how my resized form looks like after adding these controls.


648c0eedb169b.png


Double-click the form – not one of the controls! – to generate the code for its loading event handler. Here’s how the code looks right now.


648c0efe8305b.png


Let’s add the tax percentages to our ComboBox; here’s the resulting code. 


648c0f0c49924.png


Return to the form, and then double-click the “Calculate” button (or whatever name you have given it). Paste the code associate to its event handler; the result will look like this.


648c0f198d249.png


It’s time to test our application! Build the project, and then run it. Input the desired amount, choose the tax percentage, and then click the “Calculate” button. If everything works as planned, the program will display the tax value using the “labelResult” control.


648c0f2935186.png


I hope you liked this tutorial. Feel free to improve the tax calculator even further, and don’t forget that I can build many more useful applications for your business.

Loader GIF