Presentation is loading. Please wait.

Presentation is loading. Please wait.

21/03/20161 2.1 Working with Controls Text and List Boxes.

Similar presentations


Presentation on theme: "21/03/20161 2.1 Working with Controls Text and List Boxes."— Presentation transcript:

1 21/03/20161 2.1 Working with Controls Text and List Boxes

2 221/03/2016 Learning Objectives: Know what the text box and list box controls are used for and how to use them. The difference between a property and a method.

3 321/03/2016 The Text Box Used for entering and displaying data The Text property is the most common property you will use.

4 421/03/2016 Methods Controls have properties Properties have methods. A property controls the appearance and behaviour of a control. A method is an action that can be done on a property.

5 521/03/2016 The Text Box Focus is the most common method you will use. It positions the cursor inside the text box. e.g. txtBox.Focus e.g. txtBox.Focus

6 621/03/2016 The List Box Used to select one or more items from a list.

7 Common List Properties PropertiesNotes SelectedItem Contains the selected item. Sorted Specifies whether the items in the list box are sorted or not. Text Contains the currently selected item from the list box. SelectedIndex Contains the index of the selected item. The first item is indexed (numbered) by Visual Basic as 0, the next as 1 and so on.

8 821/03/2016 Common List Methods MethodsNotes Add Add an item to a list box. Remove Remove an item from a list box. Clear Removes all items from a list box. Count Counts all the items in a list box.

9 921/03/2016 Program 2.1 A list box of countries Specification: Specification: Allow the user to enter names of countries in a text box. They should be able to click command buttons to display all the names alphabetically in a list box, to delete a selected country from the list box, or to delete all the countries from the list box. Allow the user to enter names of countries in a text box. They should be able to click command buttons to display all the names alphabetically in a list box, to delete a selected country from the list box, or to delete all the countries from the list box.

10 1021/03/2016 Program 2.1 A list box of countries 1.Create a new project ‘A list box of countries. 2.Change the text property of the form to ‘Using a List Box’. 3.Drag a text box, three command buttons and a list box onto the form and position them as shown.

11 1121/03/2016 Program 2.1 A list box of countries

12 ControlProperty Property Setting TextBoxNametxtCountry ButtonNameTextbutAddAdd ButtonNameTextbutDeleteDelete ButtonNameTextbutClearClear ListBoxNameSortedItemslstCountriesTrueSpainUSAAustria

13 Program 2.1 A list box of countries 1.Double click the Add button to bring up its Click event procedure code template. 2.Write in inside the procedure code template: lstCountries.Items.Add(txtCountry.Text) 3.Return to design view and double click the Delete button. 4.Write in inside the procedure code template: lstCountries.Items.Remove(lstCountries.SelectedIte m)

14 1421/03/2016 Displaying updated information while a program is running This will normally be done in a label so: lbl LabelName.Text = whatever you want to display lbl LabelName.Text = whatever you want to display If you want to display a standard text message then enclose the text in “…….”. If you want to display a count of the items in a list then lstBoxName.Items. Count

15 Program 2.1 A list box of countries 1.Return to Design View and double click the Clear button. 2.Write in inside the procedure code template: lstCountries.Items.Clear()

16 Program 2.1 A list box of countries 1.Run the program 2.Publish the program.

17 List Boxes For example: lstBoxName.Items.Add( what you want to add ) lstBoxName.Items.Add( what you want to add ) Reading this forwards means: This list box, items of, add (what is in these brackets). This list box, items of, add (what is in these brackets). However, you will that sometimes it is easier to understand VB instructions if you read them backwards. (What is in these brackets) add it to the items of this list box. (What is in these brackets) add it to the items of this list box. lstBoxName.Items.Remove( what you want to remove ) lstBoxName.Items.Remove( what you want to remove ) Similar to above, reading it backwards: (What is in these brackets) remove it from the items of this list box. (What is in these brackets) remove it from the items of this list box. lstBoxName.Items.Count lstBoxName.Items.Count Similar to above, reading it backwards: Count the number of items in this list box. Count the number of items in this list box. 1721/03/2016

18 List Boxes For example: lstBoxName.Items.Add( textBox.Text ) lstBoxName.Items.Add( textBox.Text ) Adds the contents of a text box to a list box. lstBoxName.Items.Add( lstAnotherListBox.SelectedItem ) lstBoxName.Items.Add( lstAnotherListBox.SelectedItem ) Adds the selected item in another list box to this list box. lstBoxName.Items.Remove( lstBoxName.SelectedItem ) lstBoxName.Items.Remove( lstBoxName.SelectedItem ) Removes the selected item in a list box (from itself). lstBoxName.Items.Count lstBoxName.Items.Count Counts the items in a list box. 1821/03/2016

19 1921/03/2016 Plenary What are the text box and list box controls used for? Text box controls are used for entering and displaying data. List box controls are used for displaying a list of items from which the user can select an item.

20 2021/03/2016 Plenary between a property and a method? What is the difference between a property and a method? A property controls the appearance or behaviour of a control. A property controls the appearance or behaviour of a control. A method is an action which can be done on a control and is sometimes done through a property. A method is an action which can be done on a control and is sometimes done through a property.

21 2121/03/2016 Extension “Trip” Program Build a program using two list boxes as shown:

22 2221/03/2016 Extension “Trip” Program One list box should be named ‘lstStudents’ and should list all the students in a class. The other list box should be named ‘lstTrip’ and should list those students going on a school trip. Fill (populate) lstStudents before running the program (at design time) using its Items property.

23 2321/03/2016 Extension “Trip” Program When the program runs the user can click a button to copy the selected name from lstStudents to lstTrip. A second button should let them delete a name from lstTrip and move it back to lstStudents in case they have made a mistake or the student has changed their mind. Display the number of students going on the school trip. See slides 14 & 17 for a hint on how to do this. See slides 14 & 17 for a hint on how to do this.

24 2421/03/2016 Extension “Trip” Program When running your program be careful not to click either of the two buttons (Add to trip / Delete name) if you have no name selected. If you do the program will fail. You will have to click the Debug menu and click the Stop Debugging option. Then run the program again. You don’t know enough to be able to stop this happening although you are welcome to investigate.

25 2521/03/2016 Extension “Trip” Program 1.Disable the Delete button at design time so that it is disabled when the program is first run. Enable it only after someone has been added to the trip list. Note that you cannot disable it again without covering selection but you could investigate this as you have covered it in theory but not in practice. 2.Add another label to count the students who are not going on the trip. 3.Use the SelectionMode property to allow the user to select more than one option from the list at a time.


Download ppt "21/03/20161 2.1 Working with Controls Text and List Boxes."

Similar presentations


Ads by Google