ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • C# TextBox 자동 완성
    프로그래밍/C# 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; 

     


    '프로그래밍 > C#' 카테고리의 다른 글

    C# Resources에서 이미지 파일 불러오기  (0) 2016.07.19
    C# string을 DateTime으로 변환  (0) 2016.07.05
    C# ComboBox 아이템 추가  (0) 2016.07.05
    C# TextBox 자동 완성  (0) 2016.07.05
    C# MSSQL 연결  (0) 2016.07.05
    C# Textbox를 mssql에 저장  (0) 2016.07.05

    댓글 0

Designed by Tistory.