C # Rewrite ComboBox method to implement Drop-down arbitrary components _c# Tutorial

Source: Internet
Author: User

I. Demand

The Drop-down box for C # ComboBox does not support the Drop-down check box list and Drop-down tree list, and so on, the system needs to use the place where the third-party components, now need to replace the Third-party components.

Second, the design

Basic ideas: Rewrite the ComboBox, the native of the lower part of the screen, the use of ToolStripDropDown production pull off the pop-up

Iii. Problem-solving

1. Problem: ToolStripDropDown in the toolstripcontrolhost will have a border generated, while CheckedListBox duck for full when the bottom will be very 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. Question: BorderStyle for different components of the display effect, the dropdown part of the edge display is not good

Resolve: The component BorderStyle unified to None, and then into the panel, panel redraw the sideline and background after joining ToolStripControlHost

3. Problem: Drop down part needs to implement drag size

Solution: Through MouseDown, MouseLeave, MouseMove three events with cusor position to implement mouse drag change component size, set label text content as "◢" as a drag instructions

4. Problem: The component flashes heavily when dragging

Workaround: Use dual caching, rewrite the createparams in ToolStripDropDown, and set CP. ExStyle |= 0x02000000;//Dual Cache

5. Problem: Pull focus problem, click the drop down after the dropdown section does not get focus, resulting in the lower right corner drag and drop flag can not catch the mouse

Workaround: ComboBox may also perform some action after the event Ondropdown to get focus again, so write the action of the set Drop-down part of the focus to the Onmouseclick event

6. Question: ComboBox text entry problem

Workaround: When DropDownStyle is dropdown, ComboBox can be entered, which is not appropriate, but cannot be set without input.

When DropDownStyle is DropDownList, you can not manually enter, but not directly to the text assignment, you need a new item and then the item's value selected to implement text display

7. Question: ComboBox's dropdown part hides

Solution: When you need to hide the native dropdown, set the Dropdownheight=1

8. Question: When the dropdown part exists, click the dropdown box, close the dropdown

Workaround: Because the ToolStripDropDown Shutdown event is not designed by the state of the ToolStripDropDown until the ComboBox click event.

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 ToolStripDropDown closed event to determine whether or not to generate a Drop-down section based on this value in the Click Drop-down event.

9. Question: When the drop is not generated, no loss of focus, ComboBox Click once in the Drop-down state, need to click again to return to normal

WORKAROUND: Force recovery by entering enter key via analog keyboard

10. Question: CheckedListBox selected items to show the content

Solution: The main problem in the choice of events, if written in selected, and other events, and check box selection is not appropriate (such as double-click), written on the ItemCheck event was found before the selection, resulting in the selected item value of the decision delay.

Therefore, it is best to select the ItemCheck event that is directly linked to the check, as well as special handling of the item being check, using the XOR or (!=) operation.

11. Problem: Compatibility, dropdown support for other components

Workaround: Add the other entry in the Typec, and set the dropdown content to normal control by setting the Setdropdown (control) to set the content of the component to display, when the immediate pull type is other.

12. Problem: Drop down panel color displays problems under Windows different topics

WORKAROUND: Because in classic windows mode, the color is not invoked using SYTEM.XXX, causing the dropdown box to appear in color.

The colors in color.xxx are used when drawing and are normal in different system modes.

Iv. Methods of Use

1. Drop Pull Check List

① interface Drag out Hscombobox

② Set Properties Ctltype = CheckedListBox

③ (optional) code calls Hscombobox.setdropdown (CheckedListBox) to reset the content

④ code calls Hscombobox. CheckedListBox can get components

2. Put down pull the tree shape

⑤ interface Drag out Hscombobox

⑥ settings Property Ctltype = TreeView

⑦ (optional) code calls Hscombobox.setdropdown (TreeView) to reset content

⑧ code calls Hscombobox. TreeView to get components

3. Do ordinary ComboBox

⑨ interface Drag out Hscombobox

⑩ settings Property Ctltype = Null

4. Put arbitrary control

? Interface drag out Hscombobox

? Set Properties Ctltype = Other

? Code calls Hscombobox.setdropdown (control) put content

? Code calls Hscombobox.control to get components

V. Points of attention

1. ComboBox's text setting

Call function Showtext () Set text content, can be used to customize the component's events, etc.

2. DropDownStyle

To prevent text from being entered manually, DropDownStyle is set to DropDownList in the constructor

The above is a small part of the introduction of C # Rewrite ComboBox implementation of arbitrary components of the method, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.