0% found this document useful (0 votes)
189 views

Lab 1 Introduction To Visual Programming

This document provides instructions for a lab on visual programming concepts in C# using Visual Studio. The objectives are to understand the Visual Studio IDE, solution explorer, creating and running console and Windows applications. Exercises include creating applications to calculate factorial, count vowels, grade a student, display a series sum in a message box, and print a multiplication table in a message box.

Uploaded by

nisrine omri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
189 views

Lab 1 Introduction To Visual Programming

This document provides instructions for a lab on visual programming concepts in C# using Visual Studio. The objectives are to understand the Visual Studio IDE, solution explorer, creating and running console and Windows applications. Exercises include creating applications to calculate factorial, count vowels, grade a student, display a series sum in a message box, and print a multiplication table in a message box.

Uploaded by

nisrine omri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Lab 1

Introduction to Visual Programming

The objective of this lab is to understand following concepts:

1.1The Visual Studio IDE

1.2 The Solution Explorer

1.3 C# Console Application


1.3.1 Creating a Project
1.3.2 Creating a File for code
1.3.3 Structure of a Simple C# Program

1.4 C# Compiling and Running

1.5 C# Windows Application


1.5.1The Design View
1.5.2The Code View

1.6 The MessageBox Class


1.6.1 MessageBox Icons
1.6.2 MessageBox Buttons

Visual Programming Lab Manual 3


1.1 The Visual Studio Integrated Development Environment

Any C# application can be written with a simple text editor (such as Notepad) as long
as the Framework Class Libraries (FCL) are present. Normally, however, it is much
simpler to program and manage C# applications using the Visual Studio Integrated
Development Environment (IDE), which contains:

Visual user interface designer


Context editor with Intellisense for typing code
Programming language compilers
Run/Debug environment

The Start Page is the first wide area that appears when Microsoft Visual Studio comes up. The
left section displays a list of recently used projects under Recent Projects. At any time, to display
the Start Page:

You can click the Start Page label in the top section of the Code Editor
On the main menu, you can click View Start Page

1.2 The Solution Explorer

The Solution Explorer is a window that displays a list of the files that
make up a project. To access the Solution Explorer:

If the Solution Explorer is not yet showing on the screen:


On the main menu, click View -> Solution Explorer

The Solution Explorer is made of four sections.Like every


regular window, the Solution Explorer is equipped with a title
bar that displays its name on the left side and three buttons on
the right side: The Window Position button displays a menu
when you click it:

Visual Programming Lab Manual 4


Under its title bar, the second section of the Solution Explorer is a toolbar:

1. The Properties button allows you to display the Properties window


2. The Show All Files button is used to show the hidden files of the project
3. As its name indicates, the Refresh button is used to refresh the list of files and resources
of the project.
4. The View Code button is used to show the code of a class

1.3 C# Console Application

The C# console applications display on a black window referred to as the DOS prompt or DOS
window. To start such an application, you can use Microsoft Visual Studio as described below.

1.3.1 Creating a Project

To create a new application, click File New Project...


In the middle list, click Console Application
Change the Name to FirstApp

1.3.2 Creating a Code File

To assist you with writing code, if you use Microsoft Visual C# Express or Microsoft Visual Studio
Professional, it includes a text editor referred to as the Code Editor. If you create your project as a
Console Application, a default file would be created and you can customize it.

If you start your program as an empty project, you must explicitly add a file to it. To do that:

On the main menu, click Project Add New Item...

OR

In the Solution Explorer, right-click the name of the project, position the mouse on Add,
and click New Item...
Any of these actions would display the Add New Item dialog box. From there, in the
middle list, click Code File. Accept the name or change it. When you click OK, a blank
document would display. In the same way, you can add as many files as you need for
your project.

Visual Programming Lab Manual 5


1.3.3 Structure of a Simple C# Program

// Namespace declaration
using System;

// Program class declaration


class WelcomeCS
{
// Main begins program execution.
static void Main()
{
// Write to console
Console.WriteLine("Welcome to the C# !");
Console.ReadLine (); // keep screen from going away when run from VS
}
}

The namespace declaration, using System; indicates that you are referencing the System
namespace. Namespaces contain groups of code that can be called upon by C# programs. With
the using System; declaration, you are telling your program that it can reference the code in the
System namespace.

The class declaration, class WelcomeCS, contains the data and method definitions that your
program uses to execute. A class is one of a few different types of elements your program can
use to describe objects. This particular class has no data, but it does have one method. This
method defines the behavior of this class (or what it is capable of doing).

The first thing you should be aware of is that C# is case-sensitive. The word "Main" is not the
same as its lower case spelling, "main". They are different identifiers.A static
modifierproceeds the word Main, meaning that this method works in this specific class only,

Visual Programming Lab Manual 6


rather than an instance of the class. This is necessary, because when a program begins, no
object instances exist.

1.4 C# Compiling and Running


Choose Build Build Solution from menu to compile without running.
There are 3 ways to run (and build, if necessary) an application:

a. Choose Debug Start Debugging from menu


b. Press F5 and
c. Click the Debug tool in the toolbar

1.5 C# Windows Applications


To create a windows application, click File New Project...
In the list, click Windows Forms Application
Change the name to Welcome
Open the program.cs file and modify Main() as below.

class WelcomeCS
{
static void Main(string[] args)
{
\n to\n C#\
}
}

Example 1.5.1: Displaying the sum of in a MessageBox.

static void Main()


{
int sum = 0;
string series = " ";
for (int i = 1; i <= 100; i++)
{
sum = sum + i;
series += i;
series += "+";
}
series += "=" + sum;
MessageBox.Show(series,"Sum of Series");
}

Visual Programming Lab Manual 7


1.5.1 The Design View

The IDE displays a design-time view of the visible portion, if any, of the object being
designed.
A Designer view of a formobject appears by default in a new project.
The form will be a window at run time.

1.5.2 The Code View

The IDE provides an editor for building the actual code for the class or classes that make
up your new project.
Pre-existing (library) code is not visible here.
Choose View Code icon in Solution Explorer or click Code tab in Designer Window.

Visual Programming Lab Manual 8


1.6 The MessageBox Class
The MessageBox class implements functions to display simple message boxes. The Show()
method has four parameters:text to be displayed, caption, button style, icon

1.6.1 MessageBox Icons

1.6.2 MessageBox Buttons

The MessageBoxButtons options are given below:

MessageBoxButton.OK
MessageBoxButton.OKCANCEL
MessageBoxButton.YESNO
MessageBoxButton.YESNOCANCEL
MessageBoxButton.RETRYCANCEL
MessageBoxButton.ABORTRETRYIGNORE

Visual Programming Lab Manual 9


Lab Exercise #1

Exercise1.1
Create a console application to calculate the factorial of the number using for loop. For example
if n=5 then factorial of 5 will be 1*2*3*4*5= 120.

[Hint: for loop syntax: for(int i=0;i<10;i++){}]

Exercise 1.2
Create a console application, ask the user to input a string, save the string in a variable and then
check whether the string consists of any vowels (a, e, i, o, u). Display the total number of vowels
present in the string.

Exercise 1.3
Create a Console application in which you have to input a number from the user and then output
the respective grade the student got using if-else statement with the help of the table given
below:
A--------------------- (87-100)
B+------------------- (80-86)
B--------------------- (72-80)
C+------------------- (66-71)
C--------------------- (61-65)

Exercise1.4
Create a Windows Form application, calculate the sum of the series and display the series and its
sum in a messagebox.

Se

Exercise 1.5
Create a Windows Form application and then print the table of 2 in a MessageBox.
2x1=2
2x2=4
2x3=6
.
.
.
2x10=20

Visual Programming Lab Manual 10

You might also like