How do I create a tooltip programatically for a programaticallygenerated label

T

The Mad Ape

Hi

I have code to programatically create a label. It works but when I try
to call a sub to create a tooltip it does not work. I do not get an
error so have no idea what it wrong. Please review my code and tell me
how to fix it.

Thanks

The Mad Ape
www.tatumba.com

CODE TO CREATE LABEL:

Dim tabLblPulp As New Label
tabLblPulp.Location = New Point(14, 121)
tabLblPulp.Size = New Size(54, 13)
tabLblPulp.Name = tp.Text & "lblPulpwood"
tabLblPulp.Text = "Pulpwood"
tabLblPulp.ForeColor = Color.Blue
tabLblPulp.Visible = True
tabLblPulp.Font = New System.Drawing.Font("Microsoft
Sans Serif", 8.25!, System.Drawing.FontStyle.Underline,
System.Drawing.GraphicsUnit.Point, CType(0, Byte))
tp.Controls.Add(tabLblPulp)

'code to call sub

AddHandler tabLblPulp.Click, AddressOf ToolTiper

Public Sub ToolTiper(ByVal sender As Object, ByVal e As EventArgs)

Dim ctrlName As String
ctrlName = DirectCast(sender, Control).Name()
Dim ToolTip2 As New ToolTip
Dim ctrl As Control
For Each ctrl In Form7.Controls
If ctrl.Name = ctrlName Then
ToolTip2.SetToolTip(ctrl, "Test")
End If
Next

End Sub
 
T

The Mad Ape

What's wrong: Form7.Controls will contain only the controls that are directly on
the form, such as a tab control. The tab control itself will contain the tab
pages; one tab page will in turn contain controls that are on it, such as a
label. So your hunt through Form7.Controls never finds the control in question.

How to fix it: You have a DirectCast to Control, using the sender of your event.
Guess what? That's the control you want. There is no need to get its name, then
go try and find it in a control collection somewhere. You already have it.

Just do
ctrl = DirectCast(sender, ctrl)
ToolTip2.SetToolTip(ctrl, "Test")

Hi

I tried the following:

Public Sub ToolTiper(ByVal sender As Object, ByVal e As EventArgs)

Dim ToolTip2 As New ToolTip
Dim ctrl As Control
ctrl = DirectCast(sender, Control)
ToolTip2.SetToolTip(ctrl, "Test")

End Sub

Still nothing. No error but no tool tip either. I am totally confused
now. I am using VB.Net from VS 2005 SP2. Any ideas? This control does
reside on a tabpage if that means anything.

I would appreciate any help you could provide.

Thanks
 
J

Jack Jackson

Hi

I tried the following:

Public Sub ToolTiper(ByVal sender As Object, ByVal e As EventArgs)

Dim ToolTip2 As New ToolTip
Dim ctrl As Control
ctrl = DirectCast(sender, Control)
ToolTip2.SetToolTip(ctrl, "Test")

End Sub

Still nothing. No error but no tool tip either. I am totally confused
now. I am using VB.Net from VS 2005 SP2. Any ideas? This control does
reside on a tabpage if that means anything.

I would appreciate any help you could provide.

Thanks

Your code works for me. Is ToolTiper() being called?
 
T

The Mad Ape

Does it ever get called? Assuming that the event has fired is not a good idea.

I would suggest first making it a sub, and calling it from a button click, so
you know it is being run. Make sure the sub works. Then figure out how to get
your event to fire, if that is the problem.

The basic code works for me, so it something in the wiring...

I am creating the label and using Add Handler. Doesn't the Add Handler
statement call ToolTiper?

Dim tabLblPulp As New Label
tabLblPulp.Location = New Point(14, 121)
tabLblPulp.Size = New Size(54, 13)
tabLblPulp.Name = tp.Text & "lblPulpwood"
tabLblPulp.Text = "Pulpwood"
tabLblPulp.ForeColor = Color.Blue
tabLblPulp.Visible = True
tabLblPulp.Font = New System.Drawing.Font("Microsoft
Sans Serif", 8.25!, System.Drawing.FontStyle.Underline,
System.Drawing.GraphicsUnit.Point, CType(0, Byte))
tp.Controls.Add(tabLblPulp)

AddHandler tabLblPulp.Click, AddressOf ToolTiper

Module ToolTipIt
Public Sub ToolTiper(ByVal sender As Object, ByVal e As EventArgs)

Dim ToolTip2 As New ToolTip
Dim ctrl As Control
ctrl = DirectCast(sender, Control)
ToolTip2.SetToolTip(ctrl, "Test")

End Sub
End Module
 
T

The Mad Ape

Your code works for me. Is ToolTiper() being called?

I assume that it is being called by the line:

AddHandler tabLblPulp.Click, AddressOf ToolTiper
 
F

Family Tree Mike

I assume that it is being called by the line:
AddHandler tabLblPulp.Click, AddressOf ToolTiper

You are clicking the tabLblPulp in your test, right? AddHandler just sets
up the handler for when you click tabLblPulp.
 
J

Jack Jackson

I assume that it is being called by the line:

AddHandler tabLblPulp.Click, AddressOf ToolTiper

No, it is being called whenever you click on the label. Once you
click on the label, you should see the tooltip.
 
T

The Mad Ape

No, it just sets it up to handle tabLblPulp.Click events. It would get called
when you click on the label.

Based on your misunderstanding, you probably don't want or need the code in the
label click event. That would mean it didn't have a tooltip at first; if you
click it, then it will have one after.

You may just want to put those lines after you have finished making the label:
' ....
tp.Controls.Add(tabLblPulp)

' now just add the tooltip to it

Dim ToolTip2 As New ToolTip
ToolTip2.SetToolTip(tabLblPulp, "Test")

The tooltip object itself handles the display of the tooltip when the user
hovers over the label a certain amount of time, and all the other behavior of
it.

When I first wrote this code, this is what I did. And when it did not
work I branched out into the sub and handler. I tried it again and it
still does not work. So in an effort to find out what is going on I
added a label to the tab and referenced it in the tool tip code that
you suggested. It works so the problem lies in the wiring between the
code used to create the label and the code to create the tool tip.

Next I wrote the code to add a new label to the form and it works. So
the problem appears to be when I attempt to add the tooltip to a
control on a tab.

Would this be a, as yet, undiscovered bug in VB.net or is there a
missing step because of the tab?

Thanks
 
T

The Mad Ape

You are clicking the tabLblPulp in your test, right? AddHandler just sets
up the handler for when you click tabLblPulp.

I was and realized that I should use Hover instead. I change it and
nothing. There is a problem between creating a new label on a tab page
and the tooltip code. As of yet I have not discovered it.
 
T

The Mad Ape

I re-read my last response and I think I need to be more clear as to
what is going on.

1) I can programatically create a label and programatically add a tool
tip to that label when the label is on a form.
2) I can programatically create a tool tip for an already existing
label on a tab page
3) I can programatically create a label on a tab page but the same
code used in #1 to programatically create a tool tip does not work and
I don't know why.
 
T

The Mad Ape

I re-read my last response and I think I need to be more clear as to
what is going on.

1) I can programatically create a label and programatically add a tool
tip to that label when the label is on a form.
2) I can programatically create a tool tip for an already existing
label on a tab page
3) I can programatically create a label on a tab page but the same
code used in #1 to programatically create a tool tip does not work and
I don't know why.

Finally after 3 days I have found the problem. Prior to creating the
label and tooltip I had code to create a rectangle and some lines
using Visual Basic Power Pack 3.0. If you draw the lines first they
hide the tool tip and cursor settings. If you add the lines after the
tooltip and cursor have been added then it will work. Sort of works in
reverse logic.

I thank you guys for helping through this very frustrating experience.
Now I can go forward with this project.

The Mad Ape
www.tatumba.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top