Mouse Events
Dec 30, 2015
Mouse Driven Events
• Unlike control_click() which is passed no arguments (unless it's an index for an array), MouseDown, MouseUp and MouseMove events have much more information available– Button as integer - which mouse button(s) were pressed
– Shift as integer - which modifier keys were pressed
– X and Y as single - the location of the mouse pointer in the coordinate system of the object receiving the mouse event
Mouse Driven Events (2)
• Button– Status of the mouse buttons
• 1 - Left Button
• 2 - Right Button
• 4 - Middle Button
– Multiple buttons are treated as multiple events for MouseUp and MouseDown
Mouse Driven Events (3)
• Shift– Keys (Shift, Control and Alt)
• 1 - Shift Key
• 2 - CTRL Key
• 4 - ALT Key
– Any or all of the modifier keys can be set
Mouse Driven Events (4)
• For the Button or Shift arguments, bitwise logical testing must be performed to determine state. Examples– (shift AND 3) = 3 tests for Shift and CTRL
keys pressed– (Button AND 2) = 2 tests Right button only– (Button AND 3) = 2 tests for Right button only
Mouse Positioning Example #1 • Create a form with a textbox and code:
Private Sub Form_MouseUp(...)moveit = Falseend sub
Private Sub Form_MouseDown(...)moveit = TrueCall Form_Mousemove(Button, Shift, X, Y)End Sub
Private Sub Form_MouseMove(…)If moveit Then Text1.Move X, YEnd Sub
Mouse Positioning Example #2
• Etch-a-sketchPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer,
X As Single, Y As Single)
Line -(X, Y)
End Sub
Mouse Positioning Example #3• Improved Etch-a-sketch
dim moveit
Private Sub Form_Mousemove(…, X, Y)If moveit Then Line -(X, Y) : rem circle (x,y), 50 End Sub
Private Sub Form_MouseUp(…, X, Y)Form1.CurrentX = X Form1.CurrentY = YCall Form_Mousemove(Button, Shift, X, Y)moveit = Not (moveit)End Sub
Mouse Click, Up, Down
• What occurs in what sequence?– Mouse Down is first– Mouse up is second– mouse click is third– mouse up always follows a double click– mouse move always follows a mouse click
Three types of list controls
• List controls– Drop-down list– Combo-Box
• The last set is “initialized” with the contents of the appropriate drive directory
• These two must be initialized via code
List Box Control• Allows the user to select from a specified set of options (but he cannot
enter an unlisted value)
• Presented as a single window with the items listed sequentially
• Selections must be initialized via code (often as part of form_load)
• The columns property permits display of multiple columns of data in a snaking fashion (horizontal scroll as well as vertical scroll)
• The multiSelect property allows selecting multiple items which are placed in the list property (an array ) and identified by the selected property (an array)– selected(i) = true indicates that the related element of list(i) has been
selected
AddItem Method
• Used to add items to a list box
• Syntax– control.AddItem item– item is the expression you want displayed, and
it can be either numeric or string– if item is a literal string constant, then it must
be enclosed in quotation marks
Arrangement of List Box Items
• By use, with the most used entries appearing first in the list
• Sorted in ascending order either alphabetically, numerically, or chronologically
Sorted Property
• Can be set to either the Boolean value True or the Boolean value False
• If set the True, the list box items will be sorted in ascending order
• If set to False, the list box items will appear in the order in which they were entered
ListIndex Property
• You can use the ListIndex property to refer to an item in the list box
• The first item in a list box has a ListIndex value of 0
• If no items are selected in the list box, the ListIndex property has a value of -1
Default Item• It is customary to select a default item in a
single-selection list box when the interface first appears– the default item should be either the most used
selection or the first selection in the list– lstName.ListIndex = 0 will select the first item
in the lstName list box
• A default selection typically is not made in a multi-selection list box
List Box Initialization Example
Dim I, start_pos, stop_pos, months, thismonth
thismonth = month(Now) ' note: month returns 1-12
months = "January February March April May June "
months = months & "July August September October November December "
start_pos = 1
For item_number = 0 To 11 ' item numbers are zero-based
stop_pos = InStr(start_pos, months, " ")
List1.AddItem Trim(Mid(months, start_pos, stop_pos - start_pos)), item_number
start_pos = stop_pos + 1
Next item_number
' note: listindex is 0-based so we have to subtract 1 to get the correct month selected
List1.ListIndex = thismonth - 1
Combo Box Control (three styles)
• Drop Down (Style 0)– Like a one-line text box with a list attached– User can select a value or enter one of his own– Options are not shown unless user displays them
• Simple Combo Box (Style 1)– Always shows the list of options to the user if you design the box
large enough– User can select a value or enter one of his own
• Drop Down List Box (Style 2)– List is not displayed until user clicks down arrow– User can only select from list of options– Typing in the text box scrolls to best fit option
Lists & Combo Boxes
• The Integralheight property – if true will force box size to display an integral number of lines
and no partial lines– if false will retain box design dimensions, but may display partial
lines
• The listindex property contains the index (0-based) of the selected item (-1 = no item selected)
• The additem method (not event) is used to add or replace an item
• The removeitem method is used to remove an item• The clear method will remove all items
Lists & Combo Boxes (2)
• The sorted property sorts the list but can be very slow when adding to large lists
• The text property contains the last selected item
• The listcount property contains the number of items, always one more than the maximum listindex
• The topindex property contains the index value for the top of the displayed portion of the list when there is a vertical scroll bar
• There are other methods available (Use Object Browser to see them)
List & Combo Box Differences
• Selected and List properties– only for List Boxes which allow multiple selections
• Change event– only for combo boxes and only when a new entry is typed in
• Dropdown event– only for dropdown combo and dropdown list
Variable Arrays• A group of variables that have the same name and
data type and are related in some way• Can have as many as 60 dimensions in Visual Basic;
the most commonly used arrays are one-dimensional and two-dimensional
• Programmers use arrays to store related data in the internal memory of the computer
• Data stored inside the computer can be both written and read much faster than data stored in a file on a disk
Variable Arrays
• A one-dimensional array is simply a row (or column) of variables; a two-dimensional array resembles a table in that it has rows and columns
• Each element in an array is identified by a subscript
• You refer to an array element by the array’s name followed by the element’s subscript
Variable Arrays (2)
• Lower-bound is zero by default unless – option base 1 is declared, in which case it is 1– it is explicitly declared using the to keyword
• dim cards(1 to 52)• dim line_numbers(100 to 1000)
• Upper element designated by a long in parentheses after the name - ex. dim cards(51) (an array of 52 items numbered 0-51)
• Arrays must usually be initialized before use using some kind of looping construct or the array statement
dim x(4)x = array(5,7,9,"this",true)
One-dimensional Array
Nebraska New Jersey New Mexico Tennessee Texas
Nebraska
New Jersey
New Mexico
Tennessee
Texas
Two-dimensional Array
Nebraska
New Jersey
New Mexico
Tennessee
Texas
Lincoln
Trenton
Santa Fe
Nashville
Austin
One-dimensional Array
– Dim arrayname(lower subscript To upper subscript) As datatype
– Public arrayname(lower subscript To upper subscript) As datatype
• lower subscript and upper subscript are numbers that define the lowest and the highest subscript in the array
– The Dim (and Public) statements create and initialize the array variables in memory
One-dimensional Array
– Dim strMonthArray(1 To 6) As String– Dim intSquareArray(1 To 5) As Integer– Dim sngNum(1 To 10) As Single– Dim udtEmp(1 To 20) As EmpStruc
– Note: It is not necessary for an array name to contain the word “Array.”
One-dimensional Array
strMonthArray(1) = “Jan”
strMonthArray(2) = “Feb”
strMonthArray(3) = “Mar”
strMonthArray(4) = “Apr”
strMonthArray(5) = “May”
strMonthArray(6) = “June”
One-dimensional Array
For intNum = 1 to 5
intSquareArray(intNum) = intNum * intNum
Next intNum
For intNum = 1 to 10
sngNum(intNum) = Val(InputBox(“Enter number”))
Next intNum
One-dimensional Array
Do While Not EOF(1)
intNum = intNum + 1
Input #1, udtEmp(intNum).Name, udtEmp(intNum).Salary
Loop
Array Initialization• Use loops to initialize
dim row as integer, col as integerstatic big_matrix(1 to 5, 1 to 10) as integerfor row = 1 to 5
for col = 1 to 10big_matrix (row,col) = row*10+col
next colnext row
Calculating the Average
Declare variables
Repeat for intNum = 1 to 20 by 1add array score to intTotal variable
End repeat for intNum
Calculate the average by dividing intTotal by 20
Display the average
Finding the Highest Value
Declare variablesAssign first array value to intHigh variableRepeat for intNum = 2 to 20 by 1
If current array value > intHigh value thenassign current array value to intHigh
End If
End repeat for intNumDisplay the highest value(stored in intHigh)
Updating an ArrayDeclare variablesPrompt user for the value to addRepeat for intNum = 2 to 20 by 1
add value to current array value
End repeat for intNumDisplay “Updated” messageOpen a fileRepeat for intNum = 1 to 20 by 1
Write array value to fileEnd repeat for intNumClose the file
Two-dimensional Array
• Dim arrayname(lower subscript To upper subscript, lower subscript To upper subscript) As datatype
• Public arrayname(lower subscript To upper subscript, lower subscript To upper subscript) As datatype
Two-dimensional Array
– Dim strNameArray(1 To 6, 1 To 3) As String
– Dim intNum(1 To 5, 1 To 10) As Integer– Dim sngSales(1 To 20, 1 To 2) As Single
– Note: It is not necessary for an array name to contain the word “Array.”