Visualbasic7
Download
1 / 24

VisualBasic7 - PowerPoint PPT Presentation

VisualBasic7 Mouse and key events Some graphics Index of projects Mouse draw (dots) Mouse draw: two color dots Mousedraw with a draw panel, color and size radiobuttons KeyEvents Mouse draw In the code view: In the code view:

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha

Download Presentation

VisualBasic7

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


VisualBasic7

Mouse and key events

Some graphics


Index of projects

  • Mouse draw (dots)

  • Mouse draw: two color dots

  • Mousedraw with a draw panel, color and size radiobuttons

  • KeyEvents


Mouse draw


In the code view:


In the code view:

  • Under the menu bar icons and the view tabs there are two pull-down selectors.

  • Select events for the component you want to add an event to in the left pull-down menu

  • Select the event you want in the right pull-down menu.

  • This will paste in a sub template


Mouseclick (on the form)…for example


For a draw dot on mousedown project

  • We select mousedown event on the form

  • We define a colored circle (size)


Adding the mousedown event handler


Field declarations

Private Const blobsize As Integer = 8

Private graphicsobj As Graphics = CreateGraphics()

Notice that we did several things:

  • Declared constants whose values may not be changed

  • Initialized values at declaration time


Mousedown event code- remember about putting statements on one line

Private Sub frmMouseDraw_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown

graphicsobj.FillEllipse(New SolidBrush(Color.BlueViolet), e.X, e.Y, blobsize, blobsize)

End Sub

Notice that e.x and e.y (mouseeventargs.x and mouseeventargs.y) return the current mouse position.


Now, add a MouseUp event handler


MouseUp event handler

Private Sub frmMouseDraw_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp

graphicsobj.FillEllipse(New SolidBrush(Color.Green), e.X, e.Y, blobsize, blobsize)

End Sub


Should mouse draw?

Private Const blobsize As Integer = 8

Private graphicsobj As Graphics = CreateGraphics()

Private ShouldPaint As Boolean = False


New MouseUp event handler

Private Sub frmMouseDraw_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp

shouldpaint=false

End Sub


Add mouse move event handler and adjust your other events

Private Sub frmMouseDraw_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown

ShouldPaint = True ‘paint when mouse down

End Sub

Private Sub frmMouseDraw_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp

ShouldPaint = False ‘don’t paint for mouse up

End Sub

Private Sub frmMouseDraw_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove

If ShouldPaint Then

graphicsobj.FillEllipse(New SolidBrush(Color.BlueViolet), e.X, e.Y, blobsize, blobsize)

End If

End Sub


A sort of picture


Distinguishing mouse buttons inside your event handler

Suppose the mouseeventarg is called e (the default name VB gives it):

If e.button=mousebuttons.left then

‘handle left button stuff

Else if e.button =mousebuttons.right then

‘handle right button

End if


One of your choices for this week: build this


Clear the panel

  • Put a button on the form to clear the image

  • Use panel.refresh() to clear the panel


Some notes on mouse painter

  • It uses a panel, and you’ll have to use panelname.creategraphics() to get the correct graphics object.

  • Similarly, you’ll have to define the mouseUp, mouseDown and MouseMove events associated with the panel, not the form.


Be sure to add the mouse events to the panel, not the form


Keyprocessing


The keypressed event


Keypress

  • Keypress, keyup and keydown can be added to your form’s events

  • Event.KeyChar returns the keypress value.

  • In this example, it is simply appended to a String and displayed.

  • The clear button sets the string to empty and gives the textbox the focus.

  • The keypress event appends a char to message and displays it in the textbox.


ad
  • Login