Custom ListBox: Elegant and Efficient Implementation?

All,

I am currently working on setting up a ListBox to improve the clarity of my application. Basically, it's pretty easy to find an β€œhow” for this purpose, leading to my current result below.

Current display

I am happy with the display, but ... I came across strange behavior with the mouse wheel. I tried to find information on this issue, and I found this article:

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

which indicate that the problem with the mouse wheel is not the only one (simple copy / paste by reference)

  • The horizontal scrollbar has disappeared. Only fixed-length lines shorter than the width of the control can be displayed. What if the size of the control has changed?
  • If you tried to use the mouse wheel, you might notice that the selected item moves up and down as you move the scroll wheel.
  • Overridden methods OnPaint () OnPaintBackGround () do not work at all. They just are not tied to events. The background is painted only through Windows messages.

This gives some tips for fixing these issues, but I feel pretty disappointed to implement all of these β€œworkarounds” to display my own list. Did I miss something? is there any winform control that allows me the same settings, but in a cleaner / more elegant way? I could not find more information: /

The corresponding part of the user part of the drawing is added below, but I'm not sure that the display problem is really based on the implementation of the overridden 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; } 

With respect,

+4
source share
4 answers

Based on the link you specified, you must override void WndProc (ref Message m) to be able to control the mouse wheel problem. If you are going to roll your own user control based on an existing window object (even UserControl), you are going to override a number of methods and properties to make the control want you to. The link provided looks like a good start. Add these features along with an override for MeasureItem and see where you end up. But if you want to create a custom control, this is basically just a trial version and a bug where a lot of research is mixed.

+1
source

Implementing custom controls is magic. Before investing time in developing a complete ListBox implementation, you might be better off buying one from a shelf from a third-party vendor (DevExpress, Telerik, Infragistics, ComponentOne, to name a few). They all have demos on their home pages, so you can check to see if they fit your needs.

If you want to implement this yourself, perhaps try using a DataGridView instead of a list. Owners of drawn cells give you the opportunity to customize them individually.

0
source

It looks like you can use listview for your purpose. Check out this awesome custom list: ObjectListView .

0
source

This is how I solved the mouse scroll error. It is still a bit volatile, but much better than not working at all.

 Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer 'Mouse scroll bug - Yay microsoft If m.Msg = &H20A Then ' Trap WM_MOUSEWHEEL If CType(m.WParam, Integer) > 0 Then 'Scroll Up SendMessage(Me.Handle, 277&, 0&, vbNull) Else 'Scroll down SendMessage(Me.Handle, 277&, 1&, vbNull) End If End If 
0
source

Source: https://habr.com/ru/post/1301752/


All Articles