Array Lists and Listbox Controls Array Lists adding
Array. Lists and Listbox Controls
Array. Lists. . adding and removing items Contains(), Index. Of() and Sort() methods Iteration. . . the foreach loop Using the List. Box and Combo. Box GUI objects Typical Properties, Methods, and Events Using the Items collection property add, insert, removeat, clear Examples Working with multiple selected items
Array. Lists Arrays are not the only data structure availabe in C# – others include Array. Lists, Queues, and Stacks Array. Lists are a type of Collection, widely used throughout. NET Array. Lists are one of the most flexible C# data structures You create an Array. List and then Add and Remove items. It maintains a Count of the items for you… They reside in the Systems. Collections namespace. . .
Stacks – not covered here, but operate just like a stack of plates. You add items to the top, and remove items off from the top (first in – last out) Queues – not covered here, but operate just like a typical queue. You add items to the end, and remove items off from the start (first in - first out)
Array. Lists Fundamentally, an Array. List contains a simple list of values (although technically, they store elements of type object) They have in-built methods so we can very easily add to, insert, and delete items stored in this list They are more flexible than a 1 D Array because we can add items without specifying any size information – they simply grow (and shrink) dynamically as the items are added (or removed) Elements in an Array. List are often accessed using a foreach loop
Array. Lists – key methods and properties the Add method is almost always used with an Array. List it appends a new element to the end of the Array. List you can keep adding elements until the memory runs out the Count property (type int) keeps a track of how many items are currently in your Array. List some Methods: Add Insert Remove. At Remove. Range Clear Contains Properties: Count Item[index]
Creating an Array. List. . . Array. List arr = new Array. List(); Adding elements. . . arr. Add(“Sunday”); arr. Add(“Monday”); . . . count=1. . . count=2 Because its data type is object, we can add strings, integers, floats, etc. You can mix data types within an Array. List, but best not to. . . arr. Add(25); arr. Add(48. 6 F); . . . count=3. . . count=4
more Adding elements. . . the Insert() method adds an element at a specific position arr. Add(“Monday”); arr. Add(“Tuesday”); arr. Add(“Thursday”); arr. Add(“Friday”); . . . count=4 like Arrays, an Array. List is zero-indexed arr. Insert(2, “Wednesday”); (0) Monday (1) Tuesday (2) Thursday (3) Friday (0) Monday (1) Tuesday (2) Wednesday (3) Thursday (4) Friday
Removing elements. . . Method Remove() removes the first occurrence of a specified object arr. Remove(“Tuesday”); (0) Monday (1) Tuesday (2) Wednesday (3) Thursday (4) Friday (5) Tuesday (0) Monday (1) Wednesday (2) Thursday (3) Friday (4) Tuesday Method Remote. At() removes an element at a specific index position. . . arr. Remove. At(3); (0) Monday (1) Wednesday (2) Thursday (3) Friday (4) Tuesday (0) Monday (1) Wednesday (2) Thursday (3) Tuesday
the Clear method. . . simply removes all the elements in the Array. List. . . arr. Clear(); . . . count=0 the Contains method. . . returns a bool indicating if a specific object is in the Array. List. . . arr. Add(“Sunday”); arr. Add(“Monday”); arr. Add(“Tuesday”); if ( arr. Contains(“Monday”) ); . . . gives true if ( arr. Contains(58. 2 F) ); . . . gives false
the Index. Of method. . . searches for the specified object, returning the index of the first occurrence in the Array. List, or the value -1 if not present. . . int position; position = arr. Index. Of(“Wednesday”); position has the value 2 position = arr. Index. Of(“Saturday”); (0) Monday (1) Tuesday (2) Wednesday (3) Thursday (4) Friday (5) Wednesday position has the value -1
sorting Array. Lists. . . like Arrays, the Array. List has useful sort methods built-in. . . arr. Sort(); arr. Reverse(); (0) Monday (1) Tuesday (2) Wednesday (3) Thursday (4) Friday (5) Saturday (0) Friday (1) Monday (2) Saturday (3) Thursday (4) Tuesday (5) Wednesday (0) Monday (1) Tuesday (2) Wednesday (3) Thursday (4) Friday (5) Saturday (0) Saturday (1) Friday (2) Thursday (3) Wednesday (4) Tuesday (5) Monday
Array. Lists An example: building a list of file extensions in a folder Array. List exts = new Array. List(); string[ ] names = Directory. Get. Files (FBD 1. Selected. Path, "*. *"); foreach (string name in names) { if (!exts. Contains(Path. Get. Extension(name). To. Upper)) { exts. Add(Path. Get. Extension(name)); } }
the for and foreach loops so far, to access the elements of an array, we’ve used a for loop, something like this… string [] arr. Strings = new string[5]; … for (int i=0; i != (arr. Strings. Length); i++) { text. Box 1. Append. Text(arr. Strings[i]); } the foreach loop may also be used to access all the elements of an array (and it is particularly associated with Array. Lists). . .
for example. . . arr. Strings = new string[5]; foreach (string an. Element in arr. Strings ) { text. Box 1. Append. Text(an. Element); } keywords are in bold string an. Element : creates a variable of correct type to hold array elements in array. Strings : specifies the array/arraylist from which to access elements the loop then works its way through every element in the array, each time placing an elements value into the named variable holder in this loop you’ve no way of knowing what index position you are specifically at – but this doesn’t matter if you just wish to process every element in turn
foreach (type elementholder in Arraylist. Name) { } they are often used with Array. Lists, e. g. . Array. List names = new Array. List(); names. Add("John Doe"); names. Add("Jane Doe"); names. Add("Someone Else"); foreach (string a. Name in names ) { text. Box 1. Append. Text(a. Name + Environment. Newline); }
some more GUI controls this is a convenient point to introduce some more GUI controls… – List. Box control – Combo. Box control both are a common feature of application interfaces allowing an alternative mechanism for users to select options (and in the latter case, also to supply input information) both have strong links to the Array. List, through their Items property – which is of type Collection
List Controls list. Box displays a list from which the user can select one or more items. It is useful for preventing a user from entering invalid choices (compare it to a text box, for instance) if the total number of items exceeds the number that can be displayed, a scroll bar is shown automatically list. Box items are ‘static’: they can be added at design time, or via code, but not during program execution by the user themselves
List Controls combo. Box is a combination of a text box and a list box. it uses less space than a list box and has two parts. . . the top part acts just like a text box; so users can type in anything they wish. Thus it is not static like a list box since a choice not in the list can be typed in directly by the user the bottom part acts like a list box. A drop-down list of items lets the user select one, or possibly more, items which is then displayed in the top section
the naming convention is lst and cbo listbox occupies a user specified amount of space on the form and is populated with a list of items combo tends to takes up less space. As well as selecting items from the dropdown list, the user can also type a new item directly
list. Box / combo. Box Useful properties many are familiar… others are more specialised… Name Backcolour Enabled Size Visible Selection. Mode Selected. Item Sorted
Useful properties Sorted (type bool) determines if the list items are sorted for you Selection. Mode (type bool) determines if multiple selections allowed Selected. Index (type int) returns a value indicating the index position of the selected item (lists are zero-indexed) or sets control to have specified item selected Selected. Item returns item itself (usually a string value, but strictly speaking it is of type object so anything is possible)
Methods As always there are many methods associated with these GUI objects. For example… Clear. Selected() Find. String() Focus() Others are associated with the Items property of the object, which is itself a Collections object… Add() Remove() etc
Events The default event is the Selected. Index. Changed event. Which is raised whenever the user changes the currently selected item in the combo. Box or list. Box
The Items property these controls store multiple items of information, which are handled via the Items property (a Collection object) most manipulation of the list. Box / combo. Box contents is done via the Items collection. This has familiar Collection-like properties and methods (just like the Array. List that we saw earlier) Add() Insert() Remove. At() Clear() Count
The Items property listbox 1. Selected. Item …returns “David Beckham” listbox 1. Selected. Index …returns the value 1 listbox 1. Items. Count …returns the value 6 listbox. Items[3] …returns “Kate Moss”
Adding Items can be added in several ways. Firstly, at Design Time by using the Items Property
Adding Items Or via code while program is running, using the Add() method of the Items Collection… list. Box 1. Items. Add("Tokyo"); list. Box 1. Items. Add("London"); Or via the Insert() method… list. Box 1. Items. Insert(0, "Copenhagen"); ilst. Box 1. Items. Insert(listbox 1. Items. Count – 1 , “Paris");
Removing Items Use any of the following methods of the Items Collection… list. Box 1. Items. Remove("Tokyo"); list. Box 1. Items. Remove. At(0); listbox 1. Items. Remove(listbox 1. Selected. Item); list. Box 1. Items. Clear();
Working with multiple selections One of the trickier aspects of using combo. Box and list. Box is when you set Selection. Mode to be Multi. Simple / Multi. Extended You may then select multiple entries in the Items collection When multiple selections are made, additional Collections are created to help you to work with the selected subset… Selected. Items and Selected. Indices …these track which entries in Items are currently selected
listbox 1. Items. Count = 6 Items collection [0] = Andrew Flintoff [1] = David Beckham [2] = Freddy Mercury [3] = Kate Moss [4] = Posh Spice [5] = Tony Blair listbox 1. Selected. Indices. Count = 3 Selected. Indicies collection [0] = 0 [1] =1 [2] = 4 listbox 1. Selected. Items. Count = 3 Selected. Items collection [0] = Andrew Flintoff [1] = David Beckham [2] = Posh Spice
Deleting multiple selections Potentially tricky, because the collections are dynamically updated as items are removed Selected. Items. Count=3 Selected. Indices 0: 1 1: 3 2: 4 the temptation is to code up something like this. . . but it doesn’t work as you might expect. . .
Selected. Items. Count=3 i = 0 Selected. Indices 0: 1 1: 3 2: 4 So the list. Box item with Index value 1 (which is “b”) is deleted …which is fine so far
Now the trouble begins. With “b” removed everything below it in the listbox shuffles up one place. Which means that the two remaining selected items also shuffle up. Selected. Items. Count=2 i = 1 Selected. Indices 0: 2 1: 3 Unfortunately, our loop index is increased by one and so we attempt to delete “e”, having missed out “d” altogether They are now at positions 2 & 3 rather than at 3 & 4 The Selected. Items collection dynamically updates too – it has only two entries, correctly indicating that the selected items are now at positions 2 & 3
Finally, it all falls apart. With “e” removed everything below it in the listbox shuffles up one place. But we still have “d” selected above it at index position 3. Selected. Items. Count=1 Selected. Indices 0: 2 i = 2 Our loop index increases to value 2, which is not less than the Selected. Items. Count, so the loop now ends, leaving us with The Selected. Items collection dynamically updates – it has only one entry now, correctly indicating there is still a selected item at position 2
the trick is either to delete items ‘in reverse’ order… for (int i = list. Box 1. Selected. Items. Count - 1; i >= 0; i--) { list. Box 1. Items. Remove. At(list. Box 1. Selected. Indices[i]); } or delete the 1 st selected item as many times as the selecteditem count… for (int i = 0; i < list. Box 1. Selected. Indices. Count; i++) { list. Box 1. Items. Remove. At(list. Box 1. Selected. Indices[0]); }
a while loop offers the most elegant coding… while (list. Box 1. Selected. Items. Count > 0) { list. Box 1. Items. Remove. At(list. Box 1. Selected. Indices[0]); } Selected. Indices 0: 1 1: 3 2: 4 Selected. Indices 0: 2 1: 3 Selected. Indices 0: 2 Selected. Items. Count=3 Selected. Items. Count=2 Selected. Items. Count=1 Remove Selected. Index(0) = Item(1) Remove Selected. Index(0) = Item(2)
- Slides: 45