0% found this document useful (0 votes)
30 views40 pages

Lecture 5 V2

The document provides an overview of ListView and ImageList components in a Windows Forms application, detailing their properties, methods, and events. It explains how to add items and images to a ListView, manage subitems, and handle mouse and keyboard events. Additionally, it includes code examples for implementing these components and events in a practical context.

Uploaded by

Ahmed Al-nasheri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views40 pages

Lecture 5 V2

The document provides an overview of ListView and ImageList components in a Windows Forms application, detailing their properties, methods, and events. It explains how to add items and images to a ListView, manage subitems, and handle mouse and keyboard events. Additionally, it includes code examples for implementing these components and events in a practical context.

Uploaded by

Ahmed Al-nasheri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Dr.

Ahmed Alnasheri
Contents
 ListView  Mouse Events
 ListView Examle  Mouse Events Example

 ImageList  Keyboard Events


 ImageList Example  Mouse Events Example

2
ListView
 Similar to ListBox
 Both display lists from which the user can select one or more items

 Difference between the two classes is that a ListView can


display icons next to the list items
 Allows you to define the images used as icons for ListView items

 To display images, an ImageList component is required

3
ListViews
 When you first add a ListView on the form, it will look
like a listbox.

 In ListViews, you can add items from the list below, or


from items property. Also, you can add columns that
represent subitems
in a ListView.

4
ListViews – Adding Items
 To add items in design
time, the following
window will appear.
 Note: in this window,
each item is added as an
object which has its own
property sheet.
Properties like “text”,
“tag”, “checked” can be
modified on the item
level.

5
ListViews - View
 View: is a property that specifies the way items are
displayed in the ListView.
 LargeIcon
 SmallIcon
 Details
 List
 Tile
 These views are similar to the views of items in an
ordinary Windows opened folder.

6
ListViews - View
 These views will look much different when you
associate imagelist to the listview, and associate each
item with a certain image.

7
ImageList
 An imagelist is a component that can be added to your
project, where you can add different images that can be
used as icons along with ListView items, or TreeView
nodes, that will be discussed later (next Lecture)
 When an imagelist is added, it appears at a tray under the
form.
 In an imagelist, you can
Specify the size of images
Included in the list.
• From Choose images, you can select the images you want.

8
ImageList
 Each image will have its own index, since Images is a
collection.

9
ListView Vs. ImageList
 To associate an ImageList to a
ListView, use the properties:
“Small ImageList” and “Large
ImageList”.

 Small ImageList: display the


images in it besides the items
when using the List and
SmallIcon Views.

 Large ImageList: displays the


images in it besides the items
when using the Tile and
LargeIcon Views.

10
ListView Vs. ImageList
 The last step you need to do is to associate each item in
the ListView with a certain image in the ImageList,
using the ImageIndex property.

11
ListView Vs. ImageList
 A ListView after associating it with an ImageList.

12
ListView – Details - Columns
 To use the Details view in a ListView, we have to add
columns first.

13
ListView - Subitems
 To fill the details, you have to fill subitems for each item in the ListView.
 Go to the Item property sheet, from there add subitems (which is also a
collection).
 Note: Add subitems for each item equal to the number of columns in the
listview, or they won’t be displayed.

14
ListView – Other Properties
 Multiselect: Specifies wither you can select more than
one item in a listview or not (boolean, default: true).
 SelectedItems: an array of the items selected in the
ListView.
 Note: There is no SelectedItem property for a
ListView, but you can get the selected item in a certain
way that will be discussed later.
 listView1.Items.Count: Number of items in the
listview.

15
ListView - Events
 SelectedIndexChanged (The Default Event)

 ItemSelectedChanged (Can be created from the Events


Window)
 This event has a special argument class

 e parameter: retrieves the following properties:


 e.IsSelected: boolean property, that specifies wither the current item
is selected or not.
 e.Item: gets the selected item.
 e.ItemIndex: gets the index of the selected item.

16
ListView - Methods
 listView1.Items.Add (…): used to add a new item to
the listview.
 listView1.Items[0].SubItems.Add(…): used to add a
subitem to the first item in the listview.
 e.Item.SubItems.Add(…): used to add a subitem to
the selected item.
 listView1.Items.Clear(): Clears all items and
subitems in the listview.
 listView1.Clear(): Clears all components in the
listview, even the columns.
17
ListView Example

18
ListView Example
1 // Fig. 14.31: ListViewTestForm.cs
2 // Displaying directories and their contents in ListView.
3 using System;
4 using System.Drawing;
5 using System.Windows.Forms;
6 using System.IO;
7
8 // Form contains a ListView which displays
9 // folders and files in a directory
10 public partial class ListViewTestForm : Form
11 {
12 // store current directory
13 string currentDirectory = Directory.GetCurrentDirectory();
14
15 // default constructor
16 public ListViewTestForm()
17 {
18 InitializeComponent();
21 // browse directory user clicked or go up one level
22 private void browserListView_Click( object sender, EventArgs e )
23 {
24 // ensure an item is selected
25 if ( browserListView.SelectedItems.Count != 0 )
26 {
27 // if first item selected, go up one level
28 if ( browserListView.Items[ 0 ].Selected )
29 {
30 // create DirectoryInfo object for directory
31 DirectoryInfo directoryObject =
32 new DirectoryInfo( currentDirectory );
33
34 // if directory has parent, load it
35 if ( directoryObject.Parent != null )
36 LoadFilesInDirectory( directoryObject.Parent.FullName );
37 } // end if
19
ListView Example
38
39 // selected directory or file
40 else
41 {
42 // directory or file chosen
43 string chosen = browserListView.SelectedItems[ 0 ].Text;
44
45 // if item selected is directory, load selected directory
46 if ( Directory.Exists( currentDirectory + @"\" + chosen ) )
47 {
48 // if currently in C:\, do not need '\'; otherwise we do
49 if ( currentDirectory == @"C:\" )
50 LoadFilesInDirectory( currentDirectory + chosen );
51 else
52 LoadFilesInDirectory(
53 currentDirectory + @"\" + chosen );
54 } // end if
55 } // end else
56
57 // update displayLabel
58 displayLabel.Text = currentDirectory;
59 } // end if
60 } // end method browserListView_Click
20
ListView Example
61
62 // display files/subdirectories of current directory
63 public void LoadFilesInDirectory( string currentDirectoryValue )
64 {
65 // load directory information and display
66 try
67 {
68 // clear ListView and set first item
69 browserListView.Items.Clear();
70 browserListView.Items.Add( "Go Up One Level" );
71
72 // update current directory
73 currentDirectory = currentDirectoryValue;
74 DirectoryInfo newCurrentDirectory =
75 new DirectoryInfo( currentDirectory );
76
77 // put files and directories into arrays
78 DirectoryInfo[] directoryArray =
79 newCurrentDirectory.GetDirectories();
80 FileInfo[] fileArray = newCurrentDirectory.GetFiles();
81
82 // add directory names to ListView
83 foreach ( DirectoryInfo dir in directoryArray )
84 {
85 // add directory to ListView
86 ListViewItem newDirectoryItem =
87 browserListView.Items.Add( dir.Name );
88
89 newDirectoryItem.ImageIndex = 0; // set directory image
90 } // end foreach
21
ListView Example
91
92 // add file names to ListView
93 foreach ( FileInfo file in fileArray )
94 {
95 // add file to ListView
96 ListViewItem newFileItem =
97 browserListView.Items.Add( file.Name );
98
99 newFileItem.ImageIndex = 1; // set file image
100 } // end foreach
101 } // end try
102
103 // access denied
104 catch ( UnauthorizedAccessException )
105 {
106 MessageBox.Show( "Warning: Some fields may not be " +
107 "visible due to permission settings",
108 "Attention", 0, MessageBoxIcon.Warning );
109 } // end catch
110 } // end method LoadFilesInDirectory

22
ListView Example
111
112 // handle load event when Form displayed for first time
113 private void ListViewTestForm_Load( object sender, EventArgs e )
114 {
115 // set Image list
116 Image folderImage = Image.FromFile(
117 currentDirectory + @"\images\folder.bmp" );
118
119 Image fileImage = Image.FromFile(
120 currentDirectory + @"\images\file.bmp" );
121
122 fileFolder.Images.Add( folderImage );
123 fileFolder.Images.Add( fileImage );
124
125 // load current directory into browserListView
126 LoadFilesInDirectory( currentDirectory );
127 displayLabel.Text = currentDirectory;
128 } // end method ListViewTestForm_Load
129 } // end class ListViewTestForm

23
Software Engineering Observation
 When designing applications that run for long periods
of time, you might choose a large initial delay to
improve performance throughout the rest of the
program. However, in applications that run for only
short periods of time, developers often prefer fast initial
loading times and small delays after each action.

24
Mouse Events
 Mouse Events: Are those events that associated to
mouse actions. And can be related to the form as a
whole, or to a certain control derives from class
System.Windows.Forms.Control.
 Class MouseEventArgs
 Contains information related to the mouse event
 Information about the event is passed to the event-
handling method through an object of this class
 The delegate used to create the mouse-event handlers
is MouseEventHandler

25
Mouse Events
 MouseMove: occurs when the mouse cursor moves over a form or a
certain control.
 MouseClick: occurs when the user clicks on the form or on certain
control with the mouse button (any button click will cause this event).
 MouseEnter: occurs when the mouse cursor enters in the borders of a
form or a certain control.
 MouseLeave: occurs when the mouse cursor leaves the area of a form
or a certain control.
 MouseDown: occurs when the user presses over a mouse button, and
keeps pressing. (any button).
 MouseUp: occurs when the user releases the mouse button. (any
button).
 MouseHover: occurs when the mouse cursor hovers over a form or a
certain control.

26
Mouse Events Arguments
 According to the mouse event handled, certain events
arguments are used.
 One of two event arguments classes would appear in
the header of the Mouse Event Handler.
 MouseEventArgs: This event arguments class will be
used in the events (MouseClick, MouseMove,
MouseDown and MouseUp), and it contains special
properties of the mouse.
 EventArgs: The ordinary event arguments class, that is
usually associated with any event handler. It doesn’t
contain any special properties of the mouse.

27
MouseEventArgs
 This class will appear in the header of the mouse
events: MouseClick, MouseMove, MouseDown and
MouseUp.

 The argument “e”, which is of class “MouseEventArgs”


will contain the following mouse properties:

28
MouseEventArgs
 X: of type int; and returns the X coordinate of the
mouse cursor over a form or a certain control.
 Y: of type int; and returns the Y coordinate of the
mouse cursor over a form or a certain control.
 Button: of type MouseButtons; which is a
enumeration that has the values (Left, Right and
Middle), which indicate which mouse button was
clicked and caused the event.

29
Mouse Event Example

30
MouseEventArgs

31
Keyboard Events
 Keyboard Events: Are events associated with the
keyboard keys. Whenever a user presses on any key on
the keyboard, these events take place.

32
Keyboard Events
 KeyPress: occurs when the user presses on a certain
key in the keyboard.

 KeyDown: occurs when the user presses on a certain


key in the keyboard and keeps pressing.

 KeyUp: occurs directly when the user releases the key


in the keyboard.

33
Keyboard Events Arguments
 According to the keyboard event handled, certain
events arguments are used.
 One of two event arguments classes would appear in
the header of the keyboard Event Handler.
 KeyPressEventArgs: This event arguments class will be
used in the event KeyPress, and it contains special
properties of the key.
 KeyEventArgs: This event arguments class will be used
in the event KeyDown and KeyUp, and it contains
special properties of the key.

34
KeyPressEventArgs
 The main property retrieved by this class is: KeyChar.

 KeyChar: of type char; it retrieves the actual character printed by the


key pressed. (only printable characters, non-printable characters won’t
retrieve data in this property).

35
KeyEventArgs
 Exists in both KeyDown and KeyUp event handlers.

 Properties enclosed in this class:


 KeyCode: of type Keys; which is an enumeration that contains all
possible keys on the keyboard (printable and non-printable).
 KeyData: of type Keys; but it differs that KeyCode. KeyCode will
return the last pressed key, while KeyData will return all keys
pressed at the same time.
 KeyValue: of type int; returns an integer value that represents the
key on the keyboard.
 Alt: of type bool; returns true if Alt key is pressed, false if not.
 Shift: of type bool; returns ture if Shift key is pressed, false if not.
 Control: of type bool; returns true if Ctrl key is pressed, false if not.

36
Keyboard Event Example

37
Homework 4

38
Homework 4

39
The End
40

You might also like