Loading...
Tuesday 21 April 2020

Solution Explorer, Windows Form Designer and Code Windows in Visual Studio

In this C# video tutorial we will discuss the concept of solutions, projects. We will also discuss windows form designer ,  code window , solution explorer, creating new projects, adding multiple projects in a solution, and how to work and manage solutions and projects in solution explorer.

What is Solution

Solution is collection of projects. A Solution is nothing but container of one or more projects withing build information, visual studio window settings and any miscellaneous files that are not related with  a particular project. The file extension of solution is ".sln". Solutions may consist of one or more projects. In single solutions we can create different types of projects. The Start Up Project in Solution explorer is in bold. You can make a project as start up project in solution explorer by right clicking on it and then click "Start Up Project" from right click shortcut menu. So in simple words a solution is a container which organizes one or more related code projects such as a Widows Form Application project and a class library project.

What is Project

When you create a new project such as Windows Form Application , Asp.net Web Application or Website, Universal App etc in Microsoft Visual Studio that project is created in a solution. So Solution must contain at least one project, but you can add more projects in a single solution as discussed above. So what actually project is? It is collection of files that are compiled into executable, website or library. These files can include source code, data files, images, icons etc. A Project also contains configuration files and compiler settings  that might be needed by different components and services that your program communicates with.

Project File

Each project consist of MSBuild project file which is used by Microsoft Visual Studio to build the project in a solution. The type of project is indicated by its extension such as, C# Project file has "csproj" file extension , Visual Basic project has ".vbproj" extension, database project has file extension "dbproj" and so on.  The project file is an XML file that includes all the information and instructions that MSBuild needs to build the project, including the content, web sever or database server settings, versioning information, platform requirements and tasks to perform.

Creating a new windows form application project 

Click File Menu , move mouse to New Option and then click Project
New Project dialogue box will open , from left section click Visual C# template and then click Windows Form Application from middle section. In Bottom section of New Project Dialog Box,
Name: specify name of project which you want for example, I have specified "Calc"
Location: Specify location of your project
Solution: Choose Create New Solution , if you are creating project in a new solution
Solution Name: specify name of solution
creating new windows form application project
creating new windows form application project
We have created a new windows form application project, which is created in a solution called "calc.sln" and project file is "calc.csproj". csproj indicates that it is a c# project file.

Solution Explorer in visual studio:

Solution Explorer is a window which shows the list of all the projects in a solutions. We can add , remove, rename and manage all the projects and  their files in this window. It also contains command buttons like properties , Preview Selected Items, Show All Files , Collapse All, Refresh etc.
Solution Explorer in visual studio
Solution Explorer in visual studio

You can notice in above figure the number of projects in Solution is 1 project. Calc with icon C# is C# project, similarly if we have vb project in our solution it will be indicated with vb. If you want to open solution folder in file explorer, just right click on solution and than click "Open Folder in file explorer" option as shown in below figure.
Open solution folder in file explorer
Open solution folder in file explorer

Similarly if you want to open project folder in file explorer right click on it and click Open Folder in File Explorer.

How to add a new project in a solution 

Right Click on Solution Folder in File Explorer, move mouse to Add Option, then click New Project as shown below 
Adding a new project in a solution
Adding a new project in a solution
Now Click Installed template from left section and a project type from middle section , specify its name and click OK. In my case, suppose I want to add C# Class Library  project and name it, StandardCalcLib library for calculator standard library functions as shown in following figure. 
Adding class library project in solution
Adding class library project in solution
Now suppose we want to add a test vb project in solution explorer, Repeat the above steps, i.e. Right Click on Solution , move mouse to Add option then click new project , from installed template from left side , click Visual Basic, and from middle section of Add New Project dialogue box , click any project and then click OK, suppose I want to create a console application of visual basic, as shown in following figure. 
Adding new visual basic project in solution
Adding new visual basic project in solution
Now notice , in solution explorer , the no of projects is 3 , two C# projects and 1 Visual Basic - VB Project, as shown in following figure. 
Different projects in solution explorer
Different projects in solution explorer

Solution Properties 

Right click on solution, then from right click menu click properties, which will open solution property pages dialogue box. Same properties can be opened by pressing Alt+Enter or through the Settings icon in solution explorer. There are categories of solution properties , i.e. common properties and Configuration Properties. 
Common Properties: 
  • Startup Properties: This property is used to specify a start up project in a solution. The project which you set as start up project will be executed when you click on Start or press F5. 
  • Project Dependencies: This property is used to set a project as a dependent project for some specific project.  
  • Code Analysis Settings: This property is used to set code analysis settings such as platform and configuration option such as Release  , Debug, All Configurations, Active Configurations. 
  • Debug Source Files : This property is used to set or cut debugging files.  
Configuration Properties:
  • Configuration: This property is used to specify build or deploy configurations. 

Project Properties 

Right click on any project, and then from right click menu click Properties to open its properties. It will open various properties tabs for that project. Application properties tab is selected by default. Other properties tabs are Compile, Debug, References, Resources, Services , Settings, Signing, My Extensions, Security, Public and Code analysis as shown in following figure, which contains properties of calc project. 
project properties window
project properties window

Windows Form and Form Designer Window:

When you create a Windows Form Application, a Windows Form is generated by Visual Studio by default. The name of windows form which is generated by visual studio is form1.cs. You can notice windows form consist of a form designer window and a code window. The window form is nothing but a class which inherits from form class.  The windows form which is generated by visual studio by default can be deleted, renamed or modified according to our requirements. Following figure shows default windows form generated by visual studio in Solution Explorer. 
default windows form generated by visual studio in solution explorer
default windows form generated by visual studio in solution explorer

All the information and settings of that particular windows form are present in form1.designer.cs file The essential elements in form1.designer.cs file has InitializeComponent() method and Dispose() method which is overridden as shown in following figure. 
form1.designer.cs file
form1.designer.cs file


The IntializeComponent() method is called in code window of windows form in form1 class constructor as shown in following figure. 
       

Adding new windows form

Right click on project in solution explorer 
Then Click Windows Form 
add windows form in windows form application project
add windows form in windows form application project

Specify its name in Name Text Field, and click add 
Add new item windows form in C# windows form application project
Add new item windows form in C# windows form application project

Now you can see a new windows form has added as shown below
Windows Form and Solution Explorer in Windows C# Form Application Project
Windows Form and Solution Explorer in Windows C# Form Application Project 

A windows form is basically a class which inherits from Form class. Whenever you will add a new class in your project and then inherit that class from Form class, you will notice visual studio automatically create a form designer window and a code window. 

For Example right click on project , then move mouse to Add option, then click new item 
Add new item in C# windows form application project
Add new item in C# windows form application project
In Add New Item dialog box, Click Code From Left Pane under Visual C# items , and from middle pane click class, specify its name in Name Text Field (In my case it is Class1.cs)  and then click Add. 
add class and inherit from form
add class and inherit from form
Now following class is added in our project 
add following namespace in that class 
using System.Windows.Forms;

Now inherit that class from Form class 
Here is the complete code 

Now you can notice visual studio has generate form designer form class1.cs file as shown below
Class1 form designer generated
Class1 form designer generated

That Windows Form has a form designer window and a code window. Form designer window is generated. by default the name of that form designer window is "form1.cs" , which is actually a class which inherits from Form class. All the necessary information and settings of that form designer is written in form1.designer.cs file such as it contains InitializeComponent() method by default and it overrides Dispose method. You can delete, rename, Open and view code by right clicking on it. You can add as many as you want windows forms in you project. To add a new windows form in a project, Right Click on you Project , move mouse to add then click new item. then click Windows Form from add , new item dialogue box, specify name and then click Add.

Code Window: 

Code window is a window where we type code, The controls which we add in windows form designer window will not work until we write code for them in code window.

Solution Explorer, Windows Form, Form Design Window and Code Window Video Tutorial in Urdu Hindi


0 Comments:

Post a Comment

 
TOP