After selecting selectedindex, click the control with the mouse, select the wrong item

advertisements

The engineers requested a combobox with numbers from 500 to -500 (negative numbers listed at the bottom, so not alphabetical).

They also requested being able to type in the number in the combo to jump to correct item.

Problem: type in "44", tab away. Then click on the control with the mouse and you can see "449" is selected.

Here is the 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)
        {
            for (int i = 500; i > -500; i--)
            {
                comboBox1.Items.Add(i.ToString());
            }
        }

        private void comboBox1_Leave(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = comboBox1.FindStringExact(comboBox1.Text);
        }
    }
}

No problem! I tell myself. FindStringExact is finding the first alpha match. So I replace the Leave event code with a 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)
        {
            for (int i = 500; i > -500; i--)
            {
                comboBox1.Items.Add(i.ToString());
            }
        }

        private void setCombo(string value)
        {
            comboBox1.Items.Clear();

            int myindex = -1;
            for (int i = 500; i > -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 it again and still when I click the mouse on the combo after typing in "44" and tabbing away, "449" is selected instead.


Because of the way you're populating the ComboBox, 449 occurs earlier in the list than 44, and so it's selected first (it's the first closest match to what the user is typing). To get the desired behavior, you'd have to have two lists - one that goes from 0 to 500 and another that goes from 0 to -500.

Instead, use a NumericUpDown box and set the Maximum property to 500 and the Minimum value to -500. That will ensure the user can only enter a number that falls within the specified range.