简体   繁体   中英

Textbox autocomplete function not autocompleting in C# WinForm Application

Im having problems with this simple function on a textbox.. I have a winforms application, with a textbox, set up to autocomplete like this:

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

But it is not working. Nothing appears when i type in the textbox? If ii specify strings directly to the textbox collection property (in design mode), it works fine, but when i try to add strings programmatically, nothing happends?

Thanks in advance..

ENTIRE CODE FOR FORM HERE:

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

Found the problem.. Really stupid of me. I was referencing the wrong textbox :-( The one i should be referencing is called txtSearchSerial and not txtSerialNum. Doh !

Appreciate the effort guys..thanks.

You can set a break point to

     txtSerialNum.Enabled = true;

I think this application never run into this block.

Or after this code, you re-set the binding of txtSerialNum.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM