473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem with mouse events on form with PictureBox

I am writing a program to draw lines over an existing image within a PictureBox. The routine works perfectly INSIDE the PictureBox (I'm drawing a rubberband line), but I cannot get the mouse to work on any buttons OUTSIDE the picturebox. (In this example, Button1 doesn't respond)

I have written three mouse event subs (MouseUP, MouseMOVE, MouseDOWN) that work within the PictureBox, but I don't know how to write the program so that I get normal mouse function when the mouse is pointing outside the PictureBox.

This MUST me easy to do... but I'm obviously missing something. I'd really appreciate some help with this.

Here is (most of) my code , some of it borrowed from various examples I've found here and there. I'm using Visual Studio 2005. (the form contains PictureBox1, TextBox1, TextBox2, TextBox3, TextBox4, Button1)

Expand|Select|Wrap|Line Numbers
  1. Imports System.Drawing
  2. Imports System.Drawing.Imaging
  3. Imports System.Windows.Forms 
  4. Public Class Form1
  5.     Public bitmap As System.Drawing.Bitmap
  6.     Public bm As System.Drawing.Bitmap
  7.     Public BitmapOrg As System.Drawing.Bitmap
  8.     Private drag As Boolean = False
  9.     Private DrawMode As Integer = 1
  10.     Private x0, y0, x, y As Integer
  11.     Private cx, cy As Integer
  12.     Private gB As Graphics 
  13.  
  14.     Private Sub Form1_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
  15.     End Sub 
  16.  
  17.     Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
  18.         Dim p As Point = New Point(e.X, e.Y)
  19.         x0 = p.X    
  20.         y0 = p.Y   
  21.         TextBox1.Text = "MouseDOWN " & CStr(x0) & ", " & CStr(y0)
  22.         drag = True
  23.     End Sub 
  24.     Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
  25.         Dim p As Point = New Point(e.X, e.Y)
  26.         x = p.X
  27.         y = p.Y
  28.         cx = x - x0
  29.         cy = y - y0
  30.         TextBox2.Text = "MouseMOVE " & CStr(cy) & ", " & CStr(cx)
  31.         If drag Then
  32.             Invalidate()
  33.         End If
  34.     End Sub 
  35.     Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
  36.         cx = x - x0
  37.         cy = y - y0
  38.         TextBox3.Text = "MouseUP " & CStr(cy) & ", " & CStr(cx)
  39.         Dim pen As Pen = New Pen(Color.Yellow)
  40.         RefreshBackground()
  41.         drag = False
  42.         pen.Dispose()
  43.     End Sub 
  44.     Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
  45.         bitmap = New System.Drawing.Bitmap("c:\Bitmaps\Test_A.bmp")
  46.         Me.PictureBox1.Image = bitmap
  47.         Dim g As Graphics = Graphics.FromImage(bitmap)
  48.         Dim pen As Pen = New Pen(Color.Yellow)
  49.         TextBox4.Text = CStr(drag.ToString)
  50.         If drag Then
  51.             g.DrawLine(pen, x0, y0, x, y)
  52.         End If
  53.         pen.Dispose()
  54.     End Sub 
  55.     Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  56.         MsgBox("Here at Button 1")
  57.     End Sub 
  58.     Private Sub Form1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.SizeChanged
  59.         RefreshBackground()
  60.     End Sub 
  61.     Private Sub RefreshBackground()
  62.         Dim sz As Size = Me.Size
  63.         Dim rt As Rectangle = New Rectangle(0, 0, sz.Width, sz.Height)
  64.         Dim bm As Bitmap
  65.         If (bm IsNot Nothing) Then
  66.             bm = bitmap.Clone(rt, bitmap.PixelFormat)
  67.             BackgroundImage = bm
  68.         End If
  69.     End Sub 
  70.     Public Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
  71.  
  72.     End Sub
  73.  
  74. End Class
Mar 19 '10 #1
3 4605
tlhintoq
3,525 Expert 2GB
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Mar 19 '10 #2
tlhintoq
3,525 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
  2.         bitmap = New System.Drawing.Bitmap("c:\Bitmaps\Test_A.bmp")
Every time you paint you are reading the base image from the harddrive? Ouch!
May I suggest you read it one time at startup and use it (or a clone of it) through out the rest of the application. This is not only going to slow you down a LOT but beat the heck out of your drive.

Expand|Select|Wrap|Line Numbers
  1. Private Sub RefreshBackground()
  2.         Dim sz As Size = Me.Size
  3.         Dim rt As Rectangle = New Rectangle(0, 0, sz.Width, sz.Height)
  4.         Dim bm As Bitmap
  5.         If (bm IsNot Nothing) Then
  6.             bm = bitmap.Clone(rt, bitmap.PixelFormat)
  7.             BackgroundImage = bm
  8.         End If
  9.     End Sub 
Once you reassign a new BackgroundImage that is going to trigger a new Form1_Paint event, and reload the file from harddrive.

I kinda wonder if the system isn't just overwhelmed.

You might also want to toss in an Application.DoEvents() at the end of your Form1_Paint so you can give some CPU time to other things, like other button clicks.
Mar 19 '10 #3
You are absolutely correct about the repeated disk reads... I jfollowed your advice and cloned the bitmap... it was something I should have caught but I was entirely focused on the mouse issue!

Anyway, that was not the problem.

The problem was a foolish error... and simple as I thought it might be! The Button1 sub that wasn't working needed a "Handles" reference!!!

It now works fine by adding "Handles Button1.Click" so that the sub now reads:

Private Sub button1_Click(ByVal sender As Object, ByVal e As_ System.EventArgs) Handles Button1.Click
MsgBox("Here at Button 1")
End Sub

Anyway... many thanks for the assistance!

--Dave Emery
Mar 19 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Stephen Williams | last post by:
I am migrating a VB 6 PictureBox control to VB.NET. In VB 6, this control modified its border style during the mouse down and up events to provide user feedback that it was selected. Once...
6
by: CoreyMas | last post by:
Hello All, Here is what I would like to do I am creating a game program that displays a map (preferably in hexes but that is another matter) and whenever the user is over the "map" I want to...
1
by: Rhy Mednick | last post by:
I'm creating a custom control (inherited from UserControl) that is displayed by other controls on the form. I would like for the control to disappear when the user clicks outside my control the...
0
by: Johann Blake | last post by:
I created my own toolbar from scratch using PictureBoxes. I use the MouseEnter and MouseMove events to change the image on the PictureBox controls so that it makes the "button" look selected,...
4
by: Raj Chudasama | last post by:
I have a controls similiar to the windows calculator. (Please press some buttons on your calculator to see what i am talking about) So when u hover over a button it will change the state (it...
0
by: 6tc1 | last post by:
Hi all, I've got a UserControl that contains a few PictureBox objects. If I click on outside of the Picture in the UserControl, the scrolling with the mouse button works - however, no amount of...
3
by: Tom | last post by:
I have a picturebox on my VB.NET form. The picturebox size mode is set to stretched. I then load an image into that form and display it. As the user moves the mouse over the form, I want to get and...
7
by: MasterMax1313 | last post by:
I'm trying to make a grid of picture box controls, which I do via code. Each of these boxes has a mouseclick, mousedown, mouseup, and mouseenter event. The mouseclick event is simple enough and works...
1
by: Nitoc3 | last post by:
I have a panel with a picturebox on top of it and I would like to make the picturebox invisible to mouse events so when the mouse is hover the picturebox is actually the panel mouse events that are...
2
by: kimiraikkonen | last post by:
Hi, I want to crop and save specified image area which is in a picturebox with freehand using cursor, using mouseDown, mouseUp and mouseMove events. How can crop an image with freehand (using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.