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!

"Common Event" for controls 2

Status
Not open for further replies.

biddingbong

IS-IT--Management
Sep 10, 2004
67
0
0
MU
I've been trying to do this but each time tyhe code changes and a new error comes up.
What Ive been trying to do is that when the mouse moves on any label or textbox on my form; if its a label, the forecolor changes and if it is a textbox, I want the font to change.

I could have done it on a_specific_label_MouseOver()

And doing one-by-one will take too much time, Ive got 13 forms in all (maybe the number 13 is for something :) ).
And there's way too many labels and textboxes.
And they are not control arrays.

Any idea?
 
i'm not too sure the specifcs if your problem but you may want to try using a for next loop, just name the labels 1 to 13.

on the mouse over command make a variable equal a the number according to the label.

dim varD as integer

specific_label_1_MouseOver()
varD = 1
specific_label_2_MouseOver()
varD = 2
specific_label_3_MouseOver()
varD = 3

do this for each of the labels, and the have a timer running that will change it accordingly.

For X = 1 to 13
If varD = x then
specific_label_(x).forcolor = black
End If
Next X

 
Yes, this will work, but I have to code each MouseOver event and there's lots of them. Isn't there any shortcut?
 
not that i'm aware of, but that does not mean there isn't.

at least this route will make coding it a minimum.you could get each mouseover event down to three to four characters if you had a one character variable

specific_label_1_MouseOver()
a = 1
specific_label_2_MouseOver()
a = 2
specific_label_3_MouseOver()
a = 3

this will be alot easier than going into each mouseover event and going

label1.forecolor = black

but there may be another way around it, i'm not sayin gthere isn't, keep looking. good luck!
 
Create a control array. Then you can specify an individual control with it's index number. To create a control array give two controls, of the same type, the same name. VB will give them index numbers.

zemp
 
mmm...hmmmm... Anyways, I'll stick to your suggestions.
Thanks.
 
biddingbong,

This code is not finished. However, it could give you the idea were to dig if you realy want find the common place.

It does not work very well but appears to work somehow.

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim myControl As Control

For Each myControl In Form1.Controls

If X >= myControl.Left And X <= myControl.Left + myControl.Width And _
Y >= myControl.Top And Y <= myControl.Top + myControl.Height Then

If TypeName(myControl) = "Label" Then
myControl.FontBold = True
ElseIf TypeName(myControl) = "TextBox" Then
myControl.BackColor = vbRed
End If

Else

If TypeName(myControl) = "Label" Then
myControl.FontBold = False
ElseIf TypeName(myControl) = "TextBox" Then
myControl.BackColor = vbWhite
End If

End If

Next myControl
End Sub

vladk
 
biddingbong,
This code works better but if the user moves mouse fast enough then the tolerance should be increased. In this case, we could run into the problem if the controls are too close to each other.

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

Dim myControl As Control
Const TOLERANCE As Integer = 24

For Each myControl In Form1.Controls

With myControl

If X + TOLERANCE >= .Left And X - TOLERANCE <= .Left + .Width And _
Y + TOLERANCE >= .Top And Y - TOLERANCE <= .Top + .Height Then

If TypeName(myControl) = "Label" Then
.FontBold = True
ElseIf TypeName(myControl) = "TextBox" Then
.BackColor = vbRed
End If

Else

If TypeName(myControl) = "Label" Then
.FontBold = False
ElseIf TypeName(myControl) = "TextBox" Then
.BackColor = vbWhite
End If

End If

End With

Next myControl
End Sub

vladk
 
Thats wat Ive been looking for. Thanks vladk. And sorry for the late response, Ive been very busy these days.
 
vladk, excuse me, :) I have a problem with an array..if i have a 2 textbox namely txt and index are 1 & 2 respectively and i have a listbox which is invisible on form load, how can i possibly make a keypress event to txt(1) so that the listbox will be visible but not for txt(2)..can you pls give me a sample code?..thanks..been trying to make it for 2 weeks.. :(
 
MarQ
It's worth looking through faq222-2244 to see how to get the best from the forum, as a lot of this simple stuff has already been answered a dozen times, or is in a FAQ. For this one:

In the KeyPress event for the array use the Index argument:

Private Sub txt_Click(Index As Integer)
If Index = 2 Then
listbox1.Visible = True
End If
End Sub


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
MarQ,

Sorry for such a late response. How about this?

Private Sub txt_Click(Index As Integer)
listbox1.Visible = Index = 2
End Sub

vladk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top