Navigation

Related Articles

Back to Latest Articles

C# Basics – Writing Code – Part 2

Let's create our first application with Windows Forms Application


Andraz Krzisnik
C# Basics – Writing Code – Part 2

When we create a project, first thing we will see are a bunch of windows.

c# tutorial

Let’s talk about what each of these windows do:

  • Editor is a window meant for writing code
  • Solution Explorer – you can manipulate all files of the project here
  • Properties window will show you properties of a selected object
  • Error List will show you,  if there are any errors or warning inside your code

Developing your first C# application

We will be working with Windows Forms Project in Microsoft Visual Studio. First, you need to create a project and inspect the code of the form by right-clicking on the form and selecting View Code option.

What you will see is, there are numerous namespaces being called in the first few lines of code, followed by a defined namespace which will hold all our code. In this namespace there will be a defined class and within it, a method that is being run when we run our project.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }
}

Now that we know where our code is going to be, let’s take a look at the business end of our application – that would be the designer. Let’s add a button on it by dragging it from the Toolbox and dropping it on the form..

C# programming

We will make this button display a message window saying “Hello World”. Double click the button and write into the button click function:

MessageBox.Show("Hello World");

csharp basics

Now click the button Start or press the f5 key on your keyboard. Your application should be running now. If we press the button we created, a message box will pop up and say “Hello World”.

c# application

Your code should look something like this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hello World");
        }
    }
}

This is it, you’ve made your first step at becoming a professional software creator.

Next Chapter
Show Comments (0)

Comments

Related Articles

C# Tutorial

C# Tutorial: How To Create Gaussian Blur

Gaussian blur is an image processing operation, that reduces noise in images. It does so by a convolution process, using a matrix that contains values calculated by a Gaussian...

Posted on by Andraz Krzisnik
C# Tutorial

C# Tutorial: How To Convert a Color Image To Grayscale

Grayscale images are basically black and white images. These images are good for programming, since their pixels hold only intensity values or in other words, shades of gray, they...

Posted on by Andraz Krzisnik