views:

77

answers:

1

I have some validation code which should display the max / min of a control when a bad value is entered.

In my constructor I do this:

m_wndToolTip = gcnew ToolTip(this->components);
m_wndToolTip->AutoPopDelay = 5000;
m_wndToolTip->InitialDelay = 10;
m_wndToolTip->ReshowDelay = 250;
m_wndToolTip->ShowAlways = true;// Force the ToolTip text to be displayed whether or not the form is active.

This is my validation reflection code:

void MyGUI::IndicateValidationResult(Windows::Forms::Control^ control, bool isValid, String^ invalidReason)
{
    // If the validation failed, make the background red. If not, turn it white.
    if( isValid )
    {
        control->BackColor = Drawing::Color::White;
        m_wndToolTip->Hide(control);
    }
    else
    {
        control->BackColor = Drawing::Color::LightCoral;
        m_wndToolTip->Show(invalidReason, control);
    }
}

...which is called from varous ValueChanged methods in my text boxes. I've tried using show and also a combination of SetToolTip and active = true and nothing seems to work.

I've seen another question asking about tooltips and have tried setting a nearby label in the call to show but that doesn't fix it either. The tooltip is a member variable in my System::Windows::Forms::Form derived form to stop it going out of scope.

Am I missing something obvious?

+1  A: 

Your code worked fine when I tried it, there's no obvious mistake that I can see. I called it like this, using a Validating event of a text box:

bool ok;

System::Void textBox1_Validating(System::Object^  sender, System::ComponentModel::CancelEventArgs^  e) {
  e->Cancel = !ok;
  IndicateValidationResult(textBox1, ok, "invalid");
  ok = !ok;
}

Beware that ToolTip can be cranky. The native Windows component has a "feature" that prevents a tooltip from showing again when it timed out before. The ErrorProvider component is a better mouse trap to get this done.

Hans Passant