Method of C Rewriting ComboBox to Realize Drop down Arbitrary Component

  • 2021-11-13 02:25:16
  • OfStack

1. Requirements

C # kind of drop-down box ComboBox does not support drop-down check box list and drop-down tree list, etc. The third-party component is used where it is needed in the system, and now it is necessary to replace the third-party component.

STEP 2 Design

Basic idea: Rewrite ComboBox, shield the original pull-down part, and use toolStripDropDown to make drop-down pop-up

3. Problem solving

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

Resolve:


toolStripControlHost.Margin = Padding.Empty;
toolStripControlHost.Padding = Padding.Empty;
toolStripControlHost.AutoSize = false;
toolStripDropDown.Padding = Padding.Empty;
CheckedListBox Setting Properties IntergralHeight For false

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

Solution: Set the component BorderStyle Series 1 as None, then put it into panel, Panel redraw the edges and background, and then add toolStripControlHost

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

Solution: Through MouseDown, MouseLeave, MouseMove 3 events with the position of Cusor to achieve mouse drag to change the size of the component, set Label text content as "" as the drag indication

4. Problem: Component flashes badly when dragging

Solution: Use double caching, override CreateParams in ToolStripDropDown, set cp. ExStyle = 0x02000000; //Double cache

5. Problem: Drop-down focus problem. After clicking drop-down, the drop-down part does not get focus, which leads to the drag-and-drop sign in the lower right corner not capturing the mouse

Resolution: ComboBox may do something after the event OnDropDown to get the focus again, so write the action of setting the drop-down part of the focus in the event of OnMouseClick

6. Problem: Text input problem for ComboBox

Solution: When DropDownStyle is DropDown, ComboBox can be entered, which is not suitable, but cannot be set.

When DropDownStyle is DropDownList, you can not input it manually, but you can't assign value to Text directly. You need New1 Item and then select the value of Item to realize Text display

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

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

8. Problem: Click the drop-down box when the drop-down part exists to close the drop-down

Solution: Since the closing event of toolStripDropDown precedes the click event of ComboBox, it cannot be designed by the state of toolStripDropDown.

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

9. Problem: When the drop-down part is not generated and the focus is not lost, ComboBox is in a drop-down state after clicking once, and it needs to be clicked again to return to normal

Solution: Forced recovery by entering Enter key through analog keyboard

10. Problem: When CheckedListBox is selected, the contents of Items are displayed

Solution: The main problem lies in the selection of events. If it is written in selected and other events, it is different from the selection of check boxes and is not suitable (such as double-clicking, etc.). When it is written in ItemCheck events, it is found before selection, which leads to the delay in judging the selected Item value.

Therefore, it is best to choose the ItemCheck event that is directly linked to Check, and at the same time, do special processing for Item that is being Check, using exclusive OR (! =) operation.

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

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

12. Problem: Drop-down panel colors show problems under different themes in Windows

Resolution: The color of the drop-down box is not displayed because the color cannot be called using Sytem. XXX in the classic mode of Windows.

Use the colors in Color. XXX when drawing, and display normally in different system modes.

4. How to use it

1. Drop the pull check list

① Drag HsComboBox from the interface

② Set the attribute CtlType = CheckedListBox

③ (optional) code calls hsComboBox. SetDropDown (CheckedListBox) to reset content

④ Code calls hsComboBox. CheckedListBox can get components

Step 2 Drop the pull tree

⑤ Drag HsComboBox from the interface

⑥ Set the attribute CtlType = TreeView

⑦ (optional) code calls hsComboBox. SetDropDown (TreeView) to reset the contents

8 Code calls hsComboBox. TreeView can get components

3. Make a regular ComboBox

Pet-name ruby interface drag-out HsComboBox

Attribute CtlType = Null

4. Play any Control

? Interface drag-out HsComboBox

? Set the property CtlType = Other

? Code calls hsComboBox. SetDropDown (Control) to put in content

? Code calls hsComboBox. Control to get components

STEP 5 Pay Attention to Key Points

1. Text Settings for ComboBox

Call function ShowText () to set Text contents, events that can be used to customize components, and so on

2. DropDownStyle

To prevent manual text entry, DropDownStyle will be set to DropDownList in the constructor


Related articles: