c# - .NET winforms bug (savefiledialog)? -
the following example shows , savefiledialog on toolstripbutton click event. if pre-make savefiledialog , double-click toolstripbutton, application stackoverflows. seems bug in winforms me. not optimistic on getting fix or response ms (even couple years ago responded "no more bug fixes winforms" when reported bug), i'd opinions on whether bug or i'm doing wrong.
using system; using system.windows.forms; namespace toolstripdoubleclicksavedialog { public partial class form1 : form { savefiledialog sfd = new savefiledialog(); public form1() { initializecomponent(); } private void toolstripbutton1_click(object sender, eventargs e) { sfd.showdialog(this); } private void initializecomponent() { this.toolstrip1 = new system.windows.forms.toolstrip(); this.toolstripbutton1 = new system.windows.forms.toolstripbutton(); this.toolstrip1.suspendlayout(); this.suspendlayout(); // // toolstrip1 // this.toolstrip1.items.addrange(new system.windows.forms.toolstripitem[] { this.toolstripbutton1}); this.toolstrip1.location = new system.drawing.point(0, 0); this.toolstrip1.name = "toolstrip1"; this.toolstrip1.size = new system.drawing.size(284, 25); this.toolstrip1.tabindex = 0; this.toolstrip1.text = "toolstrip1"; // // toolstripbutton1 // this.toolstripbutton1.displaystyle = system.windows.forms.toolstripitemdisplaystyle.text; this.toolstripbutton1.imagetransparentcolor = system.drawing.color.magenta; this.toolstripbutton1.name = "toolstripbutton1"; this.toolstripbutton1.size = new system.drawing.size(23, 22); this.toolstripbutton1.text = "double click me"; this.toolstripbutton1.click += new system.eventhandler(this.toolstripbutton1_click); // // form1 // this.autoscaledimensions = new system.drawing.sizef(6f, 13f); this.autoscalemode = system.windows.forms.autoscalemode.font; this.clientsize = new system.drawing.size(284, 262); this.controls.add(this.toolstrip1); this.name = "form1"; this.text = "form1"; this.toolstrip1.resumelayout(false); this.toolstrip1.performlayout(); this.resumelayout(false); this.performlayout(); } private system.windows.forms.toolstrip toolstrip1; private system.windows.forms.toolstripbutton toolstripbutton1; } }
okay, issue comes when control doubleclicked because it's set open dialog on single click , both click events attempting open dialog @ same time. guess while dialog loading, app goes short idle state briefly before dialog opens, long enough allow other event called well, causing error when calls showdialog()
twice.
to prevent this, can system.runtime.remoting.lifetime.lease
of window , double check it's not active before showing it.
using system.runtime.remoting.lifetime; //..... private savefiledialog sfd; private ilease sfdlease; public form1() { initializecomponent(); sfd = new savefiledialog(); sfdlease= (ilease)sfd.initializelifetimeservice(); } private void toolstripbutton1_click(object sender, eventargs e) { if(sfdlease.currentstate != leasestate.active) sfd.showdialog(this); }
Comments
Post a Comment