ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • C# TextBox 자동 완성
    닷넷/WinForms 2016. 7. 5. 13:46
    반응형

    1. Collection Add


    AutoCompleteStringCollection autoCollection = new AutoCompleteStringCollection();


    // Collection 하나씩 추가

    autoCollection.Add("Collection1");

    autoCollection.Add("Collection2");

    autoCollection.Add("Collection3");


    //Collection 배열로 추가

    autoCollection.AddRange(new string[]

    {

    Collection1,

    Collection2,

    Collecion3

    });


    textBox1.AutoCompleteCustomSource = autoCollection;

    textBox1.AutoCompleteMod = AutoCompleteMod.SuggestAppend;

    textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;




    2. DB에 있는 것으로 채우기


    private void Form1_Load(object sender, EventArgs e)
            {
                AutoCompleteStringCollection autoCollection1 = new AutoCompleteStringCollection();
                AutoCompleteStringCollection autoCollection2 = new AutoCompleteStringCollection();


                //comboBox 외박사유
                SqlDataAdapter sqlDataAdapterSayu = new SqlDataAdapter("select memo from sayu", conn);
                SqlCommandBuilder sqlCommandBuilderSayu = new SqlCommandBuilder(sqlDataAdapterSayu);

                DataTable dataTableSayu = new DataTable();
                sqlDataAdapterSayu.Fill(dataTableSayu);
                BindingSource bindingSorceSayu = new BindingSource();
                bindingSorceSayu.DataSource = dataTableSayu;

                comboBox1.DataSource = bindingSorceSayu;
                comboBox1.ValueMember = "memo";
                comboBox1.DisplayMember = "memo";



                // TextBox 자동채우기
                   SqlDataReader reader;
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "select UserName, PersonID from Users";

               
                conn.Open();

                reader = cmd.ExecuteReader();

                if (reader.HasRows == true)
                {
                    while (reader.Read())
                    {
                        autoCollection1.Add(reader["UserName"].ToString());
                        autoCollection2.Add(reader["PersonID"].ToString());
                    }
                }


                reader.Close();


                // textBox2 이름
                textBox2.AutoCompleteMode = AutoCompleteMode.Suggest;
                textBox2.AutoCompleteSource = AutoCompleteSource.CustomSource;
                textBox2.AutoCompleteCustomSource = autoCollection1;

               

                //textBpx1 방번호
                textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
                textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
                textBox1.AutoCompleteCustomSource = autoCollection2; 

     


    반응형

    댓글

Designed by Tistory.