1.32k likes | 2.62k Views
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):.
E N D
Introduction to Windows Forms Application By N HanumanthaRao MCA
Microsoft Visual Studio 2008 Start All Programs Microsoft Visual Studio 2008 Microsoft Visual Studio
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
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.Formclass • 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.
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.
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, Name etc of the control • ForeColor : sedto 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
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 KeyPressOccurs 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.
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.
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
. 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(); }
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 ; } } }
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 only Label: To display static Text. Doesn’t get focus at run time. Used to display messages programmatically or as constants. Link Label: To display hyperlink kind of actions. Replacement for button. Desktop Hyperlinks.
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
Textbox, Label and Button controls : i. Control Identification • TextBox txt Button btn / cmd Label lbl • ListBoxlstComboboxcmbRadioButtonrbtCheckBoxchk 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 • txtAcNotxtSNamebtnSubmit • chkgrouprbtgender etc.
Maths Calculations Ex 2: Design the form as here Step1: Take 3 textboxes set names as txtfirstno, txtsecondno, txtresult Step2: 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.
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(); }
Code for Multiply and Devide private 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(); }
Grouping controls: • Types of Grouping Controls: we have C#.NET supports • following types grouping controls. • GroupBox Panel • SplitContainerTabcontrol Differencsbetween 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.
Code private 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; }
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; }
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
program with TabControl: Step 1: design the form as below with tab control for two menus as pg and btech.
Code private 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; }
EmpProj View (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"; }
Ex Emp For the above form display following information based on basic salory HRA – 10% DA – 8% TA – 9% PF – 2% Totalsal = bsal+allowance – deduction • 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() + "\nTotalsalory :" + totsal.ToString(); • }
Picture Box • PictureBox1.Image = Image.Fromfile(“file path.jpg”); Or • Bitmap bmp = new Bitmap(“ d:\jan\myimages \tiger.bmp”); • PictureBox1.Image=bmp;
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; }
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
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"); }
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); } }