I want to add a specific behavior to multiple forms. In this case its a balloon message that is triggered when a field fails input validation. The easiest solution I could come up with was making it a static class and calling it in the failure condition of each field's onvalidate event.
public static class BalloonMessage
{
private static ToolTip _toolTip = new ToolTip()
{
Active = false,
IsBalloon = true
};
public static void Show(string message,Control control)
{
if (!_toolTip.Active)
{
//4 year old bug in tooltip doesn't place balloon "tail" correctly
// when first attached to a control. Microsoft still hasn't fixed it.
_toolTip.Show(message, control);
_toolTip.Active = true;
_toolTip.Show(message, control);
}
}
public static void Clear()
{
_toolTip.Active = false;
}
}
private void textBox1_Validating(object sender, CancelEventArgs e)
{
if (textBox1.Text.Contains(" "))
{
BalloonMessage.Show(String.Format("Field cannot contain spaces"), textBox1);
e.Cancel = true;
}
else
{
BalloonMessage.Clear();
}
}
This allows me to use the BalloonMessage in any form without requiring an explicit dependency but I'm wondering if this is the best approach. The sample code doesn't show it but the production code uses numerous interrelated MVP triads. The validation is being done in the presenters, which don't have direct access to any of the forms' controls. So I'll have to pass the validation result and any error message back to the view for displaying in a balloonmessage.
By the way, if your wondering why I'm using a tooltip instead of wrapping EM_SHOWBALLOONTIP
its because I wanted this functionallity on Windows 2000 and EM_SHOWBALLOONTIP
was added in XP. Tooltip can be displayed as a balloon in 2000 as long as IE 5.5 or greater is installed (all my Win2K clients are using IE 6). I'm simply keeping it inactive until needed to inhibit its default on hover behavior.