【问题标题】:How to Show AutoComplete Programmatically without entering a text如何在不输入文本的情况下以编程方式显示自动完成
【发布时间】:2014-01-04 11:40:08
【问题描述】:

C# 文本框 AutoCompleteCustomSource 有一个List<string>AutoCompleteMode = Suggest.

我可以在输入字母时看到列表。

如何在不以编程方式键入字母的情况下显示整个列表?这必须在用户按下 TextBox 中的向下箭头键时完成。

是否有可用的 Win32 API?

我的解决方案

我改进了一个更好的解决方案。

在表单中添加一个ListBox 控件并使其为Visible = false

int curSelIndex = -1;

下面给出的代码将被执行Form_Load事件。

txtEmpId.AutoCompleteCustomSource.AddRange(EmpIds.ToArray());
lstAutoComplete.Items.Clear();
lstAutoComplete.Items.AddRange(EmpIds.ToArray());
txtEmpId.KeyDown += (ks, ke) =>
{
    if (!(ke.KeyCode == Keys.Down || 
          ke.KeyCode == Keys.Up || 
          ke.KeyCode == Keys.Enter)) 
    { 
        lstAutoComplete.Visible = false; 
        return; 
    }
    ke.Handled = true;
    if (ke.KeyCode == Keys.Enter)
    {
        if (lstAutoComplete.Visible)
        {
            var str = lstAutoComplete.SelectedItem + "";
            // Process the Selected Item and set to TextBox.
        }
    }
    if (!lstAutoComplete.Visible && txtEmpId.Focused)
    {
        var loc = txtEmpId.Location;
        loc.Y += txtEmpId.Height;
        lstAutoComplete.Location = loc;
        lstAutoComplete.Size = txtEmpId.Size;
        lstAutoComplete.Height = 100;
        lstAutoComplete.SelectedIndex = 0;
        curSelIndex = 0;
        lstAutoComplete.Visible = true;
    }
    else if(lstAutoComplete.Visible && txtEmpId.Focused)
    {
        if (ke.KeyCode == Keys.Down)
        {
            curSelIndex++;
            if (curSelIndex >= lstAutoComplete.Items.Count)
                curSelIndex = lstAutoComplete.Items.Count - 1;
            if (lstAutoComplete.Items.Count > 0)
                lstAutoComplete.SelectedIndex = curSelIndex;
        }
        else if (ke.KeyCode == Keys.Up)
        {
            curSelIndex--;
            if (curSelIndex < 0)
                curSelIndex = 0;
            if (lstAutoComplete.Items.Count > 0)
                lstAutoComplete.SelectedIndex = curSelIndex;
        }
    }
};
txtEmpId.Leave += (ls, le) => lstAutoComplete.Visible = false;

【问题讨论】:

标签: c# winforms textbox


【解决方案1】:

我没有找到任何API来解决你的问题,所以我只是用ListBox做了一个我自己的建议框来显示按下向下箭头键的时间,当你做其他操作时,它就会消失。我希望它对你有用。代码示例如下:

//string datasource
List<string> strList = null;
//suggestion listbox
ListBox sugBox = null;

public FrmTextSuggest()
{
    InitializeComponent();
    //setting the textbox control
    strList = new List<string>()
    {
        "USA",
        "England",
        "China",
        "Japan",
        "Korea",
        "India",
        "France",
        "Canada"
    };
    var autoCollection = new AutoCompleteStringCollection();
    autoCollection.AddRange(strList.ToArray());
    this.txtCountry.AutoCompleteCustomSource = autoCollection;
    this.txtCountry.AutoCompleteMode = AutoCompleteMode.Suggest;
    this.txtCountry.AutoCompleteSource = AutoCompleteSource.CustomSource;

    //register the Down Arrow Key event
    this.txtCountry.KeyDown += new KeyEventHandler(txtCountry_KeyDown);
}

void txtCountry_KeyDown(object sender, KeyEventArgs e)
{
    //show the your own suggestion box when pressing down arrow and the text box is empty
    if (e.KeyCode == Keys.Down && txtCountry.Text.Trim().Equals(""))
    {
        sugBox = new ListBox();
        //define the box
        sugBox.Width = txtCountry.Width;
        Point p = txtCountry.Location;
        p.Y += txtCountry.Height;
        sugBox.Location = p;
        sugBox.Items.AddRange(strList.ToArray());
        //copy the value to the textbox when selected index changed.
        sugBox.SelectedIndexChanged += new EventHandler(sugBox_SelectedIndexChanged);
        //show box
        if (sugBox.Items.Count > 0)
        {
            sugBox.SelectedIndex = 0;
            this.Controls.Add(sugBox);
            sugBox.Focus();
        }
    }
    //remove and hide your own suggestion box when other operation
    else
    {
        if (sugBox != null && this.Controls.Contains(sugBox))
        {
            this.Controls.Remove(sugBox);
            sugBox.Dispose();
            sugBox = null;
        }
    }
}

void sugBox_SelectedIndexChanged(object sender, EventArgs e)
{
    string selText = this.sugBox.SelectedItem.ToString();
    if (!string.IsNullOrEmpty(selText))
    {
        this.txtCountry.Text = selText;
    }
}

这是我的测试结果:

【讨论】:

  • 当用户第二次按下向下键时会发生什么???列表框将被重新初始化并显示???任何方式都是好主意,但必须改进..
  • 我根据您的想法添加了我的解决方案。
  • 棘手但我找到的最佳解决方案。谢谢@Scott
猜你喜欢
  • 2013-04-06
  • 2017-09-29
  • 1970-01-01
  • 2014-06-02
  • 1970-01-01
  • 1970-01-01
  • 2015-09-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多