Introduction to C#; Lesson 1

In this post, you'll be learning basic c# code which will probably help you with your c# assignments.

Well lets be honest, printing "hello world" got to be the extreme basic of every programing language, so we'll also start with this along with the concept of taking input as well. So basically, our first lesson will be: Taking "input" from user and "printing" it.

First of all, I assume you all are well familiar with visual studio. If not, then here is a link to download visual studio:
Microsoft Visual Studio 

Don't worry! Its free of cost for students. It may have less functionalities as compared to professional or enterprise version, but trust me! It has everything a student or a beginner needs. All you need to do is, click on circled link.


Download the exe file, and select needed language, say c++ or c# etc and start coding! Now after installing and all, lets jump to how to create a file, design a form and finally coding it. After opening your application, press CTRL+SHIFT+N to create a new project. Select c# language, select Windows Form App (.NET Framework), type a name for your newly created project, here we've typed "newproj", and click "OK"
 


 A window, as shown bellow, will open.


Now, we are ready to go!
So today we have decided that we'll take input from user and print it.
This scenario give us multiple choices, we'll discuss them all one by one, but in this post we'll focus on our first method.
1.    Take input from user in a textbox and display it in a textbox.
2.    Take input from user in a textbox and display it in a messagebox.
3.    Take input from user in a textbox and display it in a label.

1. Take input from user in a textbox and display it in a textbox.

Step 1:

You need to first get your toolbox ready. From the menu bar, click on "view"and from the drop-down list select toolbox.

Step 2:
 
From toolbox, select Textbox and drag it on your form. As shown in image bellow.


Similarly, drag another textbox on your form and you are ready to go. But wait! we also require a button so that when user press the button, the second textbox will display user's input from first textbox. Button can be placed the same way we've placed textbox.Your form will finally look something like this.


Step 3:
Now you have to understand one thing, user will enter some certain input in first textbox and that certain input will be printed in second textbox but the tool that is invoking this is "button1", if the user will click on button1, only then we'll get the output in second textbox, so from all this we understood that we need to code in button1. For this, double click on button and a window like this will open.


Step 4:
 
Now in the click event code the following:

 private void button1_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox1.Text;

        } 

Step 5:

After typing this, click on "start" button
 

A window like this will appear, with cursor in first textbox. Type some text, here will type the classical "Hello World", and click on button1. You'll get the following
 

It can be observed that the text in first textbox has been copied as it is in second textbox as soon as we clicked on our button1. So here you go! In simple 5 steps you've coded your very first, the classical, "Hello world" code.
 
In the next post we'll discuss "Labels"and "MessageBox" , further we'll use "TextBox" , "Label" and "MessageBox" to input and outut "Hello World".
 

Comments

Popular posts from this blog

Introduction to C#; Lesson 2

Introduction to C#; Lesson 3

Thank you, next! Linked List in C++; Lesson 4