tags:

views:

54

answers:

2

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.

A: 

I would say that this is probably the way to go. It follows the MessageBox functionality built in to .NET.

Justin Niessner
A: 

You could create a base BalloonTipForm that your other forms inherit from with the elements that you need. You still wouldn't be able to handle validation events for specific controls in it, but you could certainly have general methods for displaying it.

And I'm sure there's some sort of way you could rig it to observe your actual validation so that you may not have to explicitly call the balloon show/hide methods. (I don't know whether the actual validation in your presenters would require multiple such calls or can be just a few.)

Your own solution doesn't look too bad, either. You could easily mix the 2.

Joel B Fant