Search code examples
c#winformstooltip

Winforms ToolTip get and change all assigned tooltips


I'm cycling over my WinForms controls and give their Text and ToolTipText to my Translation service for translation. Example:

        foreach (ToolStripItem item in toolStrip.Items)
        {
            if (item is ToolStripMenuItem)
            {
                item.ToolTipText = Translate(item.ToolTipText);
                item.Text = Translate(item.Text);
            }
        }

However, I cannot access tooltips set by using the WinForms ToolTip control.

I see I can cycle over the components. Can I Get and Set their tooltips?

    protected void TranslateToolTip(ToolTip toolTip)
    {
        foreach (var component in toolTip.Container.Components)
        { 
            // Doesn't work. No ToolTipText property
            component.ToolTipText = Translate(component.ToolTipText);
        }
    }

Can I access the tooltip text directly from the control?


Solution

  • To set a tooltip text on all components in your form like button1 etc. I think you should use something like this:

    foreach (var control in this.Controls) {
                ToolTip1.SetToolTip(this.control, "Your text");
            }
    

    That's because ToolTip doesn't have a Text property and it's set like on example above.

    See also ToolTip Class and ToolTip.SetToolTip Method and ToolTip.GetToolTip Method

    Or you can try something like that but not sure if this gonna work:

    protected void TranslateToolTip(ToolTip toolTip)
    {
        foreach (var component in toolTip.Container.Components)
        { 
            // Doesn't work. No ToolTipText property
            // component.ToolTipText = Translate(component.ToolTipText);
               toolTip.SetToolTip(component , (string)Translate(toolTip.GetToolTip(component));
        }
    }
    

    I don't know what Translate(component.ToolTipText) is supposed to return. If it's just translated string, then we don't need the (string) part in my exmple.

    Hope it helps.

    EDIT: Fixed second example to show how to Set and Get tooltip text from specific control.