Tooling up

Tooling up: Getting ready to automate tasks using C#

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Want to start your journey into coding your way out of boring jobs? You should start by making Windows apps.

  • Windows apps are very easy to make
  • They have the flexibility to perform any task
  • They also look pretty good with very little effort
  • Finally, the software used for making Windows apps is great and completely free

We won’t get bogged down in details at this point.

 

Visual Studio Community

""

Because nothing is open yet, Visual Studio will create a solution at the same time to put the project in.

 

You will then be asked what kind of project you’d like to create.

Create a new WPF project
Create a new WPF project

In the tree on the left hand side, select

Installed > Templates > Visual C#, then select WPF Application from the pane on the right.

Enter the name of your project (or leave it as it is if you don’t have a project in mind yet) in the box at the bottom and make note of the save location.

Finally, click Ok to let Visual Studio start creating the necessary files.

 

What just happened then?

This is what a blank WPF project looks like
This is what a blank WPF project looks like

Visual Studio will have just created a blank WPF solution and project with a blank window. Your screen will now actually have things going on.

The large area on the left is the where you will edit code and design the UI.

The Solution Explorer on the right is a sort of tree view showing you all the files in your solution. You can see your new solution. Inside this you can see your new project with the same name.

You will see a new window control called MainWindow.xaml. The project contains a few other files that will make sense later but we will ignore them for now.

 

Working with window controls

The MainWindow.xaml file is your first window (or screen). It is your first piece of UI.

 

The Xaml

MainWindow Xaml
MainWindow Xaml

The main file (MainWindow.xaml.cs) is known as the ‘Xaml’. The Xaml is used to design the layout of your UI. You can think of it as the designer for what actually appears on screen. Double click on it to have it open in the editor to the left. It is likely to be open in the editor already.

On the left (or top) you will see the designer view. This shows you what the window looks like. Right now it’s blank.

On the right (or bottom) you will see the Xaml view. This shows the markup code that is used to build and edit your UI. It works a lot like HTML if you are familiar with that.

We will cover this in more detail later.

 

The code behind file

The code behind file
The code behind file

If you look at MainWindow.xaml in the Solution Explorer you will see a little arrow to the left of it. Click it and you will see it open up like a folder to show another file inside called MainWindow.xaml.cs.

This file is known as the ‘code behind’ file. It is used to put logic into the window. For example you may add a button in the Xaml. You would use the code behind file to actually make that button do something when it is clicked.

Double click on MainWindow.xaml.cs in the Solution Explorer to open it in the main editor window. The code behind file is written in the C# programming language.

Again, we will cover this later so don’t worry too much right now. Once you get going you will find it much easier than it probably looks right now.

 

Taking it for a test drive

It runs!
It runs!

Visual Studio has something called a debugger. This allows you to run the program from inside Visual Studio without installing it. If anything goes wrong, rather than just crash it will stop on the line of code that caused the error and give you a message that is hopefully useful.

To debug you can either press F5 or click the green play button at the toolbar at the top. Visual Studio will then build the solution (a concept we will cover later) and start running the code. You will see the blank MainWindow control appear.

There’s nothing on it and it can’t do anything right now. But that’s just because you haven’t put any work into it yet. Just be glad it runs for now.

That wasn’t so bad eh?

So hopefully by now you’ve seen your first program running. Admittedly, Visual Studio did most of the heavy lifting. And your program doesn’t actually do anything.

But you’ve just made your first Windows app and that is something to be proud of. What do you reckon? Easier than you thought?

Ask for help if you need it

Leave a comment below if you get stuck. There are no stupid questions. Chances are if you have a question there will be someone else with the same question. So do the righty and ask for everyone else’s benefit. Or you can contact me directly here.

 Next step

If you already have a task in mind that you’d like to automate, learn how to prepare for automating it here.

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *