AutoComplete TextBox Control

前端 未结 9 1231
误落风尘
误落风尘 2020-11-27 04:48

I want to have a textbox control that suggests and append values from a database in a Windows application with C# 2008 and LINQ.

I do it with a combobox but I can\'t

相关标签:
9条回答
  • 2020-11-27 05:28

    of course it depends on how you implement it but perhaps this is a good start:

    using System.Windows.Forms;
    
    public class AutoCompleteTextBox : TextBox {
    
        private string[] database;//put here the strings of the candidates of autocomplete
        private bool changingText = false;
    
        protected override void OnTextChanged (EventArgs e) {
            if(!changingText && database != null) {
                //searching the first candidate
                string typed = this.Text.Substring(0,this.SelectionStart);
                string candidate = null;
                for(int i = 0; i < database.Length; i++)
                    if(database[i].Substring(0,this.SelectionStart) == typed) {
                        candidate = database[i].Substring(this.SelectionStart,database[i].Length);
                        break;
                    }
                if(candidate != null) {
                    changingText = true;
                    this.Text = typed+candidate;
                    this.SelectionStart = typed.Length;
                    this.SelectionLength = candidate.Length;
                }
            }
            else if(changingText)
                changingText = false;
            base.OnTextChanged(e);
        }
    
    }
    

    I'm not sure this is working very well, but I think the base of this code is good enough.

    0 讨论(0)
  • 2020-11-27 05:30

    Check out the AutoCompleteSource, AutoCompleteCustomSource and AutoCompleteMode properties.

    textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
    textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
    AutoCompleteStringCollection col = new AutoCompleteStringCollection();
    col.Add("Foo");
    col.Add("Bar");
    textBox1.AutoCompleteCustomSource = col;
    

    Note that the designer allows you to do that without writing any code...

    0 讨论(0)
  • 2020-11-27 05:30

    You could attach to the KeyDown event and then query the database for that portion of the text that the user has already entered. For example, if the user enters "T", search for things that start with "T". Then, when they enter the next letter, for example "e", search for things in the table that start with "Te".

    The available items could be displayed in a "floating" ListBox, for example. You would need to place the ListBox just beneath the TextBox so that they can see the entries available, then remove the ListBox when they're done typing.

    0 讨论(0)
  • 2020-11-27 05:36
        private void TurnOnAutocomplete()
        {
            textBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            textBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
            AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
            string[] arrayOfWowrds = new string[];
    
            try
            {
                //Read in data Autocomplete list to a string[]
                string[] arrayOfWowrds = new string[];
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message, "File Missing", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
    
            collection.AddRange(arrayOFWords);
            textBox.AutoCompleteCustomSource = collection;
        }
    

    You only need to call this once after you have your data needed for the autocomplete list. Once bound it stays with the textBox. You do not need to or want to call it every time the text is changed in the textBox, that will kill your program.

    0 讨论(0)
  • 2020-11-27 05:41
        You can add a parameter in the query like @emailadd to be added in the aspx.cs file where the Stored Procedure is called with cmd.Parameter.AddWithValue.
        The trick is that the @emailadd parameter doesn't exist in the table design of the select query, but being added and inserted in the table.
    
        USE [DRDOULATINSTITUTE]
        GO
        /****** Object:  StoredProcedure [dbo].[ReikiInsertRow]    Script Date: 5/18/2016 11:12:33 AM ******/
        SET ANSI_NULLS ON
        GO
        SET QUOTED_IDENTIFIER ON
        GO
        ALTER procedure [dbo].[ReikiInsertRow]
        @Reiki varchar(100),
        @emailadd varchar(50)
        as
        insert into dbo.ReikiPowerDisplay
        select Reiki,ReikiDescription, @emailadd from ReikiPower
        where Reiki=@Reiki;
    
    Posted By: Aneel Goplani. CIS. 2002. USA
    
    0 讨论(0)
  • 2020-11-27 05:48
    private void textBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
                textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
                AutoCompleteStringCollection col = new AutoCompleteStringCollection();
                con.Open();
                sql = "select *from Table_Name;
                cmd = new SqlCommand(sql, con);
                SqlDataReader sdr = null;
                sdr = cmd.ExecuteReader();
                while (sdr.Read())
                {
                    col.Add(sdr["Column_Name"].ToString());
                }
                sdr.Close(); 
    
                textBox1.AutoCompleteCustomSource = col;
                con.Close();
            }
            catch
            {
            }
        }
    
    0 讨论(0)
提交回复
热议问题