First, the demand
C # Kind of dropdown box ComboBox does not support drop-down check box list and drop-down tree list, etc., where the system needs to use third-party components, and now need to replace the third-party components.
Second, the design
Basic idea: Rewrite the ComboBox, block the native drop-down, and use the ToolStripDropDown to make a pull-down popup
Iii. Problem solving 1. Problem: ToolStripDropDown in ToolStripControlHost when there will be a border generated, while CheckedListBox duck for full when the bottom will be a big blank
Solve:
Toolstripcontrolhost.margin = Padding.empty;
toolstripcontrolhost.padding = Padding.empty;
Toolstripcontrolhost.autosize = false;
toolstripdropdown.padding = Padding.empty;
CheckedListBox Set Property intergralheight to False
2. Problem: BorderStyle the display effect of different components, the drop-down portion of the edge display is not good
WORKAROUND: Set the component BorderStyle uniform to none and then into the panel, panel redraw edge and background and add ToolStripControlHost
3. Problem: The drop-down section needs to implement a draggable size
Resolution: Through the MouseDown, MouseLeave, MouseMove three events with the location of the cusor to achieve the mouse drag to change the size of the component, set the label text content "◢" as a drag indicator
4. Problem: Component flashing critical when dragging
Workaround: Use dual cache, override CreateParams in ToolStripDropDown, set CP. ExStyle |= 0x02000000;//Dual Cache
5. Problem: drop-down focus issue, click the drop down after the drop-down section does not get focus, resulting in the lower right corner of the drag and drop flag can not capture the mouse
WORKAROUND: The ComboBox may also perform some operations after the event Ondropdown causing the focus to be acquired again, so write the action that sets the drop-down focus to the Onmouseclick event
6. Problem: Text input issues with ComboBox
Workaround: When DropDownStyle is dropdown, the ComboBox can be entered, which is not appropriate, but cannot be set to be entered.
When the DropDownStyle is DropDownList, it can be implemented cannot be entered manually, but cannot be directly assigned to text, need to new an item and then the value of item selected to implement the text display
7. Problem: The drop-down portion of the ComboBox is hidden
WORKAROUND: When you need to hide the native drop-down section, set Dropdownheight=1 to
8. Problem: When the drop-down section is present, click the drop-down box to close the dropdown
Workaround: Due to the ToolStripDropDown shutdown event before the Click event of the ComboBox, it cannot be designed by the ToolStripDropDown state.
My approach is to set a global variable, Iscursoroncombobox, to determine whether the cursor is on the ComboBox when the drop-down is closed. Change this value in the closed event of ToolStripDropDown, depending on the value in the Click Drop-down event to determine whether to generate a drop-down section.
9. Problem: When the drop-down is not generated, the ComboBox is in a drop-down state after clicking once and it needs to be clicked again to return to normal
WORKAROUND: Force recovery by entering the ENTER key via the analog keyboard
10. Issue: CheckedListBox selected items after selection
Resolution: The main problem in the selection of events, if written in selected and other events, and check box selection is not appropriate (such as double-click, etc.), written on the ItemCheck event is found to be selected before the selection, causing the item value is selected to determine the delay.
Therefore, it is best to choose the ItemCheck event that hooks directly to the check, while special handling of the item being checked, using the XOR (! =) operation.
11. Problem: Compatibility, pull-down support for other components
Workaround: Add the other entry in Typec, when the pull type is other, set the dropdown content to normal Control, and the caller can set the component content to be displayed by setting Setdropdown (Control).
12. Issue: drop-down panel color displays issues under different windows themes
WORKAROUND: The drop-down box color is not displayed because the color is not called using SYTEM.XXX in Classic mode in Windows.
The colors in color.xxx are used when drawing, and are displayed normally in different system modes.
Iv. use Method 1. Drop-down Check list
① interface Drag out Hscombobox
② Setting Properties Ctltype = CheckedListBox
③ (optional) code call Hscombobox.setdropdown (CHECKEDLISTBOX) to reset the content
The ④ code calls Hscombobox. CheckedListBox to get components
2. Drop the tree-shaped
⑤ interface Drag out Hscombobox
⑥ Setting Properties Ctltype = TreeView
⑦ (optional) code call Hscombobox.setdropdown (TreeView) to reset content
The ⑧ code calls Hscombobox. TreeView to get components
3. Make a common ComboBox
⑨ interface Drag out Hscombobox
⑩ Setting Property Ctltype = Null
4. Put any control
? Interface drag out Hscombobox
? Set Property Ctltype = Other
? Code call Hscombobox.setdropdown (Control) to put content
? Code call Hscombobox.control to get components
Five, the attention point 1. Text setting for ComboBox
Call function Showtext () to set the text content, which can be used for custom component events, etc.
2. DropDownStyle
To prevent text from being entered manually, DropDownStyle will be set to DropDownList in the constructor
Demo Download:
Http://files.cnblogs.com/files/peifei/HsComboBoxDemo.rar
C # rewrite ComboBox implementation drop-down any component