Nielsen's Weblog : .NET [use your Context dude]
Updated: 09-11-2004; 16:46:50.

 

Subscribe to "Nielsen's Weblog" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.

 
 

07. november 2004

what a feisty little interface. I had about 10 edit controls, 6 of them with similar behavior, they should all be validated and it should be possible to do a database lookup based on the edit fields content. I figured a 'search icon' next to each edit field would be the best UI design here. Fine I placed a picturebox on 6 of the edit controls' right side, looked ok, but what troubled me was the decoupling between the edit controls and the lookup icon + each time I changed my design it got messy moving all them edit controls and pictureboxes around..if only each edit control was bundled with the picturebox and all the validation and searching functionality was associated with the edit control. My thoughts actually went back to the good old Win32/16 days ... remember the SetProp function! where you could associate custom data with each Control. After a quick walk down memory lane, I stumbled over the IExtenderProvider interface while trying to squeeze ErrorProvider onto my form also.

You must implement only 1 method, the CanExtend method where cast your vote on what type of control you want to extend.

[ProvideProperty(Search, typeof(string))]
public class SearchProvider : Component, IExtenderProvier
{
           ......
          .......
          private Hashtable hash = new Hashtable();
          //Forms designer will pass in every control to see if we will extend the control
          public bool CanExtend(object ctrl)
          {
                    //make sure my edit control is on my form.
                   if(ctrl.GetType()== typeof(TextBox))
                  {
                           if((((Control)ctrl).FindForm()==null)
                                return false;
                          else
                                return true;
                   }else {
                          return false;
                   }
          }   
 
          public void SetSearchProp(Control ctrl, string sValidate)
          { 
                  if(!DesignMode)
                 {
                   //create a class that handles the validation with Regex
                   JCSharp.Utility.ValidateSearch vs = new JCSharp.Utility.ValidateSearch(ctrl, sValidate);
                  //register the validating event
                  ctrl.Validating += new CancelEventHandler(vs.OnValidating);
                  //create my search box
                  JCSharp.utility.SearchHelper sh = new JCSharp.utility.SearchHelper(ctrl);
                   .....
                 }
                 hash[ctrl]=sValidate;
         }

          public string GetSearchProp(Control ctrl)
          {
                   return (string)hash[ctrl];
          }
          ........

}

we need to provide accessor method which we indicate with the ProvideProperty
....
....
public class SearchHelper
{
 
          public SearchHelper(Control ctrl)
         {
                  PictureBox pic = new PictureBox();
                  pic.Image = GetImage();
                  pic.Size =  new Size(16,16);
                  pic.Location = new Point(ctrl.Right + 10, ctrl.Top);//looks okay to me.
                  ctrl.Parent.Control.Add(pic);
                   //when we click on the picturebox, do a database loopup based on the content of the edit  control
                  pic.Click += new EventHandler(OnLookup);
           }
          .......

}

now call the searchProvider like this on your form

             searchProvider1.SetSearchProp(TextBox1,".*@.*"); //validate email.
             searchProvider1.SetSearchProp(TextBox1,"");

The IExtenderProvider interface, a very sparse interface, very easy to ignore :) but solves all your problems when you want to extend a control with extra properties.

"Have you seen any code reuse lately ?"
-- Anonymous attendee, OOPSLA 2004 :)


8:50:19 PM    comment []

© Copyright 2004 Allan Nielsen.



Click here to visit the Radio UserLand website.
 


November 2004
Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30        
Oct   Dec