-
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;반응형'닷넷 > WinForms' 카테고리의 다른 글
C# 컨트롤 사이즈에 맞춰서 폰트 크기 자동조절 (0) 2016.08.12 C# 폼 이동 방법 (0) 2016.07.19 C# WindowsForm에서 배경 투명하게 하는 방법 (0) 2016.07.19 C# ComboBox 아이템 추가 (0) 2016.07.05 C# Textbox를 mssql에 저장 (0) 2016.07.05