?
Solved

Runtime check if form has Tooltip-control.

Posted on 2013-10-24
6
Medium Priority
?
1,176 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

770 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