Custom separator in a DevExpress ComboBoxEdit for Winforms

The usecase I was working with allowed a user to select previously created custom blocks from a list, a block form a ‘fixed’ list, or an item that would lead to the creation of a new block. I wanted to visually separate these three groups in the dropdown list of the combobox, so that the list of custom blocks would appear on top, the fixed list below that and at the bottom of the dropdown list the item ‘Create new block…’.

The combobox does not support grouping, something that would solve this problem for me. There is no way to tell an item that it is the first of a group (as you can do in a DevExpress Ribbon, for example) because the items in a combobox are simple strings.

dropdown_NoSeparatorsSo the first thing I tried was to insert a single minus sign as an item between the groups. This added visual space between the groups but this solution did not have the separator feel to it that I was looking for. It looks a bit sloppy and confusing.

Ideally, I wanted to have a horizontal line stretching the entire width of the list. Simply adding more minus signs was not a visually appealing option, so I looked for other ways to draw it.

The combobox has a DrawItem event, which allows you to define the appearance of each item as it is drawn, based on the content of that item or its state. The examples on the DevExpress site show how to draw a custom background for an item. The code uses the DrawRectangle method of the GraphicsCache of the item, a property of the ListBoxDrawItemEventArgs object that is passed into the DrawItem event.

Because this is a graphics object, you can use this event to paint anything at all into the item’s bounds—including a dotted line:

Private ReadOnly _comboboxSeparatorPen As _
                 New Pen(Color.FromArgb(160, 160, 160), 1) With {
                 .DashStyle = Drawing2D.DashStyle.Dot}

Private Sub combo_Applications_DrawItem(
            sender As Object, 
            e As ListBoxDrawItemEventArgs) 
            Handles combo_Applications.DrawItem

    If e.Item.Equals("-") Then
              New Point(4, e.Bounds.Top + e.Bounds.Height / 2), 
              New Point(e.Bounds.Width - 8, e.Bounds.Top + 
                                                e.Bounds.Height / 2))
        e.Handled = True
    End If

End Sub

dropdown_SeparatorsThis will render a dotted gray line across the entire width of dropdown list (minus a margin of 4 pixels) when the item is a minus sign. Note that it will not draw the minus sign but the minus sign is still the item’s value.

This is valuable in the EditValueChanging event of the combobox, because I don’t want the user to be able to select a separator. In the EditValueChanging event I can check the new value of the selection and cancel the change if needed:

Private Sub combo_Applications_Properties_EditValueChanging(
            sender As Object, 
            e As DevExpress.XtraEditors.Controls.ChangingEventArgs) 
            Handles combo_Applications.Properties.EditValueChanging

    e.Cancel = e.NewValue.Equals("-")

End Sub



Reading a book a week – one year later

One year ago I made a resolution to read a book a week, for at least one full year. I broke it down into 40 pages a day assuming the books would be about 300 pages on average, and I made it a little extra fun by keeping track of which books I read. And now, on August 19th 2013, it has been one full year since I started.

And I succeeded. I read 60 books in the past year!

What kind of books did I read? All sorts, really. Fiction and non-fiction, SF and high-brow literature, documentaries, philosophical works, adventures, comical works. Awful books, pretty good books, some really good books and a lot of fine books. I read both Dutch and English books, about 50-50. See the entire list here. SF has always been a favorite genre, and I discovered non-fiction. Two books in particular I liked: Het Been in de IJssel (about a leg found in a Dutch river and the search for the person to whom it belonged) and De Vergelding (about a Nazi retribution in a small Dutch town and how it still effects lives 60 years later).

How did I find what books to read? I got my tips from everywhere: tv, radio, friends, newspapers, magazines. I bought a lot of second-hand books from Amazon, some from my favorite bookstore in Leiden, some on markets, some in obscure little bookshops abroad (where it’s sometimes almost impossible to find English books), and one from a local library in Norway that was selling old books that nobody read anymore (The Fate of the Malous by Simenon). Looking for books was almost as much fun as reading them!

When did I read? Mostly in the mornings, before work. But also on lazy Sunday afternoons, on holidays, during quiet evenings or weekend mornings. I liked longer sittings best, I found that reading for 10 minutes while waiting for something did not get me into reading-mode very well. I read while in my comfy-chair, on the couch, in bed, in the car, on busses and trains, in the park and on terraces. I also found that my back doesn’t take well to reading for more than one hour in the same position. Something I have yet to find a solution for.

Did I enjoy it? Thoroughly. Some books better than others, but reading was never a chore. I met so many characters, so many stories, so many voices and ideas. My interior world has grown considerably, like after moving to a new and unknown city and making new friends. I welcomed all writers, all their voices and their thoughts, and I was thrilled by how much writers were willing to do to make it worth my time to listen to them.

Will I continue reading a book a week? Fuck yeah!



The Butcher Boy

My previous post’s resolution to read a book a week is still going strong, but I’ve found out that reading 45 pages every day is not so easy. Some books lend themselves to reading many pages in a single sitting, and I have read up to 60 pages before breakfast, but I’ve now come across a book that’s a lot harder to read: The Butcher Boy, by Patrick MacCabe.

Continue reading

Reading a book a week

I’ve been inspired by a blogpost from 2010 where somebody detailed how he read a book a week for a whole year. I haven’t read that many books, or in that pace, since I was a student. The English curriculum often required a well-planned reading list and the discipline to read at least one hour a day. I really enjoyed reading that many books and meeting so many interesting characters–although sometimes it was just reading a book for the sake of reading a book, and I didn’t read all of the required reading. Nonetheless, that discipline and reading list gave me a wide access to the wonderful world of books. Books enrich your life! They educate, make you think, make you angry, make you happy, dreamy, eccentric, patient, curious, and in the end I believe that reading books makes you a better person.

Continue reading

Wolf Hall Quote

On the power of language:

“When you are writing laws you are testing words to find their utmost power. Like spells, they have to make things happen in the real world, and like spells, they only work if people believe them.”

Wolf Hall, Hilary Mantel