C# Tutorial Series



C# Tutorial Series

 

/!\ WARNING /!\  Source codes to the tutorials are written using Visual Studio 2012. Depending on the version you are using, you might run into compatibility issues.

C# Tutorial Series is a collection of tutorials on designing and coding desktop applications in Microsoft C#. However, these tutorials are for intermediate and perhaps advanced users, so in order to follow along it is recommended that you have some knowledge of the following topics:
  • Programming Fundamentals
  • Object Oriented Programming (OOP)
  • Algorithms
  • C# Programming Language
Before you begin working on the tutorials, you should prepare some software tools to ensure a smooth ride along. Since we are programming in C#, it is recommended that you use Windows Vista/7/8/10 along with at least (it is 2015!) Visual Studio 2012.



App #1{Coin_Flip}: 


In this tutorial we will develop an application that simulates a coin being flipped. In the initial run of the application, a form will be displayed and in it is an image of a coin’s head side. When the user clicks the “Flip” button, the tail of the coin will be displayed. So let’s begin!
  • Step 1: Open Visual Studio, in our case VS 2012 and start a new project (File->New->Project…->Visual C#->Windows Form) and name it “Coin_Flip”. 
  • Step 2: Add two “PictureBox” controls and name the firm one “Headpic” and the second “Tailpic”. Now resize both picture boxes to the exact width and height and position them one on top of the other (Hint: position the first one and copy its location values to the second picture box). Next, add head.png image to "Headpic" and tail.png image to "Tailpic".
  • Step 3: Add a button and change its name to “FlipBtn” and set its text property to “Flip”. The final form should look like Figure 1 below.
  • Step 4: Let’s add some code to the “FlipBtn” button in order to make the coin flip. Double-click the “Flip” button and you should be in the event handler, now enter the flowing code: 
  • Step 5: Run the application and click the “Flip” button, you should see head and tail after each click.
Source File: Coin_Flip.zip
 

    

      App #2{Coin_Flip} continue:


In this tutorial, we will be working with our first application "Coin_Flip". This time, we will make the coin flip fast as if you throw it in the air. In order to achieve this feature, we need to add some more code. So let's begin!
  • Step 1: Open VS2012 and open the Coin_Flip project you've created in the previous tutorial or the one downloaded from the .zip file.
  • Step 2: Double-click on the "Flip" button. Next, create a global variable "private Timer SpinTime = new Timer();".
  • Step 3: Create a method called "SpinCoin" and add the code below:
  • Step 4: Create the even handler we declared before, in which we will cut and paste the code from "FlipBtn_Click" into the event handler.
  • Step 5: Now we need to call "SpinCoin();" method from within the button click. So, add this line "SpinCoin();" to "FlipBtn_Click" event handler.
  • Step 6: Run the application and click the "Flip" button, does the coin flip? Good! Now we need to make it stop.
  • Step 7: Go back to the form design view, add a button, change its name property to "StopBtn", and text property to "Stop" (you may resize them if you want).  
  • Step 8: Double-click the "Stop" button and add this line of code "SpinTime.Stop();".
  • Step 9: Now try flipping and stopping the coin, cool huh!


     Source File: Coin_Flip_2.zip 


 

     App #3{Birth Date}: 

 

In this tutorial we will be working with strings. The application we are about to create will collect input from the user and display it in one line (string concatenation). For the design of this form we will need labels, text boxes, and buttons. Let's begin!
  • Step 1: Same as the previous tutorials.
  • Step 2: Add five labels, four text boxes, and three buttons (see screenshot below).
  • Step 3: Now we need to give our controls meaningful names and texts then align them properly within the form (see screenshot below).
  • Step 4: Let's add some code to make our application do something useful. Double-click "Clear" button and add the following lines (see screenshot below). Now double-click "Exit" button and do the same (see screenshot below).
  • Step 5: Next, we will add the code that collects user's input and display it in the "DateOutputlabel". Double-click "Show Date" button and add the following code (see screenshot below). 
  • Step 6: Final result!
    Source File: Birth_Date.zip


 

App #4{Miles per Gallon}: 

 

In this tutorial, we will be working with numbers and performing calculations. Our Miles-per-Gallon application will collect inputs (e.g., miles and gallons) from the user and calculate the MPG.So, as usual, fire up VS2012 and let's begin designing the form.
  • Step 1: Add four labels, two text boxes and two buttons, then change their name and text properties (see pictures below).
  • Step 2: Adding some code. Double-click "Close" button and add this line "this.Close();", then double-click on "Calculate MPG" button and add the code in the image below.
  • Step 3: Compile and run the application.
    Source File: Miles_per_Gallon.zip

Ars Technica