c# - comboBox with checkList -
how can combobox checklist in in c#?
i've been messing around myself. here's small sample of control has real checkboxes in rather drawing them. way can use checkbox class has offer. replaces regular drop down of combobox custom 1 inherites toolstripdropdownmenu allows add actual controls , solves problem of drop down closing whenever click on item. want extend mycombobox class quite bit fit needs, should place start.
public partial class form1 : form { public form1() { initializecomponent(); var mcb = new mycombobox() { left = 6, top = 6 }; this.controls.add(mcb); (int = 0; < 20; i++) { mcb.additem("item " + i.tostring() ); } } } public class mycombobox : combobox { private mydropdown _dropdown; public mycombobox() { _dropdown = new mydropdown() { width = 200, height = 200 }; } public void additem(string text) { _dropdown.addcontrol(new checkbox() { text = text }); } public void showdropdown() { if (_dropdown != null) { _dropdown.show(this); } } protected override void wndproc(ref message m) { if (m.msg == wndui.wm_reflect + wndui.wm_command) { if (wndui.hiword(m.wparam) == wndui.cbn_dropdown) { showdropdown(); return; } } base.wndproc(ref m); } } [toolboxitem(false)] public class mydropdown : toolstripdropdownmenu { public mydropdown() { this.showimagemargin = this.showcheckmargin = false; this.autosize = false; this.doublebuffered = true; this.padding = margin = padding.empty; } public int addcontrol(control control) { var host = new toolstripcontrolhost(control); host.padding = host.margin = padding.empty; host.backcolor = color.transparent; return this.items.add(host); } public void show(control control) { rectangle area = control.clientrectangle; point location = control.pointtoscreen(new point(area.left, area.top + area.height)); location = control.pointtoclient(location); show(control, location, toolstripdropdowndirection.belowright); this.focus(); } } public static class wndui { public const int wm_user = 0x0400, wm_reflect = wm_user + 0x1c00, wm_command = 0x0111, cbn_dropdown = 7; public static int hiword(intptr n) { return hiword(unchecked((int)(long)n)); } public static int hiword(int n) { return (n >> 16) & 0xffff; } }
Comments
Post a Comment