c# - After setting selectedindex, click on control with mouse shows wrong item -


the engineers requested combobox numbers 500 -500 (negative numbers listed @ bottom, not alphabetical).

they requested being able type in number in combo jump correct item.

problem: type in "44", tab away. click on control mouse , can see "449" selected.

here complete code:

using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms;  namespace combotest {     public partial class form1 : form     {         public form1()         {             initializecomponent();         }          private void form1_load(object sender, eventargs e)         {             (int = 500; > -500; i--)             {                 combobox1.items.add(i.tostring());             }         }          private void combobox1_leave(object sender, eventargs e)         {             combobox1.selectedindex = combobox1.findstringexact(combobox1.text);         }     } } 

no problem! tell myself. findstringexact finding first alpha match. replace leave event code loop:

using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms;  namespace combotest {     public partial class form1 : form     {         public form1()         {             initializecomponent();         }          private void form1_load(object sender, eventargs e)         {             (int = 500; > -500; i--)             {                 combobox1.items.add(i.tostring());             }         }          private void setcombo(string value)         {             combobox1.items.clear();              int myindex = -1;             (int = 500; > -501; i--)             {                 myindex += 1;                  combobox1.items.add(i.tostring());                 if (i.tostring().trim() == value.trim())                 {                     combobox1.selectedindex = myindex;                 }             }         }          private void combobox1_leave(object sender, eventargs e)         {            // combobox1.selectedindex = combobox1.findstringexact(combobox1.text);             setcombo(combobox1.text);          }     } } 

i try again , still when click mouse on combo after typing in "44" , tabbing away, "449" selected instead.

because of way you're populating combobox, 449 occurs earlier in list 44, , it's selected first (it's first closest match user typing). desired behavior, you'd have have 2 lists - 1 goes 0 500 , goes 0 -500.

instead, use numericupdown box , set maximum property 500 , minimum value -500. ensure user can enter number falls within specified range.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -