170 likes | 452 Views
Controls. General Discussion. VB Controls. Visual Basic Controls. A control is the generic name for any object placed on a form Controls may be images, text boxes, buttons, pulldown menus, active-X controls, OLE objects, etc.
E N D
Controls General Discussion
Visual Basic Controls • A control is the generic name for any object placed on a form • Controls may be images, text boxes, buttons, pulldown menus, active-X controls, OLE objects, etc. • Each control type has its own set of methods, properties and events • Each control instantiation is given a unique name and is a unique entity
Control Placement • Controls are available from the control toolbox • Click to highlight the desired control • Click and drag to place it on the form in the location and size you want • Alternatively, double-click on the control and it will be placed in the middle of the form • After it is placed, its size and position can be altered as desired (mouse or ctrl-arrow to move, shift-arrow to size)
Using the Grid • The grid is used to “snap” positions and sizes to grid increments - helps in making your form neat. • Use the grid to keep things aligned • Tools/Options/General will allow you to turn the grid on and off and to change the spacing • Controls can be locked in place with the Format/Lock option • Prevents mouse from accidentally altering, but Ctrl/Shift-Arrows can still change position/size
Copying Controls (1) • A control identical to one just created can be made by: • selecting the desired control • pressing Ctrl-C (copy) • pressing Ctrl-V (paste) • moving the new control to the desired position • Note that when you paste the copy, you will be asked if you want a control array or just another control
Copying Controls (2) • You will be asked if this is part of a control array (the choice depends on what you are trying to accomplish) • If NO, then a new control with the next higher name will be created but with the same caption/text property (a copy of text1 is text2) • If YES, then a new control with the same name will be created, but with the next higher index number (an array copy of text1 becomes text1(1) and the original is text1(0)) Note that all controls in an array share events but have individual properties
Basic Controls • Command Button • Main purpose is to make something happen • Default event is click • Text Box • Used for user data entry and simple result output • Default event is change • Label Control • A “do nothing” control for adding text to the form • Default event is click
Basic Controls (2) • Checkbox Control • Used for selecting options (e.g. setting variables which are tested by the program) • Default event is click • Multiple boxes may be checked • Option (Radio) Button Control • Used to select one (and only one) of many mutually exclusive options • Default event is click • Only one option set per form unless in a frame
Picturebox Control • Used to add a picture to a form (.bmp, .wmf, .gif, .jpg, .ico) • The Autosize property will cause the picturebox to expand/shrink to fit the picture. • If you make the picturebox smaller than the picture it will clip • Most used properties are name and picture • Default method is click • Much more functional than images
Image Control • Adds a scaled image Used to add a picture to a form (.bmp, .wmf, .gif, .jpg, .ico) • The image will be scaled to fit inside whatever box size you draw • Set the stretch property to true to permit altering the displayed control size. • Most used properties are name and picture • Default method is click • Cannot be placed on top of other controls or have focus at run time
Timer Control • Timer is visible only at design time • The enabled property enables or suspends the timer operation (unlike other controls) • The interval property determines how often the timer's timer event is triggered • Only a single event indicating timeout has occured
Common Control Properties • Enabled - allows alteration at run time • If false, control is grayed out at run time • Visible - visible to user at run time • tabindex - controls order that the tab key sequences through controls on a form at runtime
Other Controls • FileListBox Control • Shape Control • rectangle, square, oval circle, rounded square, rounded rectangle • Line Control • Data Selection control • OLE Control • Frame Control • ComboBox Control • ListBox Control • HScrollBar • VScrollBar Controls • Timer Control • DriveListBox Control • DirListBox Control
Writing Event Handlers (1) • Open the Code window for the control • double-click on the control itself or • click the code-window icon on the Project Explorer and select the control-name of interest • Select the event of interest from the pull-down selection box if the default is not what you want. • Alternatively you could type the whole thing in, but it’s too easy to make a simple naming mistake • Enter your event-handler code between the Private Sub object_event() and End Sub lines
Writing Event Handlers (2) • Use indenting (tab) to make the code easier to read (and debug) • Keep names meaningful and short • Watch out for red lines (indicating errors) • Use completions and selections rather than typing in the full whatever • Use msgbox() for simple debugging
Form Events/Methods • form_load() - invoked when the form is first loaded. Does not display the form except for the startup form and then only on completion • form_unload() - invoked when the form is exited or otherwise terminated • [object.]show - normally, a form is not displayed when it is loaded. Show overrides this.