Search code examples
c++winformstooltip

Can't hide ToolTip


I created a tool tip for a text box by including the code below in the HelpRequested event.

ToolTip^ toolTip_patterns = gcnew ToolTip;
String^ caption;

caption = "Help the user blah blah...";
toolTip_patterns->Show(caption, this->textBox_patternsTer);

I want the tool tip to disappear when the user leaves the text box. It should not be displayed unless the user presses F1 again. I tried to hide the tool tip with the code below in the Leave event.

ToolTip^ toolTip_patterns = gcnew ToolTip;
toolTip_patterns->RemoveAll();

The above doesn't work, so I tried the following. This doesn't work either.

toolTip_patterns->Hide(this->textBox_patternsTer);

When I use either Method (RemoveAll or Hide), the tool tip disappears when the user leaves the text box, but it comes back when they hover the mouse over the text box. I do not have any code in the MouseHover event. Why does it do this?

Thanks!


Update...Sorry if this isn't the right place to type an update.

Thanks for the suggestion but it doesn't work. I added a ToolTip control to my form from the Form Designer, and each of my controls now has a Property named ToolTip on toolTip_patterns. I deleted the declaration below from the HelpRequest event.

ToolTip^ toolTip_patterns = gcnew ToolTip 

I set the text to be displayed with the code below.

private: System::Void textBox_patterns_HelpRequested(System::Object^  sender, System::Windows::Forms::HelpEventArgs^  hlpevent)
     {
        String^ caption;
        String^ module;

        if (sender == textBox_patternsTer)
            module = "Terminator";
        else if (sender == textBox_patternsSec)
            module = "Secondary";
        else if (sender == textBox_patternsPri)
            module = "Primary";
        else
            return; // No help for selected control.

        caption = "Enter 32 bit Hex patterns for the " + module + " one pattern per line.\n";

        toolTip_patterns->InitialDelay = 0; //< immediately show help
        toolTip_patterns->Show(caption, (TextBox^)sender);

I also tried the non-modal form below.

        toolTip_patterns->SetToolTip((TextBox^)sender, caption);

The tool tip displays fine, and when I move the focus to another control and press F1, the tool tip for the second control is displayed. The problem is that the tooltip for the first control is displayed when I hover the mouse over it. I don't want the tool tips to be displayed after the user exits the control. The Hide() method doesn't work, the RemoveAll() method doesn't work, and setting the caption to "" doesn't work. Any idea what I'm doing wrong? Thanks.


Solution

  • I would be inclined to just set the text when the user requests help, and then clear it when the mouse leaves.

    Note that you'll need to keep a pointer to the tooltip between the help requests call and the mouse leaving, rather than allocating a new one each call to request help. If you don't do this you'll not have a handle on the object and be able to operate on it.

    ToolTip^ toolTip_patterns = gcnew ToolTip;
    
    void doHelpRequest()
    {
        String^ caption;
    
        caption = "Help the user blah blah...";
        toolTip_patterns->Show(caption, this->textBox_patternsTer)
        toolTip_patterns->InitialDelay = 0; //< immediately show help
    }
    

    then

    void doMouseLeave()
    {
        toolTip_patterns->Show("", this->textBox_patternsTer)
    }