Você está na página 1de 23

Controls with main Properties , Method and Events

By: Prof.Vaishali

Visual Basic

Check Box
A Check Box displays several Yes-No, True-False choices to the userfrom which one or more choices can be made. When a selection is made, a check appears in the box associated with the choice. To check whether one or more of the items are checked via Visual Basic, you need only check the item's Value. By default, the check box control is set to vbUnchecked. The following table lists the values and VB constants used to set the Value Property: Status of the Check Box Unchecked Checked Example: Private Sub Check1_Click() If Check1 = vbChecked Then Text1.FontBold = True Else Text1.FontBold = False End If End Sub
By: Prof.Vaishali

Value 0 1

VB Constant vbUnchecked vbChecked Private Sub Check1_Click() If Check1.Value = 1 Then Text1.FontBold = True Else Text1.FontBold = False End If End Sub
Visual Basic

Option Buttons (Radio Buttons) Option buttons are used to display several choices to the userfrom which one and only one choice can be selected. If the user clicks on each choice, a black dot appears in the circular area associated with that choice. Option buttons, also called radio buttons, are typically used in a group of two or more. Example: Private Sub Command1_Click() If Option1.Value = True Then Form2.BackColor = vbRed ElseIf Option2.Value = True Then Form2.BackColor = vbGreen Else Form2.BackColor = vbBlue End If End Sub

By: Prof.Vaishali

Visual Basic

The ListBox The ListBox control displays a list of choices from which the user can select one or more items. ListBoxes that permit the user to select only one choice are referred to as Single-Choice or Simple ListBoxes. ListBoxes that permit the user to select one or more choices are referred to as MultiSelect ListBoxes. Property List ListCount Description List property is used to add items in the ListBox at runtime as well as design time. This property gives you the total number of items in the list. Since the index starts at 0, in order to analyze the contents of the list using for next loop we write For I=0 to list1.listcount-1 ListIndex Sorted Style MultiSelect The value Listindex property gives the index number of the selected item. If no item is selected the value is -1. Arrange the items in the ListBox in alphabetical. Specifies the style of the ListBox appearance. The user can select more than one item from the list. i) None
By: Prof.Vaishali

ii) Simple

iii) Extenteded
Visual Basic

Methods AddItem Clear RemoveItem

Description Add an item to the ListBox. Listname.additem item(Index) Removes all items from the ListBox Listname.clear Removes the specified item from the ListBox Listname.RemoveItem index

Event

ListBox respond to 12 events. The KeyUp, Keydown, LostFocus and so on. The two important events are Click and Double-Click events.

By: Prof.Vaishali

Visual Basic

Adding items to a List Design Time : To add items to a list at design time, click on List property in the property box and then add the items. Press CTRL+ENTER. Run Time : The AddItem method is used to add items to a list at run time. Syntax Listname.AddItemitem, Index The item argument is a string that represents the text to add to the list The index argument is an integer that indicates where in the list to add the new item. Private Sub Form_Load() List1.AddItem 1 List1.AddItem 2 List1.AddItem 3 List1.AddItem 4 List1.AddItem 5 List1.AddItem 6 End Sub

By: Prof.Vaishali

Visual Basic

Removing Items from a ListBox: The RemoveItem method is used to remove an item from a list. The syntax for this is given below. Listname.RemoveItem index To remove the third item in a ListBox, you'd use the following statement-Listname.RemoveItem 2 To remove a selected item from a list: List1.RemoveItem List1.ListIndex The following code verifies that an item is selected in the list and then removes the selected item from the list. Private Sub cmdRemove_Click() If List1.ListIndex > -1 Then List1.RemoveItem List1. ListIndex End If End Sub
By: Prof.Vaishali Visual Basic

Clearing All Items from a ListBox: Listname.clear

Determining which Item Was Selected (in a SimpleListBox) Using ListIndex: MsgBox "You selected item " & Str(List1.ListIndex) NOTE: If no item is selected, a value of 1 is returned.

Determining which Item Was Selected (in a SimpleListBox) Using the ListBox's Text Property: If List1.Text = "Choice 1" Then Program Statements End If

By: Prof.Vaishali

Visual Basic

The ComboBox A ComboBox combines the features of a TextBox and a ListBox. This enables the user to select either by typing text into the ComboBox or by selecting an item from the list. There are three types of ComboBox styles... Dropdown Combo (style 0) Simple Combo (style 1) Dropdown List (style 2) The important properties of Combo Box are: Text ListIndex ListCount Sorted Locked Item Data The current selection in a Combo Box is done by using text property. The value Listindex property gives the index number of the selected item. If no item is selected the value is -1. This property gives you the total number of items in the list. Arrange the items in the ListBox in alphabetical. You can lock a Combo box by setting its Locked property to true. You can use this property to hold a long integer as this array is indexed in the same way as the controls items are indexed.

By: Prof.Vaishali

Visual Basic

Methods AddItem Clear RemoveItem

Description Add an item to the ComboBox. Comboname.additem item(Index) Removes all items from the ComboBox Comboname.clear Removes the specified item from the ComboBox Comboname.RemoveItem index

Event

The KeyUp, Keydown, LostFocus and so on. The two important events are Click and Double-Click events.

By: Prof.Vaishali

Visual Basic

Display Selected Item: Private Sub Command1_Click() MsgBox "Current Selected Item is " & " " & Combo1.Text End Sub Example of ItemData: Private Sub Command2_Click() MsgBox "as" & Str(Combo1.ItemData(Combo1.ListIndex)) End Sub Private Sub Form_Load() Combo1.AddItem "a" Combo1.AddItem "b" Combo1.AddItem "c" Combo1.ItemData(0) = 5 Combo1.ItemData(1) = 22 Combo1.ItemData(2) = 34 End Sub

By: Prof.Vaishali

Visual Basic

ScrollBar Control The ScrollBar is a commonly used control, which enables the user to select a value by positioning it at the desired location. It represents a set of values. The Min and Max property represents the minimum and maximum value. The value property of the ScrollBar represents its current value, that may be any integer between minimum and maximum values assigned. The important properties are: Min & Max: Value: SmallChange: LargeChange: Min and Max represent the valid range of values The most important run-time property is Value, which always returns the relative position of the indicator on the scroll bar. SmallChange is the variation in value you get when clicking on the scroll bar's arrows Large Change is the variation you get when you click on either side of the scroll bar indicator

By: Prof.Vaishali

Visual Basic

Example of ScrollBar Control : Private Sub Form_Load() HScroll1.Min = 1 HScroll1.Max = 100 HScroll1.LargeChange = 10 HScroll1.SmallChange = 1 HScroll1.Value = 50 End Sub

By: Prof.Vaishali

Visual Basic

Example of ScrollBar Control : Dim ht As Integer Dim wd As Integer Private Sub Form_Load() Image1.Stretch = True ht = Image1.Height wd = Image1.Width End Sub Private Sub HScroll1_Change() Image1.Width = HScroll1.Value + wd End Sub Private Sub VScroll1_Change() Image1.Height = VScroll1.Value + ht End Sub

By: Prof.Vaishali

Visual Basic

Slider Control
The Slider Control works similarly to a Scroll Bar control. It is a little box with optional tick marks that contains a slider. The best example of slider control is a volume control. The Min and Max properties determine the upper and lower limits of a Slider control, and you can set these properties at either design time or run time. TickStyle and TickFrequency Properties The Slider control consists of two parts: the thumb and the ticks. The appearance of the control depends on the TickStyle property. you can also program how many ticks appear on the control by setting the TickFrequency property. This property, in combination with the Min and Max properties, determines how many ticks will appear on the control

By: Prof.Vaishali

Visual Basic

SmallChange and LargeChange Properties The SmallChange and LargeChange properties determine how the Slider control will increment or decrement when the user clicks it. The SmallChange property specifies how many ticks the thumb will move when the user presses the left or right arrow keys. The LargeChange property specifies how many ticks the thumb will move when the user clicks the control or when the user presses the PAGEUP or PAGEDOWN keys. Selecting Ranges If the SelectRange property is set to True, the Slider control changes its appearance. Orientation Thos properties is used to make the slider horizontal or vertical. The possible values are: 0 and 1.

By: Prof.Vaishali

Visual Basic

Example of Slider Controls properties at run time. Private Sub Form_Load() Slider1.Max = 250 Slider1.Min = 0 Slider1.LargeChange = 5 Slider1.TickFrequency = 25 End Sub

Private Sub Slider1_Change() Text1.Text = "Slider position is " & Str(Slider1.Value) End Sub

By: Prof.Vaishali

Visual Basic

The Timer Control You use a timer control when you want to execute a code at specific intervals. Timer is a no resizable control. A Timer control is invisible at run time, and its purpose is to send a periodic pulse to the current application. This control exposes only two meaningful properties: Interval and Enabled. Interval stands for the number of milliseconds between subsequent pulses (Timer events), while Enabled lets you activate or deactivate events. When you place the Timer control on a form, its Interval is 0, which means no events. you can set these properties at either design time or run time. Timer Events The main event for times is a Timer event. The following example specifies use of timer event. Private Sub Form_Load() Timer1.Interval = 500 End Sub Private Sub Timer1_Timer() Label1.Caption = Time End Sub
By: Prof.Vaishali Visual Basic

The Shape Control The Shape control can display six basic shapes: Rectangle, Square, Oval, Circle, Rounded Rectangle, and Rounded Square. Syntax Shape

Shape Controls properties : Name Backcolor BackStyle BorderStyle FillColor FillStyle Shape Name of a control sets the background color of shape sets the background style of control sets the border style of control (0-Transparent, 1-Solid), sets the color used to fill in shape sets the pattern used to fill shape controls sets a value indicating the appearance of a shape control.

By: Prof.Vaishali

Visual Basic

The Picture Box The Picture Box is one of the controls that is used to handle graphics. You can load a picture at design phase by clicking on the picture item in the properties window and select the picture from the selected folder. You can also load the picture at runtime using the LoadPicture method. For example, the statement will load the picture grape.gif into the picture box. Picture1.Picture=LoadPicture ("C:\VB program\Images\grape.gif") The image in the picture box is not resizable.

The Image Box The Image Box is another control that handles images and pictures. It functions almost identically to the picture box. There is one major difference, the image in an Image Box is stretchable, which means it can be resized. This feature is not available in the Picture Box. Similar to the Picture Box, it can also use the LoadPicture method to load the picture. For example, the statement loads the picture grape.gif into the image box. Image1.Picture=LoadPicture ("C:\VB program\Images\grape.gif")
By: Prof.Vaishali Visual Basic

The Drive List Box The Drive ListBox is for displaying a list of drives available in your computer. When you place this control into the form and run the program, you will be able to select different drives from your computer.

By: Prof.Vaishali

Visual Basic

The Directory List Box The Directory List Box is for displaying the list of directories or folders in a selected drive. When you place this control into the form and run the program, you will be able to select different directories from a selected drive in your computer

The File List Box The File List Box is for displaying the list of files in a selected directory or folder. When you place this control into the form and run the program, you will be able to shown the list of files in a selected directory. You can coordinate the Drive List Box, the Directory List Box and the File List Box to search for the files you want.

By: Prof.Vaishali

Visual Basic

Example: Private Sub Dir1_Change() File1.Path = Dir1.Path End Sub Private Sub Drive1_Change() Dir1.Path = Drive1.Drive End Sub Private Sub File1_Click() Image1.Picture = LoadPicture(File1.Path & "\" & File1.FileName) End Sub

By: Prof.Vaishali

Visual Basic

Você também pode gostar