Hot make a mouse leave event in vb6 over a image - vba

I have a image for which i have written code in MouseMove to higlight it. this is being done what i want is to when the mouse leaves image the highlights go away but i cant seem to find any event which will do that. i am working in visual basic 6.0. i have tried the mouseup and down event but they dont match my req.
Thanks

There isn't an event like that in VB6 (although VB.Net has MouseLeave). You will need to do something in the MouseMove event of the form (and perhaps any container controls too).
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
' Unhighlight the image'
End Sub
Private Sub Image1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
' Highlight the image'
End Sub

There is a great little ocx control for this exact purpose written by Marco Bellinaso, a well respected author and a big contributor of good content to the VB community in his time.
The control is called the "MB MouseHelper". You can download it from devx.com at http://www.devx.com/vb2themax/CodeDownload/19735.
alt text http://img25.imageshack.us/img25/3985/screencap20100809110523.jpg
There are two problems with using VB's built in MouseMove event that make this control useful:
You have to catch all the places where the user could put the mouse when it leaves your image, like the form or another control or a nearby label
And the user can still move the mouse very quickly, jumping over any part of the window that would trigger the MouseMove event that unhighlights your image

You can also put the image you want to simulate the mouseleave event inside a bigger picture. That way, when you leave the inner picture (smaller) you will hit the mousemove event of the outer picture. Also, this works if you use a frame or label instead of another picture

You can always subclass the control. Here's an article that describes how to do it.

One thing to care about if you use the mouseMove event is to raise a flag when you are IN the control you want to highlight and raise another when you are OUT so as not to repeat the same action on each mouse xy change
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
' if imageIsHighlighted = true then
' Unhighlight the image'
' imageIsHighlighted = false
' end if
End Sub
Private Sub Image1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
' if imageIsHighlighted = false then
' Highlight the image'
' imageIsHighlighted = True
' end if
End Sub

Related

Which is the most suitable way to build a graph digitizer?

I have to build a graph digitizer to make the user have a graph by points from an image. This could be an example of an imported graph
What I'd like is that the user graphically defines, at first, the X axis domain (in the picture from 0 to 2000) and the Y axis domain (in the picture from 0 to 180) and then picks some points on the curve and, once this procedure has ended, I need to have the points added to a Datagridview that I've already done. Could anyone suggest me where to start?
Edit: I've set the way to store the mouse position, when the mouse button is pressed:
Private Sub PictureBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseClick
If PictureBox1.Image IsNot Nothing Then
Form3.ListBox1.Items.Add(e.X)
End If
End Sub
I need to activate this sub when I press a specified button. Until I press the button, the mouse position, even if clicking inside the picturebox, must not be stored. If tried to put the picturebox sub inside the Button_click sub but It doesn't work.
Another event that I have to program is that - when pressing the mouse button inside the picturebox - on the first click, Form4 must be shown; at the second click, Form5 must be shown; at the third click, Form6 must be shown. From the fourth click (until the user has ended to pick points), the mouse position must be stored.

How to use an event as If statement condition

I want to write a code for my form that when user clicks on each of text boxes to enter the data, textbox background change to green.
Something like this - in form current event:
Dim c as control
For each c In me.controls
If c.OnClick then
C.backcolor= vbGreen
C.tag="clicked"
End If
If c.AfterUpdate and c.tag="clicked" then
C.backcolor= vbWhite
C.tag=""
Next c
How can I find out when an event is triggered?
You will use WithEvents for that. This is poorly documented, but an example can be found in my project (too much code to post here):
VBA.ModernTheme
Full documentation and further links are in my article on the project:
Create Windows Phone Colour Palette and Selector using WithEvents
You can create two functions in your form code as follows:
Private Function SetBackColor()
Me.ActiveControl.BackColor = vbGreen
End Function
Private Function ResetBackColor()
Me.ActiveControl.BackColor = vbWhite
End Function
Then set the On Got Focus property of all the text boxes to =SetBackColor()
and the On Lost Focus property of all text boxes to =ResetBackColor()
This can be done easily by selecting all text boxes and write the property once and it will apply to all of them (no need to repeat the writes).
This will work whenever the user tries to edit any text box even if it was reached by TAB or ENTER buttons not only mouse click.

Taborder jumping between frames

I have a userform containing 3 frames that all include several textboxes. I would like to be able to tab the textboxes across frames.
So, something like this: Frame1 - Textbox1 > Frame2 - Textbox1 > Frame3 - Textbox1 > Frame1 - Textbox2 > Frame1 - Textbox3 > Frame2- Textbox2
The textboxes are dynamically added and stored in an array according to the desired taborder, so the desired order is easily accesible. I just cant seem to find a way to apply this.
It would of course be possible to change the frame layout. However, the frames are used both to control the placement of the textboxes, and also to add separate scrollbars if the amount of textboxes exceeds the frame area.
Is something like this possible? Any help or suggestions much appreciated.
Edit: Added picture of the 3 frames
Frames
Edit2:
I think destination-datas comment put me on the right track.
I created a class module
Public WithEvents TxtBox1 As MSForms.TextBox
Private Sub TxtBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
MsgBox ("Test")
End Sub
Private Sub TxtBox1_change()
MsgBox ("Test")
End Sub
And then in the sub that generates the textboxes, for the textboxes where I want to jump frames I do:
Dim tabArray1() As New TabBox1
Dim inputfelt As MSForms.TextBox
Set inputfelt = Hovedvindu.SkjemaFrame.SenderFrame.Controls.Add("Forms.TextBox.1", "M" & i & "SenderNavn", True)
Set tabArray1(i).TxtBox1 = inputfelt
This adds all the correct textboxes to an array so that I can make an Exit event that changes focus, which I think should not be too diffucult.
However, I cant seem to get the events to fire properly.
The change event seems to work when I change the textboxes with a sub, for example when I tested that the correct textboxes are added to the array by looping through and changing the text of the textboxes in the array. But when I change the textboxes manually, nothing happens. The exit event doesnt seem to work at all.
Im not too experienced with event handling, so I might have missed something.
I found an acceptable solution. The approach described in Edit 2 of the original post worked once I moved the definition of the TabArray() outside the sub, so that I keep track of the textboxes after the sub is ended.
I have one array, and one class module for the "end" textboxes on each of the 3 frames. The class modules tracks keydown on Tab. The 3 class modules are identical, except that they store the tabindex of the textbox from where the focus jumps to another frame in 3 different hidden labels on the userform. Keeping track of the tabindex allows me to know where to start when focus comes back to a frame.
the 3 class modules look like this:
Public WithEvents TxtBox1 As MSForms.TextBox
Private Sub TxtBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
If KeyCode = vbKeyTab Then
'store tabindex
Hovedvindu.Controls("SisteTab1").Caption =
Hovedvindu.SkjemaFrame.SenderFrame.ActiveControl.TabIndex
'call sub that moves the focus
flyttfokus (1)
End If
End Sub
The sub that moves focus is shown below. Input is just an integer (1,2 or 3) depending on which class module the call comes from, and thus which frame to move from/to. The sub reads the 3 hidden labels to know which textbox to start from when the focus is moved to a new frame. Error handling in the last case is for when the focus is moved from the very last textbox in the 3d frame. This will try to move focus to a non-existing textbox in the first frame, which will throw an error which can be ignored.
Sub flyttfokus(SisteFrame As Integer)
Dim St1 As Integer
Dim St2 As Integer
Dim St3 As Integer
St1 = CInt(Hovedvindu.Controls("SisteTab1").Caption)
St2 = CInt(Hovedvindu.Controls("SisteTab2").Caption)
St3 = CInt(Hovedvindu.Controls("SisteTab3").Caption)
Select Case SisteFrame
Case 1
Hovedvindu.SkjemaFrame.MottakerFrame.Controls(St2 + 1).SetFocus
Case 2
Hovedvindu.SkjemaFrame.InfoFrame.Controls(St3 + 1).SetFocus
Case 3
On Error Resume Next
Hovedvindu.SkjemaFrame.SenderFrame.Controls(St1 + 1).SetFocus
On Error GoTo 0
End Select
End Sub
This solution will work as desired when starting from the very first textbox. If the user however, mouseclicks to a textbox for example on the second line and then starts tabing, focus will be moved back to the first line when focus is moved between frames. This is because the variables (labels) that keeps track og the Tabindex from where to start when a frame receives focus are only updated as the user tabs through the sheet. It is probably possible to update these variables on mousclick to a textbox, based on the amount of textboxes that are generated by the program. For my purposes this is not deemed necessary.

Determining when the user clicks the x - VBA

I would like to programmatically decide if the user has clicked the top right "x" button while using a form in visual basic.
I have tried:
Private Sub nameOfForm_Exit()
'code goes here
End Sub
Which has been unsuccessful. Any help would be much appreciated.
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
MsgBox "Good bye"
'/ To prevent user from closing the form
'/ Set cancel to True
Cancel = True
MsgBox "You can't close me!"
End Sub
You can't invent your own terms like "Exit". You have to take them in the combo boxes above (known as Events). Since your event pertains to the Userform itself, you need to choose Userform in the left combo box. You will also see all the controls your userform has, they each have a set of events.
As has been said QueryClose will refer to the red X in the top right corner. You also have Deactivate, which will happen whenever the UserForm loses focus (if it is ModeLess, meaning that a user can click the sheet behind the userform without closing it, this will trigger Deactivate) and Terminate, which occurs after the Userform closes with QueryClose.

How can I enable highlight when mouse over into VBA?

how can I enable highlight when mouse over into VBA? I write a marco into VBA, that after creating a new shape the shape should be enabled highlight when mouse over. Maybe with color?
Thank you
argonist
Your macro would need to run on a specific event. So in Powerpoint you have options such as:
MouseDown
MouseMove
MouseUp
You can hit the F1 key to tell you exactly which event does what.
In VBA there is no event called 'MouseOver' so you need to improvise such as using a 'MouseMove' event to change the colour when you hover over it, and then maybe the pages 'MouseMove' event to change it back to the default colour when you move the mouse onto something else.
You should be able to find all of these events in the drop down list for the object in the Visual Basic window
Another option rather than using VBA is to play around with powerpoints animations and effects which may get you the same result.
I found the simply way.
myshape.ActionSettings(ppMouseOver).AnimateAction = msoTrue
But I cannot change the color and the line size. That is enough for me. Thank you.
Try this one on ActiveX picture attach in slide (code run in show mode)
'zmien_w_trybie_prezentacji - VBATools.pl
Private Sub Image1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Call kolor2(Image1)
End Sub
Sub kolor2(osh As Image)
If osh.BackColor = 255 Then _
osh.BackColor = 13998939 Else _
osh.BackColor = 255
End Sub