43
Introduction to Windows Forms Application By N Hanumantha Rao MCA

Introduction to Windows Forms Application

  • Upload
    rod

  • View
    77

  • Download
    0

Embed Size (px)

DESCRIPTION

Introduction to Windows Forms Application. By N Hanumantha Rao MCA. Microsoft Visual Studio 2008. Start  All Programs  Microsoft Visual Studio 2008  Microsoft Visual Studio. Starting New C#.NET Project. Setting Path. IDE (Integrated Development Environment):. - PowerPoint PPT Presentation

Citation preview

Page 1: Introduction to Windows Forms Application

Introduction to Windows Forms Application

By

N Hanumantha RaoMCA

Page 2: Introduction to Windows Forms Application

Microsoft Visual Studio 2008

Start All Programs Microsoft Visual Studio 2008 Microsoft Visual Studio

Page 3: Introduction to Windows Forms Application

Starting New C#.NET Project

Page 4: Introduction to Windows Forms Application

Setting Path

Page 5: Introduction to Windows Forms Application

IDE (Integrated Development Environment):

• This contains various components like• i. Solution Explorer• ii. Tool Box• iii. Server Explorer• iv. Properties Window• v. Output Window • vi. Error List Window etc

Page 6: Introduction to Windows Forms Application

Windows Form

• A form is a container, which can hold all other controls within it.

• In C#.NET every form is treated as a class and is derived from System.Windows.Forms.Form class

• In C#.NET Window Form we can place various controls like Text Box, Button, Check box, Radio buttons etc.

• In C#.NET Win forms applications every control has a special class and is present in System.Windows.Forms namespace observe the following table.

Page 7: Introduction to Windows Forms Application

TextBox

• This component will be derived from System.Windows.Forms.TextBox class

• Properties of TextBox class are given below.• Border style:• None: No border appears to the TextBox• Single: Single Line Border appears to the

TextBox• Fixed3D: 3D Border appears to the TextBox.

Page 8: Introduction to Windows Forms Application

Text Box Properties

Enabled• True (Default) : Control can be accessed at run time.• False : Control cannot be accessed at run time.• Font: Used to set or get font attributes like Size, Styles,

Nameetc of the control

• ForeColor : sed to set or get foreground color of the control

• Location X ( Left) Y ( Left) : Used to set or get the distance of the control from the top and left borders of the form

Page 9: Introduction to Windows Forms Application

Events of Text Box• 1 Click Occurs when the text box is clicked.• 2 DoubleClick Occurs when the control is double – clicked.• 3 Enter Occurs when the control is entered.• 4 FontChanged Occurs when the Font property value changes.• 5 ForeColorChanged Occurs when the FontColor property value

changes.• 6 Invalidated Occurs when a control’s display requires redrawing.• 7 KeyDown Occurs when a key is pressed while the control has focus• 8 KeyPress Occurs when a key is pressed while the control has focus• 09 KeyUp Occurs when a key is released while the control has focus• 10 Leave Occurs when the input focus leaves the control.

Page 10: Introduction to Windows Forms Application

Events of Text Box• 11 LocationChanged Occurs when the Location property valued

has changed.• 12 MouseClick Occurs when the control is clicked by the mouse.• 13 MouseDoubleClick Occurs when the control is double clicked

by the mouse.• 14 MouseDown Occurs when the mouse pointer is over the

control and a mouse• button is pressed.• 15 MouseEnter Occurs when the mouse pointer enters the

control.• 16 MouseHover Occurs when the mouse pointer rests on the

control• 17 MouseLeave Occurs when the mouse pointer leaves the

control.

Page 11: Introduction to Windows Forms Application

Example 1 Program with TextBox:

• Purpose 1 : To get the Idea about, how events will work

• What to do: when focus leaves from TextBox• Action: Display “ Welcome to C#” in TextBox3 and

Change the back ground color of tbox4.• 2. When user clicks on TextBox1• Action: Change the Back Ground color of Form.• Design the form as below and select textbox1 and

write the code for 1. Leave 2. Click events

Page 12: Introduction to Windows Forms Application

.

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace WindowsFormsApplication2{public partial class Form1 : Form{public Form1(){InitializeComponent();}

Page 13: Introduction to Windows Forms Application

Code

private void textBox1_Leave(object sender, EventArgs e){textBox3.Text = "Welcome to C#";textBox4.BackColor = Color.Green;}private void textBox1_Click(object sender, EventArgs e){this.BackColor = Color.Yellow ;}}}

Page 14: Introduction to Windows Forms Application

Button, TextBox, Label, LinkLabel

Button: To accept any action from the user we use this control.TextBox: Standard Input control, can be used even to produce output editable / read onlyLabel: To display static Text. Doesn’t get focus at run time. Used to display messagesprogrammatically or as constants.Link Label: To display hyperlink kind of actions. Replacement for button. DesktopHyperlinks.

Page 15: Introduction to Windows Forms Application

Textbox, Label and Button controls :

• Purpose : 1. To understand real time naming conventions• 2. To understand and implement functional or field level

validations.• In general in real time programming programmer is not supposed

to assign the names of the controls rather project Leader / System Analyst will design the Forms, Reports and the controls to be present in those, what names to be given to the controls with in the Design Document.

• Programmers should use the same Names given in the Design Documents.

• In general control name can be divided in to two parts.• i. Control Identification• ii. Purpose of the control

Page 16: Introduction to Windows Forms Application

Textbox, Label and Button controls :

i. Control Identification• TextBox txt Button btn / cmd Label lbl• ListBox lst Combobox cmb RadioButton rbt CheckBox

chk

ii. Purpose of the control:• For Accepting Ac No in Bank Applications Acno• For Accepting StudentName in College• Application SName• Examples of some sample Control Names are Like• txtAcNo txtSName btnSubmit• chkgroup rbtgender etc.

Page 17: Introduction to Windows Forms Application

Maths CalculationsEx 2: Design the form as hereStep1: Take 3 textboxes set names as txtfirstno, txtsecondno, txtresultStep2: Take 4 Labels and change their font colors and styles according to visible purpose.Step3: Take 4 buttons and give the text to the buttons as Add, Subtract, Multiply and Devide.Step 4: Set the names of buttons as btnadd, btnsubtract, btnmultiply and btndevide.Step 5: write the following code for each button click event.

Page 18: Introduction to Windows Forms Application

CODE

private void btnadd_Click(object sender, EventArgs e){a = Convert.ToInt32(txtfirstno.Text);b = Convert.ToInt32(txtsecondno.Text);c = a + b;txtresult.Text = c.ToString();}

private void btnsubtract_Click(object sender, EventArgs e){a = Convert.ToInt32(txtfirstno.Text);b = Convert.ToInt32(txtsecondno.Text);c = a - b;txtresult.Text = c.ToString();}

Page 19: Introduction to Windows Forms Application

Code for Multiply and Devideprivate void btnmultiply_Click(object sender, EventArgs e){a = Convert.ToInt32(txtfirstno.Text);b = Convert.ToInt32(txtsecondno.Text);c = a * b;txtresult.Text = c.ToString();}

private void btndevide_Click(object sender, EventArgs e){a = Convert.ToInt32(txtfirstno.Text);b = Convert.ToInt32(txtsecondno.Text);c = a / b;txtresult.Text = c.ToString();}

Page 20: Introduction to Windows Forms Application

Grouping controls:

• Types of Grouping Controls: we have C#.NET supports• following types grouping controls.• GroupBox Panel• SplitContainer Tabcontrol

Differencs between GroupBox and Panel• S No Group Box Panel• 1 Has Text Area Has No Text Area• 2 Does not provide scrolling facilities Provides scrolling

facilities.(set AutoScroll true)

• 3 Occupies more design space Occupies less design space.• 4 Occupies less memory Occupies more memory.

Page 21: Introduction to Windows Forms Application

Program with GroupBox and Panel:

Page 22: Introduction to Windows Forms Application

Codeprivate void radioButton4_CheckedChanged(object sender, EventArgs e) { txtname.BackColor = Color.White; }• private void optred_CheckedChanged(object sender, EventArgs e)

{ txtname.BackColor = Color.Red; }• private void optgreen_CheckedChanged(object sender, EventArgs e)

{ txtname.BackColor = Color.Green; }• private void optblue_CheckedChanged(object sender, EventArgs e)

{ txtname.BackColor = Color.Blue; }• private void optyellow_CheckedChanged(object sender, EventArgs e)

{ txtname.ForeColor = Color.Yellow;}

Page 23: Introduction to Windows Forms Application

Code

• private void optmagenta_CheckedChanged(object sender, EventArgs e) { txtname.ForeColor = Color.Magenta;

}• private void optgold_CheckedChanged(object sender, EventArgs

e) { txtname.ForeColor = Color.Gold; }• private void optblack_CheckedChanged(object sender,

EventArgs e) { xtname.ForeColor = Color.Black; }

Page 24: Introduction to Windows Forms Application

Tab Control

• This control is used to create required tab pages so that each tab page can contain

• required number of controls to group together.

• Properties With TabControl Class:• TabPages Used to set or get the tab pages

with in it the form of collection

Page 25: Introduction to Windows Forms Application

program with TabControl:Step 1: design the form as below with tab control for two menus as pg and btech.

Page 26: Introduction to Windows Forms Application

Codeprivate void btnpg_Click(object sender, EventArgs e){lblpg.Text = tbnamepg.Text + " registerd into " + tbquali.Text + " department ";}

private void btnbtech_Click(object sender, EventArgs e){ lblbtech.Text = tbnamebtech.Text + " is joined into " + tbbranch.Text; }

Page 27: Introduction to Windows Forms Application

OutPut

Page 28: Introduction to Windows Forms Application

Emp ProjView (button double click) code:{ if (int.Parse(txtsal.Text) > 5000)

lblmsg.Text = txtname.Text + " commission is 500";

else if (int.Parse(txtsal.Text) > 4000)lblmsg.Text = txtname.Text + " commission is 400";else lblmsg.Text = txtname.Text + " commission is 300";}

Page 29: Introduction to Windows Forms Application

Ex Emp

• private void button1_Click(object sender, EventArgs e)• {• double hra, da, ta, pf, totsal, bsal;• bsal = int.Parse(txtsal.Text);• hra=(bsal*10)/100;• da = (bsal * 8) / 100;• ta = (bsal * 9) / 100;• pf = (bsal * 2) / 100;• totsal = bsal + ta + da + hra + pf;• txtnetsal.Text = txtname.Text + " Salory details \n" + "HRA : "• + hra.ToString() + "\n" + "DA : " + da.ToString() + "\n" +• "TA : " + ta.ToString() + "\n" +• "PF : " + pf.ToString() + "\nTotal salory :" + totsal.ToString();• }

For the above form display following information based on basic saloryHRA – 10% DA – 8% TA – 9% PF – 2% Totalsal = bsal+allowance – deduction

Page 30: Introduction to Windows Forms Application

Output

Page 31: Introduction to Windows Forms Application

Picture Box

• PictureBox1.Image = Image.Fromfile(“file path.jpg”);

Or• Bitmap bmp = new Bitmap(“ d:\jan\myimages

\tiger.bmp”);• PictureBox1.Image=bmp;

Page 32: Introduction to Windows Forms Application

Image Changing• If pictures automatically change from one image to another and another like

continuous• then the code was• if(i==0)• { PictureBox1.Image = Image.FromFile(“d:\jan\myimages\sun.jpg”);• i++; }• else if (i = =1)• { PictureBox1.Image = Image.FromFile(“d:\jan\myimages\moon.jpg”);• i++; }• else if (i = =2)• { PictureBox1.Image = Image.FromFile(“d:\jan\myimages\apple.jpg”);• i++; }• else if (i = =3)• { PictureBox1.Image = Image.FromFile(“d:\jan\myimages\horse.jpg”);• i=0; }

Page 33: Introduction to Windows Forms Application

Timer :• To execute a given code at regular intervals.

(Normally we have to use threads and• monitoring them to perform timer activity. No

logic replaces the timer activity)• Exercise 6: To display time with changing as

seconds wise

Page 34: Introduction to Windows Forms Application

Digital Clock

• · Timer _ properties _ Enabled _ true• · _Interval _ 1000 event _ tick• Double click timer control and write the follow

code• private void timer1_Tick(object sender,

EventArgs e) {• label2.Text = DateTime.Now.ToLongTimeString();• DateTime.Now.ToString("dd-m-yyyy hh:mm:20");

}

Page 35: Introduction to Windows Forms Application

Output

Page 36: Introduction to Windows Forms Application

Browse Code

• private void btnBrowse_Click(object sender, EventArgs e)

• {• openFileDialog1.Filter = "jpeg|*.jpg|bmp|

*.bmp|all files|*.*";• DialogResult res = openFileDialog1.ShowDialog();• if (res == DialogResult.OK)• {• pictureBox1.Image =

Image.FromFile(openFileDialog1.FileName); } }

Page 37: Introduction to Windows Forms Application
Page 38: Introduction to Windows Forms Application
Page 39: Introduction to Windows Forms Application
Page 40: Introduction to Windows Forms Application
Page 41: Introduction to Windows Forms Application
Page 42: Introduction to Windows Forms Application
Page 43: Introduction to Windows Forms Application