文本框自动完成功能不能在C#WinForm应用程序中自动完成

时间:2013-06-03 08:09:51

标签: c# winforms autocomplete textbox

我在文本框上遇到这个简单功能的问题..我有一个winforms应用程序,带有一个文本框,设置为自动完成,如下所示:

if (rbSerialNumSearch.Checked)
{
       txtSerialNum.Enabled = true;
       AutoCompleteStringCollection data = new AutoCompleteStringCollection();

       //Test data
       data.Add("555-777-333");
       data.Add("222-333-444");
       data.Add("111-222-333");

       txtSerialNum.AutoCompleteCustomSource = data;

       txtSerialNum.AutoCompleteMode = AutoCompleteMode.Suggest;

       txtSerialNum.AutoCompleteSource = AutoCompleteSource.CustomSource;
}

但它不起作用。当我输入文本框时,什么都没有出现?如果我直接指定字符串集合属性(在设计模式下),它工作正常,但当我尝试以编程方式添加字符串时,没有任何事情发生?

提前致谢..

此处的格式代码:

namespace GUI
{
    public partial class UpdateEquipmentForm : Form
    {
        EquipmentManager em;
        ProductManager pm;
        CategoryManager cm;

        public UpdateEquipmentForm()
        {
            InitializeComponent();
            em = new EquipmentManager();
            pm = new ProductManager();
            cm = new CategoryManager();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (rbCategorySearch.Checked)
            {
                List<Equipment> equipments = em.GetAllEquipmentInStock().Where(eq => eq.Product.Category_Id == (int)cbChooseCategory.SelectedValue).ToList();

                var resultset = (from eq in equipments
                                 select new { eq.Product.ProductNameNum, eq.Id, eq.SerialNumber, eq.InvoiceNumber, eq.CreatedDate, eq.ExpiryDate, eq.FirstUseDate }).ToList();

                dgvResult.DataSource = resultset;
            }
            if (rbProductsSearch.Checked)
            {
                List<Equipment> equipments = em.GetAllEquipmentInStock().Where(eq => eq.Product.Id == (int)cbChooseType.SelectedValue).ToList();

                var resultset = (from eq in equipments
                                 select new { eq.Product.ProductNameNum, eq.Id, eq.SerialNumber, eq.InvoiceNumber, eq.CreatedDate, eq.ExpiryDate, eq.FirstUseDate }).ToList();

                dgvResult.DataSource = resultset;
            }
            if (rbSerialNumSearch.Checked)
            {
                List<Equipment> equipments = em.GetAllEquipmentInStock();

                var resultset = (from eq in equipments
                                 where eq.SerialNumber.Contains(txtSearchEquipment.Text)
                                 select new { eq.Product.ProductNameNum, eq.Id, eq.SerialNumber, eq.InvoiceNumber, eq.CreatedDate, eq.ExpiryDate, eq.FirstUseDate }).ToList();

                dgvResult.DataSource = resultset;
            }
        }

        private void rbCategorySearch_CheckedChanged(object sender, EventArgs e)
        {
            if (rbCategorySearch.Checked)
            {
                cbChooseCategory.Enabled = true;
                cbChooseCategory.DataSource = cm.GetAllActiveCategories();
                cbChooseCategory.DisplayMember = "Name";
                cbChooseCategory.ValueMember = "Id";
            }
            else
            {
                cbChooseCategory.Enabled = false;
            }
        }

        private void rbProductsSearch_CheckedChanged(object sender, EventArgs e)
        {
            if (rbProductsSearch.Checked)
            {
                cbChooseType.Enabled = true;
                cbChooseType.DataSource = pm.GetAllActiveProducts();
                cbChooseType.DisplayMember = "ProductNameNum";
                cbChooseType.ValueMember = "Id";
            }
            else
            {
                cbChooseType.Enabled = false;
            }
        }

        private void rbSerialNumSearch_CheckedChanged(object sender, EventArgs e)
        {
            if (rbSerialNumSearch.Checked)
            {
                txtSerialNum.Enabled = true;
                AutoCompleteStringCollection data = new AutoCompleteStringCollection();

                data.Add("555-777-333");
                data.Add("222-333-444");
                data.Add("111-222-333");

                txtSerialNum.AutoCompleteCustomSource = data;

                txtSerialNum.AutoCompleteMode = AutoCompleteMode.Suggest;

                txtSerialNum.AutoCompleteSource = AutoCompleteSource.CustomSource;
            }
            else
            {
                txtSerialNum.Enabled = false;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

发现问题..真是我的傻瓜。我引用了错误的文本框:-(我应该引用的那个名为txtSearchSerial而不是txtSerialNum。Doh!

感谢工作人员......谢谢。

答案 1 :(得分:0)

您可以将断点设置为

     txtSerialNum.Enabled = true;

我认为这个应用程序永远不会碰到这个块。

或者在此代码之后,您重新设置txtSerialNum的绑定。