Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mousedown does not load picture 1

Status
Not open for further replies.

rss53

IS-IT--Management
Feb 14, 2006
3
0
0
US
Hi,

I started this as a titled thread "problem with mousedown event". A couple of people have responded but no one seems to understand what's going on. It's kind of odd as this has got to be a simple problem!

Here's the skinny: When I click on the mouse on part of the picture a new picture is suppose to load. In the demo case I clear the old picture out. If you click in the center of the picture or frame the original picture is suppose to load. The code sort of works by doing what I want if I move the mouse cursor off the picture and then back onto the picture. Isn't there some way to make the picture load without having to move the cursor? I suspect that there's a basic mistake in how I set up the control. Here's the code that's associated with spreadsheet 1. Help would be greatly appreciated.

(Private Sub Image1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, _
ByVal x As Single, ByVal y As Single)


Range("a2").Value = Button
Range("b2").Value = Shift
Range("c2").Value = x
Range("d2").Value = y

' by testing the x,y coordinates you can blink an eye, open a mouth,etc'

If Range("c2").Value < 20 Then
Image1.Picture = LoadPicture("")

Else
Image1.Picture = LoadPicture("C:\Documents and Settings\My Documents\My Pictures\smiley.bmp")

End If


End Sub
 
The image has to be repainted (the parent object, like for userform), but the worksheet does not have such method. Instead, there is a workaround, using a frame:
1. put a frame onto a worksheet and remove caption,
2. add userform to vba project and add image control, copy the control,
3. edit frame on the worksheet (right-click it, object frame > edit),
4. paste image control onto frame (maybe there is an easier way to add control onto frame, I haven't found it),
5. assuming control names are Frame1 and Image1 (that's hard to find) add code to worksheet's module:
Code:
Private WithEvents xImage As MSForms.Image

Private Sub InitWithEvents()
Set xImage = Me.Frame1.Controls.Item("Image1")
End Sub

Private Sub xImage_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If X < 20 Then
xImage.Picture = LoadPicture("")
Else
xImage.Picture = LoadPicture("C:\test.gif")
End If
Me.Frame1.Repaint
End Sub
6. run InitWithEvents procedure

combo
 
Thanks combo but here's another way that seems a lot simpler although I'm still not sure why it works:
Code:
Private Sub Image1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, _
    ByVal x As Single, ByVal y As Single) 
  
    Range("a2").Value = Button
    Range("b2").Value = Shift
    Range("c2").Value = x
    Range("d2").Value = y
    
With Image1
        .Picture = LoadPicture("C:\Documents and Settings\My Documents\My Pictures\smiley.bmp")
  .Visible = False
        If Range("c2").Value < 20 Then .Picture = LoadPicture("")
        Range("a20").Value = x
        .Visible = True
End With
     
End Sub

Take a look at the code below. The first part does not run but the second part does. How come one works and the other doesn't?

Code:
Private Sub Image1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, _
    ByVal x As Single, ByVal y As Single) 
  
    Range("a2").Value = Button
    Range("b2").Value = Shift
    Range("c2").Value = x
    Range("d2").Value = y

'this code will not run correctly
 If Range("c2").Value < 20 Then
       Image1.Picture = LoadPicture("")
       Image1.Visible = True
    Else
       Image1.Picture = LoadPicture("C:\Documents and Settings\My Documents\My Pictures\smiley.bmp")
       Image1.Visible = False
    End If

'if you comment out the above and uncomment out below it 'works - how come?


'With Image1
'        .Picture = LoadPicture("C:\Documents and Settings\My Documents\My Pictures\smiley.bmp")
'        .Visible = False
'        If Range("c2").Value < 20 Then .Picture = LoadPicture("")
'        Range("a20").Value = x
'        .Visible = True
'End With

        
     
End Sub

I'm not sure why this works. For instance, how come we need to set visible to false after we load the picture - shouldn't that turn the picture off? Then, when we click on the side we load the null image and say visible = true. Just seems backwards. What's up? In the original If then else block how come the picture disappears but using the with statement it works the way it's intended?
 
The problem is that the code works in each case, but for some reason the control is not repainted each time.
Repaint method forces redrawing for a frame or userform, but does not exist for a worksheet.
Looks like the change of 'Visible' property forces repainting too. So this will trigger redrawing:
Code:
With Image1
    If Range("c2")<20 Then
        .Picture=LoadPicture("")
    Else
        .Picture=LoadPicture("C:\Path\FileName")
    End If
    .Visible=False
    .Visible=True
End With

combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top