unit 4 visual basic controls

Upload: lalitha

Post on 09-Apr-2018

237 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 Unit 4 VISUAL BASIC CONTROLS

    1/10

    UNIT 4 VISUAL BASIC CONTRO LSStructure4.0 Introduction4.1 Objectives4.2 What iq a Control?4.3 What is a Custom Control?4.4 Controls in a Fnrrn

    . 1'!cf11rc Pnk3.4.2 Lahei Controls4.4.3 'Text Control4.4.4 Command Button4.4.5 Shape

    4.5 Summary

    4.0 INTRODUCTIONIn the previous three chapters. you have been exposed to the basics of Visual Basic. You are nowfamiliar with various type< nf variahles and functions. In this chapter, you will learn about theVisual Raqic controls and thc custom controls, which can be used in the forms. This controls makethe environment very user friendl!.

    4.1 OBJECTIVESAfter completion of this unit. you will be able to:

    Discuss various controls.Add the controls on the form and can change their properties.

    4.2 WHAT IS A CONTROL?A control is a speiial t pc of oh.ject that one draws on a Form to enable user interaction with anapplication. In Visual Basic. most of the objects appear inside forms. All objects that appear insidea form are callcd controls. Vcni15and menu items are all controls. One can add menu controls usingthe Menu Editor. The tool< which are also controls appear in the Toolbox and can be placed on aform by douhle-clicking or by click-drag operation onto a form.('ontrols muit l la \ c name\. Controls ;lccepf user input or display output. Controls have propertiesthat deline aspect

  • 8/8/2019 Unit 4 VISUAL BASIC CONTROLS

    2/10

    Controls can respond to events initiated by the user. They also respond to the events triggered by Visual Basicthe system. For example, if one wants that whenever the command button is selected it should Controlsperform the action desired and should display the result. For this you can write code in aCommandButton control's Click event procedure that would load a file or perform a calculation and thendisplay the result.In addition to properties and events. one can use methods to manipulate controls from within thecode. For example. one can use the Move method with some controls to change their location andsize. Each control has its own set of properties. events. and methods.Graphical controls include the Image. Label. Line. and Shape controls. A &aphical control usesfewer system resources and has different drawinp and display characteristics than other controls.

    4.3 WHAT IS A CUSTOM CONTROL?The custom control is a program that someone has writtell which can hc included in the VisualBasic program. The two types of custom controls that Visual Basic can use are VBX (Visual Basiccustom extension controls) and OCX (OLE Custom extension controls).A file with a .VBX or.OCX extension or an insertahle object that. when added to a pro-ject using the custom controlsdialog box. extends the Toolbox.The custom controls are prosrams. They are likc!! to have thc hu;s in them. This might create aproblem since the source code of these controlsis not available with you and without that it is notpossihle to remove the bug.To load the custom controls

    I Jse he Custom Controls dialog bos to load custom controls and insertable objects to your prosect'sToolbox.To open the Custom Controls dialog hox. chooseC,'uctom Controls from the TooLy menu. Thisoperation can also he performed by pressins CtrlLT.

    I'hc dialog hos displa?\ :IT av;li:ai-ic custom coiltrois an6 I?~scrta'nic ijects . You wil! also find the

  • 8/8/2019 Unit 4 VISUAL BASIC CONTROLS

    3/10

    Components ofWindowsProgramming& Visual Basic

    box appearing on the left of the list of controls. If the insertable objects or controls are notappearing completely, you can click at the respective boxes and then the complete list shall appear.

    One can select either or both of the following options.~AsertableObjects option displays insertable objects, such as a Microsoft Excel Chart.Controls option displays controls with .OCX and .VBX filename extensions.Selected Items Only option displays only those items in the Available Controls which you havebeen selected to include in the project.

    If one wants to load a custom control each time Visual Basic is loaded, follow the followingprocedure.

    TO add a control or insertable object to the Toolbox, click on the check box next to its name.You will find 'X ' mark appearing in the box.To remove a control or insertable object from the project, click the check box next to its name.Clicking this time shall deselect the control from the list available and 'X' mark shall disappearfrom the box.When you are finished making selections, Click on OK button to update your Toolbox.

  • 8/8/2019 Unit 4 VISUAL BASIC CONTROLS

    4/10

    4.4 CONTROLS IN A FORM4.4.1 Picture Box

    To display a picture on the form, you have to have a Picture Box, A PictureBox control can displaya graphic from a bitmap, icon, or metafile. It clips the graphic if the control is not large enough todisplay the entire image.

    ITo use the PictureBox control, Click on this Picture Box button in the Toolbox and draw it on theI form.

    Click on the Picture property button of the PictureBox to open picture dialog box.

    Click here

    'You will be displayed the files in the Load Picture box which can be loaded. Select a picture file ofyour choice and click on the Open button.

    Visual BasicControls

  • 8/8/2019 Unit 4 VISUAL BASIC CONTROLS

    5/10

    Components ofWindowsProgramming& Visual Basic

    No w you ca

    A Label control is a graphical control that one can us e to disp lay text that a user can not changedirectly.Sele ct Label contr ol from the Toolbox and draw it in the for m.

  • 8/8/2019 Unit 4 VISUAL BASIC CONTROLS

    6/10

    1Labell Label

    You can also chan ge the font and size of the label. Click on the Font property button to display Fontdialog box.

    Visunl RnsicControls

    By default, the label nam e is given as Label 1 (label one), but one can change the name a s desiredusing the Caption property.

    Click here

    Select the Font an d size from the rcspe ctive list boxcs and click on the OK button.

  • 8/8/2019 Unit 4 VISUAL BASIC CONTROLS

    7/10

    4.4.3 Text ControlW i n d mProgramming& Visual Basic

    A TextBox control, sometimes called an edit control, displays information entered at design time,entered by the user, or assigned to the control in code at run time.Select TextBox control from the Toolbox and draw it on the form.

    To display multiple lines in a TextBox control, the MultiLine property of the text box should bechanged to True.

    If a multiple-line TextBox does not have a horizontal scroll bar, text wraps automatically evenwhen the TextBox is resized. To customize the scroll bar, click on the ScrolBar option of theTextBox and change the property.

    You can also change the font and size of the text from the font dialog box. Click on the Fontproperty button to open font dialog box.

    Click hereSelect the Font and size from the respective list boxes and click on the OK button.

  • 8/8/2019 Unit 4 VISUAL BASIC CONTROLS

    8/10

    VIsuPl BasicControls

    4.4.4 Command B utton

    Use a CommandButton control to begin, interrupt, or end a process. When chosen, aCommandButton appears pushed in and so is sometimes called a push button.Select CommandButton from the Toolbox and draw it on the form.

    I By default its caption is Commandl, but you can change it to EXIT in the Caption property.

    And now you can write code for this button to close the application. Double click on the button todisplay the code window and write End command.

  • 8/8/2019 Unit 4 VISUAL BASIC CONTROLS

    9/10

    Compocpvts ofWindsws1'1 g ~n~ming& Visual Basic

  • 8/8/2019 Unit 4 VISUAL BASIC CONTROLS

    10/10

    Check Your Progress Visual BasicControlsOn your computer, start the visual basic software. Once the Visual Basic is loaded, perform thefollowing operations:I. Place a Picture control on the form. Change its location.

    2. Load any picture on Picture control. Set its properties as per your choice.

    3 . Place one or two Label5 and Text boxes on the form.

    4. Change label caption.i 5. Change label size.

    6. Place a command button on the form.

    7. Write code, for the command button - whenever click on it, Proiect must be close.

    4.5 SUMMARYA ~ontrols an objzct that enables users to interact with the application. These are the building

    I blocks of an application. Each control has a unique set of properties, which determine itsappearance and behavior.

    A control has a name property that distinguishes it from other objects in a project. There are otherproperties as well which can be changed.The custom control is a program that someone has written which can be included in the VisualBasic program. The two types of custom controls that Visual Basic can use are VBX (Visual Basiccustom extension controls) and OCX (OLE Custom extension controls).A custom control is a filewith a .VBX or .OCX filename extension or an insertable object that can be added to a project usingthe custom controls dialog box to extend the 'Toolbox. The desired selection can be made byclicking the box to the left of the list, which shall mark 'X' in the box. The deselection shall removethe 'X' mark.