/---------------------\ | Notes for lecture11 | | 16 November 1999 | | CS 125-609 | | Michael Goldwasser | \---------------------/ ========================================================================= FOUR ADDITIONAL CONTROLS ========================================================================= Preface: Consider this a "Cliff Notes" version of Chapter 5.2 of the text. (I forget, are Cliff Notes generally longer than the original?) Please see the chapter (or VB help pages) for more details and examples. ========================================================================= FRAMES ------------------------------------------------------------------------ Use: To let the designer create a group of controls for convenience ------------------------------------------------------------------------ A Frame is a very simple rectangular-shaped control. By itself, it has relatively few events, and it appears completely invisible, except that you can optionally choose to give it a border and an optional caption. The main use of frames is actually to group other controls in a way that is convenient for developing programs. Whenever you create a control in VB, for example a command button, it is officially a "child" of some other object. By default, a control's parent is the Form. The importance of a control's parent is because some of the effects of a parent's properties are inherited by the child. For example, if the Form.Visible property gets set to false, then the form - including all of its children - become invisible. Even if the child's visible property is set to true, it will still be invisible because of the parent. The Enabled property is similarly inherited. The placement position of controls is also relative to the parent's placement. For instance if you move the location of the form on the screen, the location of all controls which are children of the form move as well. A Frame is a special control which can have children of its own. Unfortunately, existing controls cannot be forced to be children of a frame; similarly, controls which are children of a frame can never switch to be children of some other object. The parent-child relationship is set once and for all when the child object is created. By default, if you doubleclick to create an object, it is placed as a child of the form. To instead create a new control as a child of a Frame, do the following: (1) First create the frame (2) Single-Click on the control icon in the toolbox to select it (3) Move the mouse over the frame, and then drag and drop a new control ------------------------- Secret #1: If you really do want to change the parent of an existing control at design time, you can acomplish this by "cutting" the existing conrol, and then clicking inside a frame and "pasting" it. (In effect, you are actually creating a brand new control, but its properties are set to match the control which you had just deleted) ------------------------- Secret #2: Picture boxes can also have controls as children. ========================================================================= CHECK BOX ------------------------------------------------------------------------ Use: To give the user a way to independent select "Yes/No" for a choice. ------------------------------------------------------------------------ This control consists of a small square and a caption which can be placed on either side of the square. At runtime, when the user clicks on the box, it toggles between being checked and unchecked. The control has one new property: chkbox.Value which has an integer value either 0, 1 or 2, with the interpretation: 0 = unchecked 1 = checked 2 = grayed (read the text to find out about the grayed setting) ========================================================================= OPTION BUTTONS ------------------------------------------------------------------------ Use: To let the user choose exactly ONE option from a group of choices. ------------------------------------------------------------------------ Option buttons are similar to check boxes, except for three key differences (the third of which being the most important): (1) instead of being a square with or without a check mark, they appear as a circle, which or without a solid dot inside the circle. (2) the Value property is a Boolean, thus it only has two values True = the button is currently selected False = the button is not currently selected (3) All option buttons with a common parent are grouped together so that only one button can be selected at any given time. This is enforced by VB; whenever you click on one option button, all other buttons in that group are unselected. Property (3) is one of the main reasons that Frames are useful. If you want to create a new group of option buttons that are not effected by an existing group of option choices, you can create the new ones inside a new frame. ========================================================================= LIST BOX ------------------------------------------------------------------------ Use: To let the user choose exactly one text option from a list ------------------------------------------------------------------------ A list box displays a list of strings (using a scroll bar, if necessary); the user is allowed to pick *one* of these strings. When a user clicks on one of the strings, that string is highlighted (and any previously selected string is unhighlighted). The programmer chooses the strings which appear in a list box in one of two ways: * at runtime, the command lstBox.AddItem str adds the string str to the list. * at design time, by clicking on the List property, you are allowed to add one new string to the list. (and you can add many strings by repeating this process) The items are displayed either in order they were added, or in sorted order, depending on a property setting. Also, you can refer to the items based on their index into the list, where the first item is assigned index 0. (BEWARE: If items are being automatically sorted, this means that the indicies of particular items may change based on the insertion or deletion of other items) Some relevent methods and properties are: lstBox.AddItem str ' adds string str to the list lstBox.RemoveItem n ' removes the item with index n lstBox.Clear ' removes all items lstBox.Sorted ' true/false - are items displayed in sorted order lstBox.ListCount ' this returns the number of items in the list lstBox.NewIndex ' the index of the most recently added item lstBox.List(n) ' the string having index n lstBox.ListIndex ' the index of the user highlighted item (returns -1 if nothing is highlighted) lstBox.Text ' the string currently highlighted (this is READ-ONLY at runtime) lstBox.ItemData(n) ' integer of your choice associated with item n (this is often used to sidestep the issue of changing indicies in a sorted list, as the ItemData will not change) =========================================================================