SoFunction
Updated on 2025-04-06

C# Rewrite ComboBox to implement pull-down any component

1. Requirements

The drop-down box ComboBox of C# does not support drop-down checkbox lists and drop-down tree lists, etc. The places that need to be used in the system use third-party components, and now the third-party components need to be replaced.

2. Design

Basic idea: Rewrite ComboBox, block the native pull-down part, and use toolStripDropDown to create a drop-down pop-up

3. Problem resolution

1. Problem: When toolStripControlHost is placed in toolStripDropDown, there will be a border generated, and when the duck of CheckedListBox is full, there will be a big blank at the bottom.

solve:

 = ;
 = ;
 = false;
 = ;
CheckedListBoxSet propertiesIntergralHeightforfalse

2. Problem: BorderStyle has different display effects on different components, and the display effect of the pull-down edge is not good.

Solution: Set the component BorderStyle to None, then put it in the panel, and then add toolStripControlHost after the Panel is redrawn the edges and background.

3. Problem: The drop-down part needs to achieve draggable size

Solution: MouseDown, MouseLeave, and MouseMove are used to coordinate the position of the Cusor to change the component size, and set the Label text content to "◢" as an indication of dragging.

4. Problem: Component flickering severely when dragging

Solution: Use double cache, rewrite CreateParams in ToolStripDropDown, set |= 0x02000000;//Double cache

5. Problem: Pull down focus problem. After clicking pull down, the pull down part does not get the focus, resulting in the drag and drop sign in the lower right corner not being able to catch the mouse

Solution: ComboBox may perform some operations after the event OnDropDown, causing the focus to be obtained again, so the action to set the pull-down part of the focus should be written in the OnMouseClick event

6. Question: ComboBox text input problem

Solution: When DropDownStyle is DropDown, ComboBox can be input, which is not appropriate, but cannot be set and cannot be input.

When DropDownStyle is DropDownList, it is possible to implement that it cannot be manually input, but cannot directly assign a value to Text. You need to New Item and then select the value of Item to achieve Text display

7. Problem: The pull-down part of ComboBox is hidden

Solution: When you need to hide the native drop-down part, just set DropDownHeight=1

8. Problem: When the drop-down part exists, click the drop-down box to close the drop-down

Solution: Since the closing event of toolStripDropDown before the click event of ComboBox, it cannot be designed through the status of toolStripDropDown.

My approach is to set a global variable isCursorOnComboBox to determine if the cursor is on the comboBox when the pull-down is closed. Change this value in the Closed event of toolStripDropDown, and determine whether to generate a drop-down part in the click drop-down event based on this value.

9. Problem: When the pull-down part is not generated and the focus is not lost, ComboBox is in the pull-down state after clicking once, and it needs to click again to restore normality.

Solution: Force recovery by simulating the keyboard input Enter key

10. Problem: CheckedListBox displays the content of the selected Items after it is selected

Solution: The main problem is in the selection of events. If it is written in selected events, it is different from the selection of the check box and is not suitable (such as double-click, etc.). When written in the ItemCheck event, it is found that it is before selection, resulting in delay in determining the selected Item value.

Therefore, it is best to choose the ItemCheck event that is directly linked to Check, and at the same time, special processing of the Item that is checked is performed, using the XOR (!=) operation.

11. Issue: Compatibility, pull-down support for other components

Solution: Add another entry in TypeC. When the drop-down type is Other, set the DropDown content to normal Control. The caller can set the component content to be displayed by setting SetDropDown (Control).

12. Problem: The color of the drop-down panel is displayed under different Windows themes

Solution: Because in Windows classic mode, the color cannot be called using the color, the color of the drop-down box cannot be displayed.

The colors used when drawing are displayed normally in different system modes.

4. How to use

1. Put down the checklist

① Drag HsComboBox out of the interface

② Set the attribute CtlType = CheckedListBox

③ (Optional) Code call (CheckedListBox) reset content

④ The code calls hsComboBox. CheckedListBox can obtain the component

2. Put down the tree shape

⑤ Drag HsComboBox out of the interface

⑥ Set the attribute CtlType = TreeView

⑦ (Optional) Code call (TreeView) reset content

⑧ The code calls hsComboBox. TreeView can get the component

3. Make a regular ComboBox

⑨ Drag HsComboBox out of the interface

⑩ Set attribute CtlType = Null

4. Leave any Control

? Drag HsComboBox out of the interface

? Set the attribute CtlType = Other

? Code calls (Control) to put content

? Code calls can obtain components

5. Pay attention to the key points

1. ComboBox Text settings

Call the function ShowText() to set the Text content, which can be used to customize the events of the component, etc.

2. DropDownStyle

To prohibit manual text input, DropDownStyle will be set to DropDownList in the constructor

The above is the method of rewriting ComboBox to pull down any component by C#. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!