Sunteți pe pagina 1din 9

Activity #3Working on C# List View

Objectives:
After completing this unit you will be able to:
o Use Visual Studio to build simple Windows Forms applications.
o Use the Forms Designer to visually design forms.
o Create an Application using List View
Introduction:
A list box is used to display a list of strings and all items of that control are primarily strings. To go
a little further than a simple list of strings, the Microsoft Windows operating system provides the list
view control. A List View is used to display a list of items to the user but it can be configured to change
the type of display. (yevol.com)
List View provides a useful view of items with icons. This view is similar to those found in file system
managers such as Windows Explorer. By providing functionality built-in, we avoid complex layout
routines.

Using the List View


You can add ListView Controls by dragging it from the toolbox in Visual Studio into your forms. You
can also use the search box on the Tool box window for easy access.

Properties
View
There are several properties of ListView which we can access and Edit for specific needs. One of the
common is the VIEW Property. This different settings of the View property determines how the icons
in the control are displayed. You can Either select, Large Icos, Small Icon and Tile
(Image from dotnetperls.com/listview)

Items
The Items property is the most important one on the ListView. It enables you to add the data entries.
You can add to Items by double-clicking on the Items entry and adding Items one-by-one in the
ListViewItem Collection Editor. However, More useful, you can add elements dynamically through C#
code, as in the Load event handler on the enclosing form.
You can add the "default" event handler for each control by double-clicking the control in design view.

CheckBoxes
We can add CheckBox controls to every item. To do this, please set the CheckBoxes property to true.
The user will then be able to check or uncheck various items in the ListBox through the user interface.
Checked:You can set this to true or false to mutate the check programmatically, or read it to see if the
user checked the item.
(Image from dotnetperls.com/listview)

LabelEdit
Similar to Windows Explorer, the ListView allows you to change the text of items. You can do this by
setting the LabelEdit property to true. The user can then select the text and wait to get an edit cursor
in the ListView.
And:You can access this changed text programmatically through the Text property as well.
(Image from dotnetperls.com/listview)

ImageList, icons
When you are using the ListView, you will probably want to add icons to every item. To do this, you
must first create an ImageList control. You may try this by double-clicking on the control in the Toolbox
in Visual Studio to create one.

HoverSelection
The HoverSelection property allows you to specify that an item becomes selected whenever the user
hovers the mouse over it. This selection will also trigger the SelectedIndexChanged event handler.

Activation
The term "activation" refers to how an item is chosen by the user. In other words, double-clicking or
pressing Enter are ways of activating an item. With the Activation property, you can specify how the
activation event is triggered.

Alignment
Another useful property on the ListView control is the Alignment property. You specify Left here to
have all the items aligned to the left edge of the enclosed region. The values Default and Top are
equal.

Columns
The Columns property is useful only when you are using the Details view. As with other Columns
properties in Windows Forms, such as those upon the DataGridView, these items serve as a template
to drive the display of the data.

DataGridView Columns
HeaderStyle:The HeaderStyle property is useful when the View property is set to "Details". This
affects header appearance.

Groups
The Groups property in the ListView provides a way to visually separate distinct classes of items. You
can add GroupItems to the ListView and then change the items to reference specific groupings.

SelectedIndexChanged
It is possible to listen for when the selection changes. This occurs when the user clicks the mouse or
pressed another key such as up, down, left, right, or tab. Please add the SelectedIndexChanged
event handler.

Using the List View on an application


1. After adding your ListView on your Form, you can open its task view to edit its
common properties. You can do this by selecting the ListView in your form and
Clicking the small arrow icon on upper right corner of the ListView.

2. To add columns click Edit Columns on ListView task.

Here you can add as many columns as you like.

3. For this activity where going to add 3 columns, so click the Add Buttons thrice.
(You may click remove button if you dont want the column you just added).
Clicking a created column will shows its object properties.

4. Change the Text Properties of ColumnHeader1 to Name, ColumnHeader2 to


Address and ColumnHeader3 to Contact Number and click Ok. (As of now you
may not see yet the Changes you made)
5. To View the changed properties you need to change the View properties in
ListView Task and select Details.

Your form should look like this.

You can resize the width of the Column Name depending on your needs.
6. Few Basic Properties are need to configure too.
a. Enable GridLines and FullRowSelect by making its value TRUE. GridLines
literally adds Grid Lines to the ListView while FullRowSelect Highlights the entire
row as you select its entry.

Adding Items on the ListView


7. Add 3 other Buttons on the form. We will use the first button to Add Item so without
changing their properties add the following code for button 1:

ListViewItem lstInformation = new ListViewItem("Juan Dela Cruz");


lstInformation.SubItems.Add("Quiapo Manila");
lstInformation.SubItems.Add("09123456789");
listView1.Items.Add(lstInformation);

What happened here is that we made a new instance of the ListView to add new item
(ListViewItem lstInformation = new ListViewItem) and use this instance to add the
adder items on the list (listView1.Items.Add(lstInformation).
8. Run the code and Click the button 1.
Clicking the button 1 will add the items in the code in our previous step. As for now
this is not a good coding because as you click the button 1 again and again, the
same item is being added. To handle this we can add textbox for the items to be
added.
9. Getting a Value from the Selected Row. To get the value from a selected row we
will use the button 2 to invoke the code and shows the selected value in message
box. To do this add the following code in button 2.
String str;
int indexOfSelectedRow;
if (listView1.SelectedItems.Count == 0) return;
indexOfSelectedRow = listView1.FocusedItem.Index;
str = listView1.SelectedItems[indexOfSelectedRow].SubItems[0].Text;
MessageBox.Show(str, "info");

Here we declare a str variable to handle the value returned by the listView1. We also
declare a int indexOfSelectedRow to handle the index of selected row.
The if (listView1.SelectedItems.Count == 0) return; Handles error when we click
button 2 of no selected items in listView.
str = listView1.SelectedItems[indexOfSelectedRow].SubItems[0].Text; //this code
get
the
row
and
column
of
the
selected
items.
The
SelectedItems[indexOfSelectedRow] gets the rows and SubItems[0] return the column
zero. Changing the 0 to any value changes its selected columns.

10. Deleting an Items. To delete the selected row its index must be identified first then
invoke Remove funcions.
Add the following code in button3
int indexOfSelectedRow;
if (listView1.SelectedItems.Count == 0) return;
indexOfSelectedRow = listView1.FocusedItem.Index;
listView1.Items[indexOfSelectedRow].Remove();

note:
For this activity, if you use listView1.SelectedItems[indexOfSelectedRow].Remove();
in deleting the last entry in the listView (Assuming there are multiple entry) may lead to
error.

Supplemental Activity:
1. Create and design your own simple Directory of your Friends. Your Program can
ADD & DELETE Entry. The information on the Directory must contain at least 10
entries and may include but not limited to NAME, ADDRESS, CONTACT No,
BIRTHDAY, E-Mail ADDRESS & NICKNAME.
Sample Form

2. Modify the Program in #1 and Add Sorting functions and Editing Function for your
directory Listing.

S-ar putea să vă placă și