反之亦然 [英] combo box writes vice versa

查看:105
本文介绍了反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我为comboBox的TextChanged事件编写了以下代码,但是当我尝试编写某些内容时,它只会将其写成相反的内容(例如:当我键入google时,它会写成elgoog),我该如何正确设置? /> 代码:


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:


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





感谢





Thanks

推荐答案

解决方案1是一个很好的建议,尽管它不能解决文本向后书写的问题,您需要使用像combo.SelectionStart = combo.Text.Length;之类的东西如下图所示.

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.

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:

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;
    }
}


而不是使用Add方法,而是使用Insert:
Instead of using the Add method, use Insert instead:
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.


这篇关于反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆