Tuesday, July 20, 2004

Praise be for the quick wins

There's something very satisfying, I think, about building UI's. Although at times they can be quite tedious, particularly the finer points of layout, they do provide some of the really quick dirty wins. And I like that.
I'm just putting the dektop UI on SavageBlog (which is what I've decided to call my little blogging app), and I've done something really rather clever. Well, I think, anyway. What I wanted was a textbox for putting the content into blog entries. Although I'm not averse to adding my own tags for formatting, I wanted something a little quicker to work with, something not dissimilar to Blogger.com's very own UI for editing posts. So I added a little toolbar, with buttons for Bold, Italic and Hyperlink text. All I wanted, when I clicked the button, was for the selected text in the box to be prepended and appended with the opening and closing tags for my format of choice (<b> and </b>for instance). The way I did it was this:

txtContent.SelectedText = String.Format("<b>{0}</b>", txtContent.SelectedText)
Which worked fine. Happy as larry. I just select some text to embolden, and it adds the right tags. After using it a couple of times, though, I wanted to improve it a bit. If there was no text selected, I wanted it to add the tags, and then reposition the cursor between the tags. So, if there was nothing selected and I hit the bold button (I haven't added ctrl+x key mappings yet) I could just carry on typing rather than having to move the cursor back between the tags.
The solution I came to was simplicity itself, it just needed an extra 4 lines of code: For your enjoyment, here is the whole lot:
Private Sub ToolBar1_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles ToolBar1.ButtonClick
    Dim SelLength As Integer = txtContent.SelectionLength
    Select Case CType(sender, ToolBar).Buttons.IndexOf(e.Button)
        Case 0
        txtContent.SelectedText = String.Format("<b>{0}</b>", txtContent.SelectedText) 
        Case 1 
        txtContent.SelectedText = String.Format("<i>{0}</i>", txtContent.SelectedText) 
        Case 2 
        txtContent.SelectedText = String.Format("<u>{0}</u>", txtContent.SelectedText) 
        Case 3 
        Dim Linker As New HyperLinkBox 
        If Linker.DialogResult = DialogResult.OK Then 
            txtContent.SelectedText = String.Format("<a href="{0}" target="_blank">{1}</a>", Linker.LinkAddress, txtContent.SelectedText) 
        End If 
        End Select 
    If SelLength = 0 Then 
        txtContent.Select(txtContent.SelectionStart - 4, 0) 
    End If
End Sub
For reference, Linker is a dialog box that allows the user to enter a URL for the link.
It's not terribly exciting, and to be fair, I reckon 90% of people would never even notice of that feature hyadn't been added. HOWEVER, it did make me feel happy. And it didn't take very long to do. And it worked (quite amazingly, for me) first time. Like I said, quick dirty wins. I should imagine there'll be quite a few more.
On a related note, Blogger.com's just upped the ante, and improved its UI.

No comments: