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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Active Label

Status
Not open for further replies.

Aqif

Programmer
Apr 27, 2002
240
0
0
AU
Hi,

I have created few of hyperlink type labels on my form. Is it possible that when I click on a label it gives me the caption by using something like ActiveLabel.Caption?


Cheers!
ÙÇãá

It's important to learn the rules so that you know how to break them.
 
something like the following in the On Click event of your label:
Code:
Private Sub yourLabel_Click()
Dim strLabel As String
strLabel = Me.yourLabel.Caption
MsgBox strLabel

End Sub

HTH,
fly

[blue]Typos, that don't affect the functionality of code, will not be corrected.[/blue]

Martin Serra Jr.
[blue]Shared Database_Systems and _Applications across all Business_Areas[/blue]
 
if you want to go to the hyperlinkaddress after klicking on the label (e.g. the caption of the label would be you can use:

Code:
Private Sub yourLabel_Click()
Dim strLabel As String
strLabel = Me.yourLabel.Caption

Application.FollowHyperlink strLabel

End Sub

HTH,
fly

[blue]Typos, that don't affect the functionality of code, will not be corrected.[/blue]

Martin Serra Jr.
[blue]Shared Database_Systems and _Applications across all Business_Areas[/blue]
 
Dear HTH

With your method I need to program click-even on all the labels that I want to use. I want to avoid that. Thats why I asked for a method by wich it automatically picks up the caption of current label.


Cheers!
ÙÇãá

It's important to learn the rules so that you know how to break them.
 
Since a label can't gain focus, the ActiveControl method won't work. However, you can place a transparent command button over the top of the label - the button *can* gain focus, so you can code its click event to snag the button name with the ActiveControl method, then use that to snag the name of the underlying label. This assumes you use a naming scheme such that the button and label names are easily related to one another, i.e. btn01, lbl01. Something like this:
Code:
Private Sub btn01_Click()
Dim ctrlName As String
ctrlName = "lbl" & Right(Screen.ActiveControl.Name, 2)
Me.Controls(ctrlName).BackColor = vbRed
Me.Controls(ctrlName).ForeColor = vbWhite
End Sub
With this technique you can create one function to handle whatever it is you want to do with your labels, and just pass the label (or label name) to the function as an argument.

Ken S.
 
p.s. Here's another bit of code from a little DB I just hacked together to demonstrate:
Code:
Option Compare Database
Option Explicit
Const vbGray As Long = 12632256

Private Sub Command0_Click()
    Dim lblTheLabel As Label
    Set lblTheLabel = Me.Controls("Label" & Right(Screen.ActiveControl.Name, 1))
    Call SetColor(lblTheLabel)
    Set lblTheLabel = Nothing
End Sub

Private Sub Command1_Click()
    Dim lblTheLabel As Label
    Set lblTheLabel = Me.Controls("Label" & Right(Screen.ActiveControl.Name, 1))
    Call SetColor(lblTheLabel)
    Set lblTheLabel = Nothing
End Sub

Private Sub Command2_Click()
    Dim lblTheLabel As Label
    Set lblTheLabel = Me.Controls("Label" & Right(Screen.ActiveControl.Name, 1))
    Call SetColor(lblTheLabel)
    Set lblTheLabel = Nothing
End Sub

Sub SetColor(lblMyLabel As Label)
    If lblMyLabel.BackColor = vbGray Then
        lblMyLabel.BackColor = vbRed
        lblMyLabel.ForeColor = vbWhite
        Else
            lblMyLabel.BackColor = vbGray
            lblMyLabel.ForeColor = vbBlack
    End If
End Sub
The form has 3 label controls named Label0, Label1, and Label2. The transparent command buttons (which receive the click) are named Command0, Command1, and Command2. So you still have to code each command button, but it's the same code for each one, and I can change the behavior of the labels by editing the SetColor sub.

HTH,

Ken S.
 
To go off on a slight tangent - is it possible to prevent a linklabel from automatically opening the link?

I'm trying to put together a task pane type interface for a small db I'm working on.

Basically, I wanted to control the app through a taskpane on the left and an unbound child control on the right which I populate with the relevant sub form. I'm using Toggle Buttons at the moment - but they look so ugly. The other solution that I may use is ordinary labels and just manage the colour and underlining.
 
earthandfire, generally speaking, with some imagination and enough lines of code, I believe just about *anything* is possible. ;-) Yeah, I think you could pretty easily do what you want by controlling the forecolor, underlining, and hyperlink address properties of ordinary labels.

Aqif, to carry my example to another level, here's another offering:
Code:
Option Compare Database
Option Explicit
Const vbGray As Long = 12632256

Private Sub Command0_Click()
    Call ClickedButton
End Sub

Private Sub Command1_Click()
    Call ClickedButton
End Sub

Private Sub Command2_Click()
    Call ClickedButton
End Sub

Sub ClickedButton()
    Dim lblTheLabel As Label
    Set lblTheLabel = Me.Controls("Label" & Right(Screen.ActiveControl.Name, 1))
    Call SetColor(lblTheLabel)
    Set lblTheLabel = Nothing
End Sub

Sub SetColor(lblMyLabel As Label)
    If lblMyLabel.BackColor = vbGray Then
        lblMyLabel.BackColor = vbYellow
        lblMyLabel.ForeColor = vbBlue
        Else
            lblMyLabel.BackColor = vbGray
            lblMyLabel.ForeColor = vbBlack
    End If
End Sub
FWIW...

Ken S.
 
earthandfire,

I just played around with your idea a bit, and with a command button I was able to pretty easily disable the normal function of a linklabel:
Code:
Private Sub Command7_Click()
If Me!Label6.HyperlinkAddress <> vbNullString Then
    Me!Label6.HyperlinkAddress = vbNullString
    Else
        Me!Label6.HyperlinkAddress = "[URL unfurl="true"]http://www.microsoft.com"[/URL]
End If
End Sub
It still *looks* like a hyperlink when it's toggled off, but the click doesn't do anything.

Ken S.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top