How to change the tooltip of a 3rd party composite .NET control like the ArchestrA Object Editor

by Klaus Graefensteiner 9. November 2007 07:16

    This article is for users of the ArchestrA Object Toolkit. This post might be also useful to readers that try to change a tooltip of a composite control that is provided by any other 3rd party library. The code samples refer to the Watchdog Object that ships with the Toolkit. The Watchdog Visual Studio solution has one C# project dedicated to the editor control of the ArchestrA Object. The controls used in the composite editor control are mostly sub classes of controls found in the System.Windows.Forms namespace. By default the tooltip of each control shows the name of the ArchestrA attribute.

    clip_image001

    For most users this doesn't seem the best use of the tooltip. Especially for controls that display strings like a Textbox it would be nice if these controls would display the whole string value in the tooltip instead of the name of the data binding source.

    clip_image002

    This article describes how to code this feature in case of a library control that is a sub class of a .NET control e.g. the aaTextBox control and a composite control like the aaTextBoxMxReference control . In the basic example the tooltip will be set by a button that I added to the editor control. In a second more realistic example the change of the tooltip will be wired to the ArchestrA DataChanged events of the constituent controls.

    Basic approach:

    Open the WatchDogEditor.cs file in the Visual Studio designer and add a button control. Name it btnSetTooltip.

    clip_image003

    Double click the button. This will create and open the click handler method. Add the following lines of code:

     1:  private void btnSetTooltip_Click(object sender, System.EventArgs e)
     2:  {
     3:    //Set the Tooltip for a control that is derived from a .NET TextBox control
     4:    //This will set the value of the default property
     5:     m_ToolTip.SetToolTip(aaTimeout_Limit, "Easy Test");
     6:  
     7:    //Setting the Tooltip in a composite control that doesn't have a default property
     8:    string ValueForToolTip;
     9:    System.Windows.Forms.TextBox tb;
     10:  
     11:    //Find the constituent TextBox control in the composite control's Controls collection
     12:    foreach (System.Windows.Forms.Control c in aaWatchdogMonitoredBitInputSource.Controls)
     13:    {
     14:      if(c is System.Windows.Forms.TextBox)
     15:      {
     16:        //Cast it to a TextBox
     17:        tb = (System.Windows.Forms.TextBox) c;
     18:  
     19:        //Get the attribute value displayed in the constituent control using the Editor
     20:        //Framework's GetData() function and the control's property that represents
     21:        //the ArchestrA attribute.
     22:        ValueForToolTip = Convert.ToString(GetData(aaWatchdogMonitoredBitInputSource.Attribute));
     23:  
     24:        //Set the Tooltip
     25:        m_ToolTip.SetToolTip(c, ValueForToolTip);
     26:  
     27:      }
     28:    }
     29:  }

    More realistic implementation:

    The tooltip needs to be changed automatically after the editor opens and every time the value in the text control changes. The best approach is to trigger this change using the DataChanged event.

    The first step here is to create a method that will be called for each control event on the editor.

     1:  //Create a function that handles the ArchestrA DataChanged events of the aaTextBox and the
     2:  //aaTextBoxReference control
     3:  private void SetTooltip_Handler(object sender, ArchestraEditorFramework.DataChangeEventArgs e)
     4:  {
     5:    string ValueForToolTip;
     6:  
     7:    if( sender is aaTextBox)
     8:    {
     9:      ValueForToolTip = Convert.ToString(GetData(((aaTextBox) sender).Attribute));
     10:     m_ToolTip.SetToolTip((aaTextBox) sender, ValueForToolTip);
     11:   }
     12:   if( sender is aaTextBoxMxReference)
     13:   {
     14:     System.Windows.Forms.TextBox tb;
     15:     foreach (System.Windows.Forms.Control c in ((aaTextBoxMxReference) sender).Controls)
     16:     {
     17:       if(c is System.Windows.Forms.TextBox)
     18:       {
     19:         tb = (System.Windows.Forms.TextBox) c;
     20:         ValueForToolTip = Convert.ToString(GetData(((aaTextBoxMxReference) sender).Attribute));
     21:         m_ToolTip.SetToolTip(c, ValueForToolTip);
     22:       }
     23:     }
     24:   }
     25: }

    Then wire the events in the Visual Studio designer of each of the controls that needs to get the new tooltip to the SetTooltip_Handler method.

    For the aaTextBox:

    clip_image004

    And for the aaTextBoxMxReference control:

    clip_image005

    kick it on DotNetKicks.com

Tags: ,

ArchestrA Object Toolkit | Toolkits | Wonderware

Comments

11/9/2007 7:23:03 AM #

trackback

Trackback from DotNetKicks.com

How to change the tooltip of a 3rd party composite .NET control like t

DotNetKicks.com |

Comments are closed

About Klaus Graefensteiner

I like the programming of machines.

Add to Google Reader or Homepage

LinkedIn FacebookTwitter View Klaus Graefensteiner's profile on Technorati
Klaus Graefensteiner

Klaus Graefensteiner
works as Developer In Test and is founder of the PowerShell Unit Testing Framework PSUnit. More...

Open Source Projects

PSUnit is a Unit Testing framwork for PowerShell. It is designed for simplicity and hosted by Codeplex.
BlogShell is The tool for lazy developers who like to automate the composition of blog content during the writing of a blog post. It is hosted by CodePlex.

Administration

About

Powered by:
BlogEngine.Net
Version: 1.6.1.0

License:
Creative Commons License

Copyright:
© Copyright 2014, Klaus Graefensteiner.

Disclaimer:
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Theme design:
This blog theme was designed and is copyrighted 2014 by Klaus Graefensteiner

Rendertime:
Page rendered at 4/21/2014 12:22:35 AM (PST Pacific Standard Time UTC DST -7)