You are on page 1of 55

VISUAL STUDIO

• is a complete set of development tools for building


ASP.NET Web applications, XML Web Services,
desktop applications, and mobile applications.

• Visual Basic, Visual C#, and Visual C++ all use the
same integrated development environment (IDE),
which enables tool sharing and eases the creation of
mixed-language solutions.
C# INTRODUCTION
• is a simple, modern, general-purpose, object-oriented
programming language developed by Microsoft within
its .NET initiative led by Anders Hejlsberg.

• programming based on C and C++ programming


languages, so if you have a basic understanding of C or
C++ programming, then it will be fun to learn C#.
WEB-BASED
• A web application or web app is any program that
runs in a web browser.

• It is created in a browser-supported programming


language (such as the combination of JavaScript,
HTML and CSS) and relies on a web browser to render
the application.
• The .Net framework applications are multi-platform
applications. The framework has been designed in
such a way that it can be used from any of the
following languages: C#, C++, Visual Basic, Jscript,
COBOL, etc
It is worth to note the following points:
 C# is case sensitive.
 All statements and expression must end with a
semicolon (;).
 The program execution starts at the Main method.
 Unlike Java, program file name could be different from
the class name.
Data Types
 Bool – is one of the simplest data types. It can consist
of 2 values- false and true.
 int – is short for integer, a data types for storing
number without decimals. When working with
number, int is the most commonly used.
 String – is used for storing text, that is, a number of
chars in c#, string are immutable, which means that
strings are never changed after they have been created.
 char – is used for storing single character.
 float – is one of the data types used to store number
which may or may not contain decimals.
Following are some of the components of the
.Net framework:
 Common Language Runtime (CLR)
 The .Net Framework Class Library
 Common Language Specification
 Common Type System
 Metadata and Assemblies
 Windows Forms
 ASP.Net and ASP.Net AJAX
 ADO.Net
 Windows Workflow Foundation (WF)
 Windows Presentation Foundation
 Windows Communication Foundation (WCF)
 LINQ
Integrated Development Environment
(IDE) for C#
 Microsoft provides the following development tools for
C# programming:
 Visual Studio 2010 (VS)
 Visual C# 2010 Express (VCE)
 Visual Web Developer
Creating Hello World Program
A C# program consists of the following parts:
 Namespace declaration
 A class
 Class methods
 Class attributes
 A Main method
 Statements and Expressions
 Comments
simple code that prints the words
"Hello World":
using System;
namespace HelloWorldApplication
{
class HelloWorld
{
static void Main(string[] args)
{ /* my first program in C# */
Console.WriteLine("Hello World");
Console.ReadKey();
}
}
}
Let us look at the various parts of the given program:
• The first line of the program using System; - the using keyword
is used to include the System namespace in the program.
A program generally has multiple using statements.

• The next line has the namespace declaration. A namespace is a


collection of classes. The HelloWorldApplication namespace
contains the class HelloWorld.

• The next line has a class declaration, the


class HelloWorld contains the data and method definitions that
your program uses.
Classes generally contain multiple methods. Methods define the
behavior of the class. However, the HelloWorld class has only one
methodMain.
• The next line defines the Main method, which is the entry
pointfor all C# programs.
The Main method states what the class does when executed.
• The next line /*...*/ is ignored by the compiler and it is put to
addcomments in the program.
• The Main method specifies its behavior with the
statementConsole.WriteLine("Hello World");

WriteLine is a method of the Console class defined in


the Systemnamespace.

This statement causes the message "Hello, World!" to be


displayed on the screen.

The last line Console.ReadKey(); is for the VS.NET Users. This


makes the program wait for a key press and it prevents the screen
from running and closing quickly when the program is launched
from Visual Studio .NET.
method
• Is a group of statements that together perform a task.
Every C# program has at least one class with a method
named Main.

To use a method, you need to:


 Define the method
 Call the method
Defining Methods in C#

 When you define a method, you basically declare the


elements of its structure.
 The syntax for defining a method in C# is as follows:
Following are the various elements of a method:
 Access Specifier: This determines the visibility of a variable or a
method from another class.

 Return type: A method may return a value. The return type is the data
type of the value the method returns. If the method is not returning
any values, then the return type is void.

 Method name: Method name is a unique identifier and it is case


sensitive. It cannot be same as any other identifier declared in the class.

 Parameter list: Enclosed between parentheses, the parameters are


used to pass and receive data from a method. The parameter list refers
to the type, order, and number of the parameters of a method.
Parameters are optional; that is, a method may contain no parameters.

 Method body: This contains the set of instructions needed to


complete the required activity.
 class Program
{
public static void Main(string[] args)
{
Program n = new Program();
n.sum();
n.product();
Console.ReadKey();
}
public void sum()
{
int num1 = 300, num2 = 400;
int add = (num1 + num2);
Console.WriteLine("Total:{0}", add);
}
public void product()
{
int num1 = 300, num2 = 400;
int product = (num1 * num2);
Console.WriteLine("product:{0}", product);
}
}
Or using static
 class Program
 {
 public static void Main(string[] args)
 {
 //Program n = new Program();
 //n.sum();
 //n.product();
 Program.sum();
 Console.ReadKey();
 }
 public static void sum()
 {
 int num1 = 300, num2 = 400;
 int add = (num1 + num2);
 Console.WriteLine("Total:{0}", add);
 }
 class Program
 {
 public static void Main(string[] args)
 {
 Program ename = new Program();
 ename.personalinfo();
 Console.ReadKey();
 }
 public void personalinfo()
 {
 string name = "sofia arquero";
 string xaddress = "Muntinlupa City";

 Console.WriteLine("Good day:{0}", name);


 Console.WriteLine("Your City is in:{0}", xaddress);
 }
Passing the method

class Program
{
public static void Main(string[] args)
{
int val1 = 0, val2 = 0;
//accept value1 from the user
Console.WriteLine("Enter first value");
val1 = Convert.ToInt32(Console.ReadLine());
//accept value2 from the user
Console.WriteLine("Enter first value");
val2= Convert.ToInt32(Console.ReadLine());
Program ename = new Program();
ename.sum(val1, val2);
Console.ReadKey();
}
public void sum( int num1, int num2)
{
int add = (num1 + num2);
Console.WriteLine("Total is:{0}", add);
}
 class Program
 {
 public static void Main(string[] args)
 {
 int val1 = 0, val2 = 0;
 string fullname;
 //accept value1 from the user
 Console.WriteLine("Enter first value");
 val1 = Convert.ToInt32(Console.ReadLine());
 //accept value2 from the user
 Console.WriteLine("Enter first value");
 val2= Convert.ToInt32(Console.ReadLine());
 Program ename = new Program();
 ename.sum(val1, val2);
 Console.WriteLine("Type your name:");
 fullname = Convert.ToString(Console.ReadLine());
 ename.info(fullname);
 Console.ReadKey();
 }
 public void sum( int num1, int num2)
 {
 int add = (num1 + num2);
 Console.WriteLine("Total is:{0}", add);
 }
 public void info(string name)
 {
 Console.WriteLine("Good day:{0}", name);
 }
C# IDE
IDE PARTS
 1. Menu Bar
 2. Standard Toolbar
 3. ToolBox
 4. Forms Designer
 5. Output Window
 6. Solution Explorer
 7. Properties Window
C# Windows Forms
The first step is to start a new project and build a form.
Open your Visual Studio and select File->New Project
and from the new project dialog box select Other
Languages->Visual C# and select Windows Forms
Application.
For example, if you want to change the back color of the
form to Brown, you can code in the Form1_Load event
like the following.

Private void Form1_Load(object sender, EventArgs e)


{
this.BackColor = Color.Brown;
}
C# Label Control
 Labels are one of the most frequently used C# control.
We can use the Label control to display text in a set
location on the page. Label controls can also be used
to add descriptive text to a Form to provide the user
with helpful information. The Label class is defined in
the System.Windows.Forms namespace.
 private void Form1_Load(object sender, EventArgs e)
{
this.BackColor = Color.Brown;
label2.Text = "I'm a Label2 Control";
}
C# Button Control
 Windows Forms controls are reusable components
that encapsulate user interface functionality and are
used in client side Windows applications. A button is a
control, which is an interactive component that
enables users to communicate with an application.
The Button class inherits directly from the ButtonBase
class. A Button can be clicked by using the mouse,
ENTER key, or SPACEBAR if the button has focus.
button1.Text = "Click Here";

private void button1_Click(object sender, EventArgs e)


{
MessageBox.Show("http://www.facebook.com");
}
C# TextBox Control
 A TextBox control is used to display, or accept as input,
a single line of text. This control has additional
functionality that is not found in the standard
Windows text box control, including multiline editing
and password character masking.
 A text box object is used to display text on a form or to
get user input while a C# program is running.
 For displaying a text in a TextBoxcontrol, you can code
like this
textBox1.Text = "http://yahoo.com";
textBox1.Width = 250;
textBox1.Height = 50;
textBox1.BackColor = Color.Blue;
textBox1.ForeColor = Color.White;
label2.Text = textBox1.Text;
textBox1.MaxLength = 40;
textBox1.ReadOnly = true;
textBox1.Multiline = true;
textBox1.PasswordChar = '*';
How to Numeric only textbox?

 How to retrieve integer values from textbox?


 int num1, num2, sum;
 num1 = int.Parse(textBox3.Text);
 num2 = int.Parse(textBox4.Text);
 sum = (num1 + num2);
 MessageBox.Show("sum" + sum);
 textBox5.Text = (sum.ToString());
Exit Program
 System.Windows.Forms.Application.Exit();

Open newform
var nextform = new Form2();
nextform.Show();
this.Hide();
answer = firstTextBoxNumber + secondTextBoxNumber;
 int TF, Down, Cash, Change;

 TF = int.Parse(tFTextBox.Text );
 Down = int.Parse(downTextBox.Text);
 Cash = int.Parse(cashTextBox.Text);

 Change = (Cash - TF);

 MessageBox.Show(Change.ToString());
 downTextBox.Text =(Change.ToString());
COMPUTE
int num1, num2, sum;
num1 = int.Parse(textBox1.Text);
num2 = int.Parse(textBox2.Text);
sum = (num1 + num2);
MessageBox.Show("sum" + sum);
textBox4.Text = (sum.ToString());

CLEAR

textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
pictureBox1.Visible = false;
pictureBox2.Visible = false;
Combo box
 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
 {
 if (comboBox1.SelectedItem == "Meal 1")
 {
 textBox3.Text = "500";

 }
 else if (comboBox1.SelectedItem == "Meal 2")
 {
 textBox4.Text = "600";
 }
 else if (comboBox1.SelectedItem == "Meal 3")
 {
 textBox5.Text = "1000";
 }
 }
private void Form3_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("Meal 1");
comboBox1.Items.Add("Meal 2");
comboBox1.Items.Add("Meal 3");
pictureBox1.Visible = false;
pictureBox2.Visible = false;
C# Label Control

 Labels are one of the most frequently used C# control.


We can use the Label control to display text in a set
location on the page. Label controls can also be used
to add descriptive text to a Form to provide the user
with helpful information. The Label class is defined in
the System.Windows.Forms namespace.

 label1.Text = "This is my first Label";


C# Button Control

 Windows Forms controls are reusable components


that encapsulate user interface functionality and are
used in client side Windows applications. A button is a
control, which is an interactive component that
enables users to communicate with an application.
C# TextBox Control

 A TextBox control is used to display, or accept as input,


a single line of text. This control has additional
functionality that is not found in the standard
Windows text box control, including multiline editing
and password character masking.
C# ComboBox Control

 C# controls are located in the Toolbox of the


development environment, and you use them to create
objects on a form with a simple series of mouse clicks
and dragging motions. A ComboBox displays a text box
combined with a ListBox, which enables the user to
select items from the list or enter a new value .
C# ListBox Control

 The ListBox control enables you to display a list of


items to the user that the user can select by clicking.
C# Checked ListBox Control

 The CheckedListBox control gives you all the


capability of a list box and also allows you to display a
check mark next to the items in the list box.
C# RadioButton Control

 A radio button or option button enables the user to


select a single option from a group of choices when
paired with other RadioButton controls. When a user
clicks on a radio button, it becomes checked, and all
other radio buttons with same group become
unchecked
C# CheckBox Control

 CheckBoxes allow the user to make multiple selections


from a number of options. CheckBox to give the user
an option, such as true/false or yes/no
 checkedListBox1.Items.Add("Sunday", CheckState.Checked);
 checkedListBox1.Items.Add("Monday", CheckState.Checked);
 checkedListBox1.Items.Add("Tuesday", CheckState.Checked);
 checkedListBox1.Items.Add("Wednesday", CheckState.Checked);
 checkedListBox1.Items.Add("Thursday", CheckState.Checked);
 checkedListBox1.Items.Add("Friday", CheckState.Checked);


 // checkedListBox1.Items.Add("Monday", CheckState.Unchecked);
 //checkedListBox1.Items.Add("Tuesday", CheckState.Indeterminate);
 // checkedListBox1.Items.Add("Wednesday", CheckState.Checked);
 // checkedListBox1.Items.Add("Thursday", CheckState.Unchecked);
 //checkedListBox1.Items.Add("Friday", CheckState.Indeterminate);
 //checkedListBox1.Items.Add("Saturday", CheckState.Indeterminate);

 listBox1.Size = new System.Drawing.Size(245, 200);
 listBox1.Font = new Font("Georgia", 16);
 listBox1.BorderStyle = BorderStyle.FixedSingle;
 listBox1.BackColor = System.Drawing.Color.Orange;
 listBox1.ForeColor = System.Drawing.Color.Black;
 listBox1.Items.Add("ICE CREAM");
 listBox1.Items.Add("CAKE");
 listBox1.Items.Add("SALAD");

You might also like