c# - Custom ComboBox not closing properly -


i trying make custom combobox inheriting containercontrol. used this article base rewrote it, use toolstripcontrolhost, own custom listbox & toolstripdropdown.

now combobox button click on show dropdowncontaining listbox, works fine overriding onmouseclick.

the problems starts when try close dropdown, dropdown's 'autoclose' property true, dropdown closes if click somewhere outside dropdown (including button) ...

protected override void onmouseclick(mouseeventargs e) {     base.onmouseclick(e);         /* listboxcontrol = toolstripdropdown */     if (!listboxcontrol.visible)     {     listboxcontrol.show(this, getdroplocation(), toolstripdropdowndirection.belowright);             //listbox.capture = true;     } } 

this code click on button .. happens if click ? if dropdown shown, first closes dropdown, fires onmouseclick event. meaning: listboxcontrol.visible false & show dropdown again. of causing quick close-open.

i have been stuck problem time , google doesn't seem know lot subject (that article on codeproject has same bug).

what have tried disabling autoclose , capturing mouse after show dropdown, works partially affects working of hosted listbox. listbox contains set of controls (the items), these items have hover paint effect. capturing mouse in listbox control prevents onmouseenter fired.

all input appreciated !

you need variable track cursor position when dropdown closing.

here quick , dirty example control:

public class customdropbox : control {   private listbox box = new listbox() { integralheight = false };   private toolstripcontrolhost host;   private toolstripdropdown drop;   private bool wasshowing = false;    public customdropbox() {     box.minimumsize = new size(120, 120);     box.mouseup += box_mouseup;     box.keypress += box_keypress;     box.items.addrange(new string[] { "aaa", "bbb", "ccc" });     host = new toolstripcontrolhost(box) { padding = padding.empty };     drop = new toolstripdropdown() { padding = padding.empty };     drop.closing += drop_closing;     drop.items.add(host);   }    private rectangle getdownrectangle() {     return new rectangle(this.clientsize.width - 16, 0, 16, this.clientsize.height);   }    void drop_closing(object sender, toolstripdropdownclosingeventargs e) {     if (e.closereason == toolstripdropdownclosereason.appclicked) {       wasshowing = getdownrectangle().contains(this.pointtoclient(cursor.position));     } else {       wasshowing = false;     }   }    void box_keypress(object sender, keypresseventargs e) {     if (e.keychar == (char)keys.enter && box.selectedindex > -1) {       drop.close();     }   }    void box_mouseup(object sender, mouseeventargs e) {     int index = box.indexfrompoint(e.location);     if (index > -1) {       drop.close();     }   }    protected override void onmousedown(mouseeventargs e) {     if (e.button == mousebuttons.left && getdownrectangle().contains(e.location)) {       if (wasshowing) {         wasshowing = false;       } else {         drop.show(this, new point(0, this.height));       }     }           base.onmousedown(e);   }    protected override void onpaint(painteventargs e) {     e.graphics.clear(color.white);     controlpaint.drawcombobutton(e.graphics, getdownrectangle(), buttonstate.normal);     base.onpaint(e);   } } 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -