Solved

Runtime check if form has Tooltip-control.

Posted on 2013-10-24
6
1,021 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 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

13 Experts available now in Live!

Get 1:1 Help Now