Solved

Runtime check if form has Tooltip-control.

Posted on 2013-10-24
6
1,047 Views
Last Modified: 2013-10-30
I need to make a list of all the Tooltips that are set on the forms that I open in a vb.net application.
The tooltip control is not in the controls collection.
How can I check if the tooltip control / class is added to the form?
Secondly, when It exists, how do I connect to the tooltip class?

to put it more clear, when I open a from in my application, I enumerate all the buttons, labels and linklabels and store the text and image properties in a database. I also want to add the tooltip if one exists. For buttons I can loop through the controls collection but I don't find the tooltip anywhere. How do I see if a the tooltip control is added to the form and if so, how do I connect to that tooltip control to get the tooltips.
0
Comment
Question by:Louis_Hillebrand
  • 3
  • 3
6 Comments
 
LVL 40
ID: 39599751
Some of the objects provided by the Toolbox are not a Control, but simply a Component. This is the case for most of the objects that accumulates in the component tray at the bottom of the form instead of going into the form.

You must thus look into the Components collection instead of the Controls collection to find your ToolTip:

For Each component As System.ComponentModel.Component In Me.Components.Components
	If TypeOf component Is ToolTip Then
		MessageBox.Show("Found one.")
	End If
Next

Open in new window

0
 

Author Comment

by:Louis_Hillebrand
ID: 39599885
This works fine when I have the code in the form, but the code is in a block of code outside the form, called from the form_load event.
I cannot access the components collection when I reference the form.
The following code gives an error "'components' is not a member of 'System.Windows.Forms.Form'
For Each component As System.ComponentModel.Component In CallingForm.Components.Components
	If TypeOf component Is ToolTip Then
		MessageBox.Show("Found one.")
	End If
Next

Open in new window

0
 

Author Comment

by:Louis_Hillebrand
ID: 39600281
I managed to locate the tooltip on the form, but are unable to read the tooltip-texts from the controls.
I change the code to set the tooltip on the controls but the I get two tooltips on the same control.

    Function getToolTip_Component(ByVal obj As Object) As ToolTip
        'Dim TT As ToolTip = Nothing

        Dim t As Type = obj.GetType
        Dim frm As Object = Activator.CreateInstance(t)
        Dim field As FieldInfo = t.GetField("components", BindingFlags.Instance + BindingFlags.NonPublic)
        If Not field Is Nothing Then
            Dim C_Container As IContainer = field.GetValue(frm)
            If Not C_Container Is Nothing Then
                With C_Container
                    'find component by type 
                    For i = 0 To .Components.Count - 1
                        If TypeOf .Components(i) Is ToolTip Then
                            Return .Components(i)
                        End If
                    Next
                End With
            End If
        End If
        Return Nothing
    End Function

    Private Sub Set_ToolTips(ByVal objRoot As Object, Optional ByRef TT As ToolTip = Nothing)

        If TypeOf objRoot Is Form Then
            Try
                TT = getToolTip_Component(objRoot)
            Catch ex As Exception

            End Try
        End If

        For Each obj As Control In objRoot.Controls
            If TypeOf obj Is Button OrElse
                TypeOf obj Is LinkLabel OrElse
                TypeOf obj Is Label Then

                If NOT TT Is Nothing Then
                    Dim ObjToolTip as string = TT.GetToolTip(obj)  
                         ' always returns empty string
                    TT.SetToolTip(obj, obj.name)                               
                         ' to test setting the tooltip
                         ' creates a second tooltip on the same control
                End If

            End If

            If obj.HasChildren Then
                Set_ToolTips(obj, TT)
            End If
        Next

    End Sub

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 40
ID: 39600591
I am not sure if this suits your needs, its not completely clear for me, but since Set_ToolTips in is Private, it is part of the Form itself, so the following enables you to move through the ToolTips and to set the text for all the controls:
Private Sub Set_ToolTips()

	For Each component As Component In Me.components.Components
		Debug.WriteLine(component.GetType.Name)
		If TypeOf component Is ToolTip Then
			Dim toolTipControl As ToolTip = component
			For Each ctl As Control In Me.Controls
				toolTipControl.SetToolTip(ctl, "Text to set in the tooltip")
			Next
		End If
	Next

End Sub

Open in new window

0
 

Author Comment

by:Louis_Hillebrand
ID: 39608385
The Set_Tooltips procedure (or Read ) is not in the form-class but called from de Form_Load event. (It is part of a larger procedure which sets default values to the forms I open).

the  getToolTip_Component function returns a reference to the correct Tooltip Component, but with that reference I cannot read the available tooltips and when I set a tooltip on the control It adds another tooltip to the control.
I tested this by setting a tooltip which shows the control name, this overlays the original tooltip. (see attached image).

My question remains: How do I connect to the tooltip Component to read and properly set tooltips?
Tooltip.jpg
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 39609005
The problem is that the Components collection of the form is Private, so it can be read only in the form itself.

The only way I see to make it work in a method that is outside of the form if to implement IContainer in the form so that you can get out its list of components:
Public Class Form1
	Implements IContainer

	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
		Set_ToolTips(Me)
	End Sub

	Public Sub Add(component As IComponent) Implements IContainer.Add
	End Sub

	Public Sub Add(component As IComponent, name As String) Implements IContainer.Add
	End Sub

	Public ReadOnly Property FormComponents As ComponentCollection Implements IContainer.Components
		Get
			Return Me.components.Components
		End Get
	End Property

	Public Sub Remove(component As IComponent) Implements IContainer.Remove
	End Sub
End Class

Open in new window

Then, a method like the following in a module or dll enables you to both read and set or replace the tooltips as needed.
	Public Sub Set_ToolTips(form As IContainer)

		For Each component As Component In form.Components
			Debug.WriteLine(component.GetType.Name)
			If TypeOf component Is ToolTip Then
				Dim toolTipControl As ToolTip = component
				For Each ctl As Control In DirectCast(form, Form).Controls
					If toolTipControl.GetToolTip(ctl) = "Tooltip to replace" Then
						toolTipControl.SetToolTip(ctl, "New tooltip")
					End If
				Next
			End If
		Next

	End Sub

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SLMGR Switches Are Not Working On KMS Host 3 68
Library to convert HTML to PDF. 8 55
Make a border less form movable 2 22
Need a starter for ETL protocol? 4 39
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now