开发者

How to workaround C# sorting error in Combobox when AutoCompleteMode is Suggest?

There is a sorting error in C# in Combobox when using AutoCompleteMode with mode Suggest and AutoCompleteSource is ListItems.

Example: Combobox contains items: "Svedberg", "Swedbank", "Swedish"

When typing "Sw" in Combobox I should get two items suggested, "Swedbank" and "Swedish". Problem is that only "Swedbank" is shown. It seems that C# sorts the items as: "Swedbank", "Svedberg", "Swedish"

If I could C# to use StringComparer.Ordinal it would solve the problem, since Ordinal sorting seems to work better.

Any ideas on how to solve this problem?

System.Windows.Forms.ComboBox comboBox1;
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.comboBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
this.comboBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Items.AddRange(new object[] {
        "Svedberg",
        "Swedbank",
        "Swedish"});
this.comboBox1.Location = new System.Drawing.Point(142, 474);
this.comboBox1.Name = "c开发者_如何学编程omboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 21);
this.Controls.Add(this.comboBox1);


This code works for me:

        System.Windows.Forms.ComboBox comboBox1;
        comboBox1 = new System.Windows.Forms.ComboBox();
        comboBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
        comboBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
        comboBox1.FormattingEnabled = true;
        comboBox1.Items.AddRange(new object[] {
    "Svedberg",
    "Swedbank",
    "Swedish"});
        comboBox1.Location = new System.Drawing.Point(0, 0);
        comboBox1.Name = "comboBox1";
        comboBox1.Size = new System.Drawing.Size(121, 21);
        Controls.Add(comboBox1);

The only thing I did is removing the this reference.

Regards, M.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜