Click here to Skip to main content
15,170,621 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I'v written the code below for TextChanged event of a comboBox , but when I try to write something it just writes it vice versa(For example : when I type google it writes elgoog ), how can I make it right?
code:


C#
combo.Items.Clear();
foreach (string a in Addresses)
    if (a.Contains(combo.Text))
        combo.Items.Add(a);
combo.DroppedDown = true;




Thanks
Posted
Updated 5-Nov-11 6:53am
v2

Solution 1 is a good suggestion although it will not fix the problem with the text written backwards you need to move the text caret to the end of the line using some thing like combo.SelectionStart = combo.Text.Length; as shown below.

C#
private void comboBox1_TextChanged(object sender, EventArgs e)
{
    string[] Addresses = { "http://google.com/", "http://www.yahoo.com/" };

    comboBox1.Items.Clear();
    foreach (string a in Addresses)
        if (a.Contains(combo.Text))
            combo.Items.Add(a);
    combo.DroppedDown = true;

    combo.SelectionStart = combo.Text.Length;
}


Some code that might help you figure something out with selecting using keyboard:

C#
List<int> matchingIndexes = new List<int>();
int index = 0;
bool selecting = false;
private void comboBox1_TextChanged(object sender, EventArgs e)
{
    string[] Addresses = { "http://google.com/", "http://www.yahoo.com/" };

    if (!selecting)
    {
        comboBox1.Items.Clear();
        matchingIndexes.Clear();
        foreach (string a in Addresses)
        {
            if (a.Contains(comboBox1.Text))
            {
                comboBox1.Items.Add(a);
                matchingIndexes.Add(comboBox1.Items.IndexOf(a));
            }
        }
        comboBox1.DroppedDown = true;

        comboBox1.SelectionStart = comboBox1.Text.Length;
    }
}

private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Down)
    {
        selecting = true;
        if (index > matchingIndexes.Count - 1)
        {
            index = matchingIndexes.Count - 1;
        }
        else if (index < 0)
        {
            index = 0;
        }
        comboBox1.SelectedIndex = matchingIndexes[index++];
    }
    else if (e.KeyCode == Keys.Up)
    {
        selecting = true;
        if (index < 0)
        {
            index = 0;
        }
        else if (index > matchingIndexes.Count - 1)
        {
            index = matchingIndexes.Count - 1;
        }
        comboBox1.SelectedIndex = matchingIndexes[index--];
    }
}

private void comboBox1_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up)
    {
        selecting = false;
    }
}
   
v6
Comments
mehdi_k 5-Nov-11 14:38pm
   
it worked, thanks
but after I find my item in the list can I select it with key up or down? Is it possible without mouse?
LanFanNinja 5-Nov-11 15:44pm
   
Recheck my solution for some code that might help you figure something out.
mehdi_k 6-Nov-11 5:11am
   
The index goes out of range. but I will try more to make it right
Thanks for your help.
LanFanNinja 6-Nov-11 10:38am
   
I'm sorry. Please note the changes in my solution above. matchingIndexes.Clear() in the TextChanged event handler, and the changes to the if statements in the KeyDown event handler. Hopefully you will see this and it works this time. :) Let me know if it does or doesn't.
Instead of using the Add method, use Insert instead:
C#
combo.Items.Clear();
foreach (string a in Addresses)
    if (a.Contains(combo.Text))
        combo.Items.Insert(0, a);
combo.DroppedDown = true;
This way the later items are always at the front.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900