How to Add C# Controls in Windows Form ???
Hello everyone, hope you all are fine and having fun with your lives. Today, I am going to show you How to add C# Controls in your project. In our previous tutorial, we have seen an Introduction to C# Windows Forms in which we have created our first project and then we have discussed all about its different features like How to use Properties etc. and if you haven't read it yet then I would suggest you to read that one first as I am gonna continue from that tutorial onward.
So, I am assuming that you have read Introduction to C# Windows Forms and you have your first project created and ready to use. So, if you have dne exactly as I have shown in my previous tutorial then you have a for as shown in the below figure:
So, now we are gonna add some C# controls in this Windows form. So, let's get started with How to add C# Controls in it.
How to add C# Controls ???
- In our previous tutorial, we have discussed the Toolbox, which is available on the left side of your project.
- All the components in the toolbox are shown in the below figure:
- These are a lot of controls and we will cover most of them in our coming tutorials but rite now we are just interested in knowing How to add these C# Controls in Windows Form we created in our previous tutorial.
- So, the first C# Control I am gonna use is the Text Box, which is present in the Common Controls category.
- So, what you need to do is to drag this Text Box from toolbox to your Windows Form and then it will be placed on your Windows Form as shown in below figure:
- Now you can see in the above figure that our Form has a new C# Control in it which is a Text Box.
- Now when you select this Text Box then its Properties will open up in the Properties Section.
- From its Properties you can change many of its attributes.
- The two main attributes in Properties of any C# Controls are Name and Text.
- Name is the name of that Control with which we call it when we writing the code for that control.
- Text is the text which appears on that Control for user to view.
- Let me add another C# control on this Windows Form i.e. Button and then show you the difference between this Text and Name Property.
- So, drag a Button from the Toolbox and place it on this Windows Form as shown in below Figure:
- So, now we have two C# Controls on our Windows Form.
- Now I will select the Button and then its Properties will open up in the Properties Section and I am gonna change its Text to Click Here as shown in below figure:
- Now, lets see in the Form, what's written on the Button. :P
- Wow the magic happened :D and what we have written in the Text Section of the Properties of this Button has appeared on it.
- So, that's How the Text Property works. Whatever you written in the Text Section is appeared on that C# Control.
- The Name of Button Control is still button1 so whenever I am gonna call this button in the back end code I will use that Name.
- We are gonna do this in the next tutorial.
So, that's all for today. I hope you guys now have the idea How to add C# Controls in the Windows Form. In the coming tutorial, I am gonna add code behind each of these controls and will let you know How to use them. So, till then take care and have fun !!! :)
Introduction to C# Windows Forms
Hello everyone, hope you all are fien and having fun with your lives. Today, I am going to post a new C# tutorial in which we will have a Introduction to C# Windows Forms. Till now, we have seen Introduction to C# in which we have designed a console application in C# and after that we have discussed that project and write some code in it in First Code in C# Tutorial. So, till now we have an idea How to create a console application and how to run it easily.
I will continue with this C# console projects but first we have to discuss a little about C# Windows Forms. Microsoft Visual Studio is a very vast software so we can design different types of projects in it and also can use different languages. So, when talking about C#, we not only can design console applications but can also design C# Windows Forms in it. C# Windows Forms projects are much more easier than Console applications because in them you have a clear GUI in which you can drag and drop your components and can write the background code quite easily. So, let's get started with C# Windows Forms.
Introduction to C# Windows Forms
- First of all create a new project in Microsoft Visual Studio and in the settings select C# Windows Forms as shown in below figure:
- So, you can see in the above figure that all the steps are same as we did for creating a Console Application in Introduction to C# tutorial.
- But the only difference here is that instead of selecting Console application, we are selecting Windows forms Applications.
- Now, when you click OK then a new Project will open up as shown in below figure:
- So, now our project is ready and we can design any kind of project on it, but first let's discuss its features.
- If you have a look at it then you can see that it has three parts.
- The center part is where we have our Form1, we will change its name later.
- The on the left side we have a Toolbox which has all the components which can be used in this C# Windows Forms.
- On the right side, we have this Solution Explorer and below it we have Properties panel.
- So, let's discuss each of them separately.
Toolbox in C# Windows Forms
- Here's an image of all the components in the toolbox of C# Windows Forms:
- First of all there are categories like Common Controls etc and each of these categories has components in it which I have shown in detail by expanding these categories.
- So, these all controls can be used in C# Windows Forms and it depends on your application, which one you want to use.
- Most commonly used are buttons and text boxes etc which are in Common Controls section.
Solution Explorer in C# Windows Forms
- Here's an image of the Solution Explorer in C# Windows forms:
- This Solution Explorer shows all the files used in your C# Windows Forms Project.
- First of all, we have the Properties folder which has the AssemblyInfo.cs file, it contains the basics things about your project like name and copyrights etc.
- Then we have the References section, which has all the system library files.
- Next we have the Form1.Designer.cs file which contains out Form1 design.
- Finally we have the Program.cs file which has the back end code for our Form1.
- All the code we are gonna write for our C# Windows forms Project is gonna be add in this Program.cs file.
Properties in C# Windows Forms
- Here's an image of the Properties panel in C# Windows Forms:
- Now when you select any component on your C# Windows Forms then its properties open up.
- Rite now, I have selected the Main Form and its properties are appearing here.
- Now from these properties you can change many attributes of your project.
- Like for Form1, it says the name is Form1, so let me change the Text of it to something else as shown in below figure:
- Now you can see in the above figure that I have changed the Text of this Form1 to Introduction to C# Windows Forms.
- So, it will change the text of your Form to that as shown in below figure:
- Now you can see the Name of our Form has changed to the text which we have written in the Text section of its properties.
So, that's all about the Introduction to C# Windows form. In the coming tutorials, we are gonna add controls from the toolbox in this C# and then we will add some back end code to make them work. So, till then take care and have fun !!! :)
First Project in C#
Hello everyone, I hope you all are fine and having fun with your lives. Today, we are going to design our First Project in C#. In the previous tutorial, we have seen a detailed Introduction to C#, where we have studied its importance in the software world.
Today, we are going to create our first project in C#. So, let's get started with our first code in the C# Tutorial series.
First Project in C#
- I am going to use Microsoft Visual Studio 2019 Community Edition, which is free to use, and you can download it from its official website.
- There are different languages available in Visual Studio i.e. C, C++, C# and F#. C# is the most commonly used programming language among all.
- Now, I assume that you have installed Visual Studio and are ready to design your first project.
- So, open your Microsoft Visual Studio, and create a New Project by clicking File and then New Project, as shown in the figure on the right.
- You can also create a new project by clicking Ctrl+N.
- Now once you create the New Project, a new pop up window will open up, as shown in below figure:
- Now, as shown in the above figure, first of all, select the Visual C# as obviously we are gonna use the C# language in introduction to C#.
- Next, select the Windows and then Console Application.
- Next, we need to give a name to this Console Application, which I have given is MyFirstProject and finally click the OK button.
- Now in this window, we are gonna update our code in C#. Currently, it has simple Hello World code, as shown in below figure:
- Let's understand this code, line by line:
Namespace Declaration
- The first line of code is "using System", here the System is a built-in C# Namespace and we are declaring it at the top of our code.
- Currently, we are using just 1 namespace but later on, we are going to add a lot more and all these namespace declarations will come at the top of our code.
- You can think of a namespace as a library that has different classes & methods in it.
- So, when we declare it at the top, then all its classes become available to use in our code i.e. Console is a class of System namespace.
Projects Namespace
- Next, we have namespace TEPProject, which is the namespace of our newly created project, all of our classes will be placed inside this namespace.
- You can see this namespace has curly brackets { }, which has all the remaining code in them.
Program class
- When you run your code, the compiler finds the project's namespace and inside this namespace, it makes a search for a C# Class named Program and in Program class, it goes into the C# Method named Main.
- That's why we have the class Program inside our namespace TEPProject and inside this class, we have our static void Main(string[] args) function.
- This Main function is a static function and it has arguments with datatype string.
- This Main Function is the entry point of our compiler in our project, we have to write our code in this function or method. ( Functions are also called methods )
- Inside this Main function, we have simply printed Hello World to our Console.
- This Console is a member of namespace Systems, if we remove the namespace from the top then this Console will create the error.
So, now let's run our code and if everything goes fine then you will get something as shown in the below figure:
- We have Hello World printed in our console panel, it's doing nothing else.
- Moreover, if you are not getting things like C# Namespace or C# Methods etc. then no need to worry, we will cover all of them in coming tutorials in detail.
Extend C# Hello World Project
- Solution Explorer contains all files of your project and Program.cs is the actual code file in which I am going to write our code.
- So, let's add the below code in our Main function of Program.cs File:
Console.WriteLine("What are you learning ???");
string subject = Console.ReadLine();
Console.WriteLine("I am learning {0}", subject);
- Now your Program.cs file will look something as shown in the below figure:
- In the first line of code, I am printing "What are you learning ???" on the console.
- In the second line, I am waiting for incoming data from the console, which the user has to enter, and I am saving that data in a string variable called subject.
- In the third line, I am printing the data entered by the user on the Console.
- You must have noticed {0}, it's one way of adding data in a string and is called Place Holder Syntax.
- You can add as much data as you want, separated by commas, and can print them as {0} {1} {2} and so on.
- Let's run our code and we will get a similar output, as shown in the below figure:
- I have entered C# and it has printed it back, as you can see in the console.
- Now when you press any key, the console will stop.
- You can also use Concatenation instead of Place Holder Syntax, let's have a look at the third line, in below code:
- So, now I have used + sign instead of curly brackets {0}, it's called concatenating a string.
- If you run your code, you will get the same output, as shown in the above figure.
That's all for today. I hope you guys have got something out of it. In the next C# tutorial, we will have a look at
Introduction to Data Types in C#. So stay tuned and have fun !!! :)