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

(C#) Chapter - 2

This document provides information about creating different types of C# programs using Visual Studio .NET, including console applications, Windows Forms applications, and WPF applications. It discusses how to create a new project for each type in Visual Studio and provides examples of basic code structures for console and Windows Forms applications. The document also compares WPF and Windows Forms applications, noting that WPF uses XAML and supports more advanced graphical capabilities.

Uploaded by

thwe thwe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

(C#) Chapter - 2

This document provides information about creating different types of C# programs using Visual Studio .NET, including console applications, Windows Forms applications, and WPF applications. It discusses how to create a new project for each type in Visual Studio and provides examples of basic code structures for console and Windows Forms applications. The document also compares WPF and Windows Forms applications, noting that WPF uses XAML and supports more advanced graphical capabilities.

Uploaded by

thwe thwe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Chapter - 2

Writing a First Program

Create your first C# program using Visual Studio .NET


Console Applications
Windows Forms Applications
WPF Applications
Windows Store Applications
Create your first C# program using
Visual Studio .NET
To create a new project in a new solution,
select File ➪ New Project to display the New
Project dialog.

(To add a new project to an existing solution,


select File ➪ Add ➪ New Project.)
The following list summarizes the project types that are
easiest to use with this book.

Windows Forms—A program that uses Windows Forms


controls and that runs on the Windows desktop

 WPF (Windows Presentation Foundation)—A program


that uses WPF controls and that also runs on the Windows
desktop

Console—A program that reads text input and displays


text output in a console window

Windows Store—A Windows Store app


 Both WPF and Windows Store applications use extensible
markup language (XAML) code to define the WPF controls
that make up their user interfaces.

 WPF controls are more flexible and graphically powerful


than Windows Forms controls.

 For example, WPF controls can display gradient


backgrounds, scale or rotate images, play video, and use
animation to change their appearance over time, all things
that are hard for Windows Forms controls.
Console Applications
Console applications have only a text interface, so they are
even simpler, as long as you don’t need graphical features
such as a drawing surface or the mouse.

When you make a console application, Visual Studio creates


a file called Program.cs that defines a class named Program.

To create a new project in a new solution, select File ➪


New Project to display the New Project dialog, select
Console Application, enter a project name, and enter a
project location. Then click OK to create the project.
Console Applications
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyConsoleApplication
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Press Enter to continue");
Console.ReadLine();
}
}
}
By default, when you run the program, Visual Studio
searches for a static method named Main and executes it.
Initially, there’s only one such method, so there’s no
problem.

If you create another class and give it a static method named
Main, Visual Studio cannot figure out which one to launch.
You can resolve that problem in a couple ways.

Another approach is to select Project ➪ Properties to open


the application properties window.
Write a program in C# to generate prime numbers between 1
and 1000. for (i = 2; i < 1000; i++)
{
using System;
for (j = 2; j < 1000; j++)
using System.Collections.Generic; {
using System.Linq; if (i % j == 0)
{
using System.Text;
break;
using System.Threading.Tasks; }
namespace ConsoleApplication1 }
{ if (i == j)
{
class Program Console.WriteLine(i + "is
{ prime number");
static void Main(string[] args) }
}
{ Console.ReadLine();
int i,j; }
}
}
Windows Forms Applications
Because console applications have no user interfaces, many
C# books write all their examples as console applications.
However, Windows Forms applications look nicer and have
greater flexibility.

For example, a Windows Forms application can display


images, draw graphics, and display results in controls such
as combo boxes or lists.

To create a new project in a new solution, select File ➪


New Project to display the New Project dialog, select
Window Form Application, enter a project name, and enter
a project location. Then click OK to create the project.
Windows Forms Applications

1
2

5
1. Solution Explorer—This area lists the files associated with the project.
Double-click a file to open it in the designer area.
2. Designer—This area contains designers that enable you to edit different
kinds of files. For example, the Code Editor enables you to edit C# code,
and the Form Designer enables you edit forms. Figure shows the Form
Designer editing the user interface for the form defined by the file
Form1.cs.
3. Toolbox—While you are editing a form, you can click a control in the
Toolbox to select it. Then you can click and drag to place an instance of
that control on the form.
4. Properties—If you select a control in the Window Designer, this area
displays that control’s properties and enables you to edit them. In Figure,
the form is selected, so this area is showing the form’s properties. For
example, you can see in the Properties Window that the form’s Text
property is set to Form1. In the Form Designer, you can see that the form
displays its text at the top.
5. Other windows—This area typically holds other windows such as the
Error List and Output Window. The program shown in Figure does not
currently have any errors, so the ErrorList is empty.
lblInformation.Text += "\n";
using System;
lblInformation.Text += "LastName - " +
using System.Collections.Generic; txtLastName.Text;
using System.ComponentModel; txtFirstName.Text = "";
using System.Data; txtLastName.Text = "";
using System.Drawing; btnClick.Text = "Finished!!";
using System.Linq; }
using System.Text; private void btnClear_Click(object sender,
using System.Threading.Tasks; EventArgs e)
using System.Windows.Forms; {
txtFirstName.Text = "";
namespace WindowsFormsApplication1 txtLastName.Text = "";
lblInformation.Text = "";
{ }
public partial class Form1 : Form
{ private void btnExit_Click(object sender, EventArgs
public Form1() e)
{ {
System.Environment.Exit(0);
InitializeComponent();
} }
private void btnClick_Click(object sender, EventArgs
} e)
{ }
lblInformation.Text= "";
lblInformation.Text += "FirstName - " + txtFirstName.Text;
WPF Applications
 Creating a WPF application is similar to creating a Windows Forms
application. Select File ➪ New Project, select the WPF Application template,
and enter a project name, enter a project location. Then click OK to create the
project.

 The center of Visual Studio contains a Window Designer similar to the Form
Designer in Window Form. The Solution Explorer and Properties Window are
on the right. (Although, there are many differences between the two versions of
the Properties Window.) The Error List appears at the bottom of both figures.

 One notable difference between the two displays is the XAML code window
at the bottom of the Window Designer.

 Use the Properties Window to set the button’s properties. Note that in WPF
applications the button’s Content property determines its caption or other
contents, not the Text property used by a Windows Forms button.
WPF Applications
using System;
using System.Collections.Generic; private void ClickMeButton_Click(object sender,
using System.Linq; RoutedEventArgs e)
using System.Text; {
using System.Threading.Tasks; lblInfo.Content = "";
using System.Windows;
using System.Windows.Controls; if (Apple.IsChecked==true)
using System.Windows.Data; {
using System.Windows.Documents; lblInfo.Content += "Apple Fruit\n";
using System.Windows.Input; }
using System.Windows.Media;
using System.Windows.Media.Imaging; if (Orange.IsChecked == true)
using System.Windows.Navigation; {
using System.Windows.Shapes; lblInfo.Content += "Orange Fruit\n";
namespace WpfApplication1 }
{
public partial class MainWindow : Window if (Watermelon.IsChecked == true)
{ {
public MainWindow() lblInfo.Content += "Watermelon Fruit";
{ }
InitializeComponent(); }
} }
}
Difference Between Window Form
Application and WPF Application
• WPF is a graphical subsystem that renders user interfaces in Windows
based applications. Window Form is the graphical API that provides
access to native Microsoft Windows interface elements.

• WPF is a markup language alternative which is known as XAML that


defines UI elements and relationships with other UI elements. Window
Form is an event drive application supported by the Microsoft .NET
framework.

• WPF application is able to be deployed on the desktop or hosted on a


web browser. Window Form application is truly based on the user’s
interface with the computer.
Windows Store Applications
 Windows Store applications are programs designed to run in Windows
8. They support the look and feel of Windows 8 applications. For
example, they can display tiles on the start screen and can update those
tiles at runtime to tell the user what they are doing.

 To create a Windows Store application, select File ➪ New Project,


select one of the Windows Store templates, enter a project name, and
enter a project location. Then click OK to create the project.
Windows Store Applications

You might also like