There are three key events available in C#
1)keypress
2)keydown
3)keyup
q)what is the difference in between keypress & keydown
a)In keypress, Logic will be executed before displaying the data.
In keydown and keyup, logic will be executed after displaying the data
Keyboard character set is divided into 2 types
1)ASCII characters
2)scancode characters
ASCII characters:
ex: A…….Z
a……..z
0……9
Special chars
Printable
Key press
Scancode characters:
Ex: F1---F12
Multimediakeys
Non printable
Working with combobox and listbox controls:
1)combobox and listbox controls are 99% same
2)these controls contains a collection of items
3)every item will be identified with unique index number, always index begins from 0
4)combox allows to select only one item at a time
5)listbox allows multiple selection also optionally
Common properties for combobox and listbox controls
1)items:allows to add items at design time
2)selected item
3)selected index:in case if number of item is selected then selected index returns -1
Common methods for combobox and listbox controls
1)listbox1.items.Add(string)…..allows to add items at runtime
2) listbox1.items.insert(index,string)
3) listbox1.items.indexof(string)
4) listbox1.items.remove(string)
5) listbox1.items.removeat(index)
6)listbox1.items.clear();
7)listbox1.items.count;
8)listbox1.items.getenumarator();
Example on combobox:
A program to change the shape of the form
Open windows forms application project:
Start->programs->Microsoft visual studio 2010->Microsoft Visual studio 2010->file menu->new->
project->select visual c# from installed templates->select windows forms application project
place a combobox->properties and set
items=add 3 items as follows
circle
elipse
triangle
using system.Drawing.Drawing2D;
code for comboBox1_selectedindexchanged event(double click on combobox)
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;
using System.Drawing.Drawing2D;
namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int i = comboBox1.SelectedIndex;
GraphicsPath g = new GraphicsPath();
if (i == 0) //circle
g.AddEllipse(10, 10, 250, 250);
else if (i == 1) //ellipse
g.AddEllipse(10, 12, 200, 500);
else if (i == 2) //triangle
{
g.AddLine(10, 10, 250, 300);
g.AddLine(250, 300, 150, 152);
g.AddLine(150, 152, 10, 10);
}
Region r = new Region(g);
this.Region = r;
}
}
}
Execute the project F5