c# - WinForms ListBox intermittently changes the selection on CTRL+ENTER -


i'm adding hotkey support app, , throughout, use ctrl+enter hotkey confirming dialogs.

i'm having unique issue in dialog below, contains listbox. if listbox has focus (it does) when ctrl+enter hotkey pressed, intermittently shifts listbox selection down 1 position.

this creates problem, since changing selected item in listbox updates new customer name field. what's happening users choose settings want, hit ctrl+enter execute merge, , settings change before merge occurs.

i'm not handling key events on listbox, , selection change weirdly intermittent (perhaps 1 in 8 times). can't seem enter or ctrl+enter cause selection changes intentionally.

what causes behavior, , how suppress it?

enter image description here

hotkey handling

the approach i'm using form-level hotkey handling set form.keypreview = true , handle keyup event. based on this post, cleanest way support handling of modifier keys, , seems work charm.

    private void frmmerge_keyup(object sender, keyeventargs e)     {         switch (e.keycode)         {             case keys.escape:                      close(); // close form                   return;             case keys.enter:                  if (e.control) // ctrl+enter                       actionmerge(); // merge customer                   return;         }     } 

listbox use

the listbox setup simply. populated once on form load;

    private void frmmerge_load(object sender, eventargs e)     {          // add customers list          foreach(customer customer in customers)         {             lbxnames.items.add(customer.name);          }          // if there items, select first 1          if (lbxnames.items.count > 0)             lbxnames.selectedindex = 0;       } 

and handles 1 event, selectedindexchanged, update new customer name textbox

    private void lbxnames_selectedindexchanged(object sender, eventargs e)     {         txtname.text = lbxnames.selecteditem string;      } 

these 2 places listbox touched in code.

i've found workaround problem, not determined cause. best guess i'm dealing keyboard hardware or driver issue, rather listbox issue.

the intermittent nature of problem caused ctrl+m hotkey combination, used open merge dialog. m appears stick in key queue somewhere, perhaps in hardware keyboard buffer or driver. on executing ctrl+enter hotkey, m fires second time. in listbox, pressing m shifts selected record if there record beginning m.

workaround

i made 2 changes. first suppress m key in listbox. works fine me, since need arrow key , home/end navigation in listbox, , not alphabetic selection.

    private void lbxnames_keydown(object sender, keyeventargs e)     {         // suppress listbox hotkeys prevent dialog hotkey conflicts         switch (e.keycode)         {             case keys.m:                 e.handled = true;                 e.suppresskeypress = true; // required                 break;         }     } 

second, changed dialog-level hotkeys handler use processcmdkey approach in place of keyup. reason using keyup event handlers conflicted strangely between form level , listbox level. many keys captured listbox first, keypreview enabled.

    protected override bool processcmdkey(ref system.windows.forms.message msg, keys keydata)     {          switch (keydata)         {             case keys.escape:                 // close dialog                 close();                 return true;             case keys.control | keys.enter:                 // perform primary action                 actionmerge();                 return true;         }          return base.processcmdkey(ref msg, keydata);     } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -