Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1878
Strange behavior when scrolling with cursor keys in WinComboEditor
posted

I've an issue with WinComboEditor that cost me at least 6 hours to find out where the problem resides. Fortunately I was able to reduce it to a very small code snippet I can present here.

I'm populating the WinComboEditor with a list of business objects, set the display member of WinComboEditor to the property I want to display in the Combo. When I scroll with the cursor keys the first time I scroll down nothing is updated in the text area of the combo. When I scroll down again the next value is displayed but it is always one value behind. The value on the bottom cannot be selected by keyboard at all. See screenshot below for illustration of the problem:

The source code for that example is depicted below:

public partial class Form3 : Form
{
   private readonly List<Country> _businessObjects = new List<Country> { new Country("United States"), new Country("Austria"), new Country("United Kingdom"), new Country("France") };

   public Form3()
   {
      InitializeComponent();
   }

   private void OnFormLoad(object sender, EventArgs e)
   {
      cmbNames.DataSource = _businessObjects;
      cmbNames.DisplayMember = "Name";
   }

   public class Country
   {
      public string Dummy
      {
         get;
         set;
      }

      public string Name
      {
         get;
         set;
      }

      public Country(string name)
      {
         Name = name;     
      }
   }
}

What I've found out next is that the property "Dummy" in the Country object causes the problem. If I remove it, the scrolling works fine, if I e.g. put it after the "Name" property it's also working fine. The problem only occurs when it is in front of the property to be displayed i.e. the "Name" property in that example.

Further investigations lead to the result that when initializing Dummy with a value the behavior was also correct. Also when I set the value member to same property as display member it worked also fine so it seems for me that it has to do something with the value member of WinComboEditor that is taken from first property in the business object if not specified explicitly and if this is not initialized the behavior is incorrect.

What is the correct way of using it if the business object has no value member (identifier) to be used? Set it to the same property as the display member?

Regards, Wolfgang

  • 469350
    Verified Answer
    Offline posted

    Hi Wolfgang,

    The problem is that you are setting the DisplayMember, but not the ValueMember. ValueMember is therefore defaulting to the first field in the data, which is the Dummy column and since no object in the list has any value for the Dummy column, the Combo is getting confused because every item you select has exactly the same value - they are all null.

    If you set ValueMember to "Name" instead of, or in addition to, setting the DisplayMember, it works fine.