Monday, August 30, 2004
« Keeping with the times | Main | Micro-ISV - The One Man Band »

I'm in the process of writing a custom web control (System.Web.UI.WebControls.WebControl-based class).  This control renders client-side as a DIV tag that allows for rich text editing.  One of the biggest challenges had to do specifically with focus.  This would not have been such a problem had I simplified my UI, but I'm not one to sacrifice functionality for a crappier UI if at all possible as many of my friends would atest.

My control renders a custom toolbar (or set of toolbars).  Each of these toolbar buttons or menu items renders (presently) as a TABLE, though I'm in the process of figuring out how to update my control to use DIVs and SPANs as appropriate, but that's beside the point.  My toolbar buttons are 16x16 images contained within a TD that measures 23 pixels tall by an arbitrary width.  It so happens that if I clicked directly on the image in the button, my DIV would not lose selection and the appropriate action would take place on the entire range.  However as soon as I clicked off the image (but still clicked within the boundaries of the button), my selection would disappear on mouse down and when the click event fired the DIV would have lost its focus and the action would affect just the text at the beginning of the selection.

I spent hours attempting to figure out how to programmatically select the appropriate text within the DIV using TextRanges and what not, all to no avail.  And then it dawned on me that instead of trying to reselect the appropriate range of text, I should instead work on not allowing the selection to disappear in the event that the button was clicked (but not directly on the image).

It was approximately 23 seconds (maybe 24) after this thought occurred to me that I added the UNSELECTABLE='on' attribute to the TD that defined the button.  The UNSELECTABLE attribute is quite handy - when you click on an UNSELECTABLE element on the page the current selection is not destroyed.  It turns out this was an answer to my prayers :-)  Now I just have to finish the control as it's 95% done.

Monday, August 30, 2004 8:27:00 PM (Mountain Standard Time, UTC-07:00)  #    Disclaimer  |  Comments [6]  |  Trackback