views:

306

answers:

3

All,

I'm currently working on some ListBox customization in order to improve the clearness of my app. Basically, it is quite easy to find 'how to' for this purpose leading to my current result below.

Current display

I'm happy with the display but ... I faced strange behavior with the mousewheel. I tried to find information on this issue and I found this article :

http://aviationxchange.net/wikis/winforms/net-color-listbox.aspx

which point out that the mousewheel problem is not the only one (simple copy/paste from the link)

  • The horizontal scrollbar disappeared. Only fixed length strings smaller than the control width can be displayed. What if the control resized?
  • If you tried to use a mouse wheel, you may have noticed that the selected item moves up and down erratically when the scroll wheel is moved.
  • The overridable methods OnPaint() OnPaintBackGround() do not work at all. Simply they are not hooked to the events. Background is painted only via Windows messages.

It gives some advices for correcting these issues, but I feel quite frustrated to implement all these "workarounds" for displaying a custom list. Do I miss something ? is there any winform control which allow me the same kind of customizations, but in a more clean/elegant way ? I was not able to find more information :/

Below, added relevant part of the custom drawing part, but I'm not sure that the display issue is really based on the implementation of overriden method, more on the control itself.

    public RecordListBox(): base()
    {
        mListBox = this;

        mListBox.DrawItem += new DrawItemEventHandler(mListBox_DrawItem);
        mListBox.MeasureItem += new MeasureItemEventHandler(mListBox_MeasureItem);
        this.DrawMode = DrawMode.OwnerDrawFixed;

    }

    public void mListBox_DrawItem(object sender, DrawItemEventArgs e)
    {
        if (this.DesignMode) return;

        e.DrawBackground();
        e.DrawFocusRectangle();

        // drawing actions

    }

    public void mListBox_MeasureItem(object sender, MeasureItemEventArgs e)
    {
        e.ItemHeight = 40;
    }

Regards,

A: 

There is a 3-rd party control, IntegralUI ListBox, which allows you to customize the layout of every item. Variuos objects like: text, image, hyperlink, custom contrtols etc can be added and placed in different locations in single item space. See this screenshot:

alt text

Lokey
A: 

Implementing user controls is a magic on it's own. Before you invest the time to develop a complete ListBox implementation, you might be better of buying one off the shelf from a third-party vendor (DevExpress, Telerik, Infragistics, ComponentOne to name a few). They all have demos available on their homepages so you can check if they suit your needs.

If you want to implement this yourself, maybe try using a DataGridView instead of a listbox. Owner drawn cells give you the freedom to customize them in every possible way.

Johannes Rudolph
THis project is clearly a personal one so I do not intend to invest any money for it, except my time. In a personal scope, the result is acceptable for the moment. I already tried before using DataGridView, but I was not seduce by the render. Finally, I will try to implement advices given in the link I provided in the original post and see if it fixes the problem. When it will be done, I will give an update on the result.
camous
+1  A: 

Based on the link given, you have to override void WndProc(ref Message m) to be able to manage the mouse wheel problem. If you are going to roll you own custom control based on an existing window object ( even the UserControl ) you are going to have override a number of methods and properties to make the control do want you want. The link given looks to be a good start. Implement those functions along with the override for MeasureItem and see where you end up. But if you want to create a custom control, it is mostly just trial and error with a lot of research mixed in.

JDMX