Events

Tips >> Visual Basic

MAKE WEB APPLICATION WITHOUT KNOWLEDGE OF CODING? CLICK HERE

 

An event occurs in response to an action, such as the user clicking the mouse or pressing a key in the application. An event handler is a routine that responds to the event. At the top of the code Window in Visual Basic, there are two drop-down lists.

The first list relates to the controls used in the form (including the form),
The second relates to the events that are available for that object.
The General section is used for variable declarations, procedures and functions.

The following example illustrates events by using the MouseMove event for a CommandButton. The MouseMove event occurs before the Click event, so we can use this to move the CommandButton before the user gets a chance to click on it. As they try to click on it, the cmdCash CommandButton is moved around the screen.

To try the example, set up a form as shown below with a Label, and two CommandButtons. Name one of the CommandButtons "cmdQuit", and the other "cmdCash". Set the TabStop property of the cmdCash CommandButton to False. Right-click on the cmdCash CommandButton and select "Bring To Front", to ensure it doesn't go behind the cmdQuit CommandButton when it's moved.

 

cash.frm
Option Explicit
Const distance As Integer = 50
Private Sub cmdCash_Click()
     Dim strMessage As String
          strMessage = "I owe you loads of money" & vbCrLf & vbCrLf & _
                              " and I promise you will get it"
          MsgBox strMessage, vbInformation, "Cash Dispenser"
End Sub
' Use the MouseMove event to move the command button so that it can never be
' clicked by the user. For this to work, the TabStop property of the button
' must be set to false, otherwise the user can press the tab key to get focus
' on the button and press the return key which is effectively clicking on
' the button.
Private Sub cmdCash_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
     ' Determine which direction to move the button horizontally
     If X < cmdCash.Width / 2 Then
          ' Move to the right (chasing from the left)
          cmdCash.Left = cmdCash.Left + X + distance
     Else
          ' Move to the left (chasing from the right)
          cmdCash.Left = cmdCash.Left + X - cmdCash.Width - distance
     End If
     ' Determine which direction to move the button vertically
     If Y < cmdCash.Height / 2 Then
          ' Move down the form (chasing from the top)
          cmdCash.Top = cmdCash.Top + Y + distance
     Else
          ' Move up the form (chasing from the bottom)
          cmdCash.Top = cmdCash.Top + Y - cmdCash.Height - distance
     End If
     ' Determine if the button has gone off the left or right of the form
     If cmdCash.Left + cmdCash.Width > Me.ScaleWidth Then
          ' Move the button back in to the left of the cursor
          cmdCash.Left = cmdCash.Left - cmdCash.Width - distance
     ElseIf cmdCash.Left < 0 Then
          ' Move the button back in to the right of the cursor
          cmdCash.Left = cmdCash.Width + distance
     End If
     ' Determine if the button has gone off the top or bottom of the form
     If cmdCash.Top + cmdCash.Height > Me.ScaleHeight Then
          ' Move the button back in to the bottom of the cursor
          cmdCash.Top = cmdCash.Top - cmdCash.Height - distance
     ElseIf cmdCash.Top < 0 Then
          ' Move the button back in to the top of cursor
          cmdCash.Top = cmdCash.Height + distance
     End If
End Sub
Private Sub cmdQuit_Click()
     Unload Me
End Sub

 


If you don't find what you are looking for. Please click here to submit your query, our experts will reply soon.

 

E-mail : sales@virtualsplat.com
Phone : +91-9892413501

+91-9967648641

+91-9967648641