什麼,我從你的問題的理解,你想創建一個自動完成像谷歌網站確實,是嗎?在這種情況下,我寫了示例代碼,此代碼的工作方式類似於auto complete,它會自動填充存儲在數據庫中的剩餘方框。
Manager類:
private static string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database/Health.accdb";
public static void AutoComplete(AutoCompleteStringCollection _collections)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
string query = "SELECT DISTINCT [Description] FROM [Database] ORDER BY [Description] ASC";
connection.Open();
using (OleDbCommand command = new OleDbCommand(query, connection))
using (OleDbDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
_collections.Add(reader["Description"].ToString());
}
reader.Close();
}
connection.Close();
}
}
public static void GetData(TextBox _windowsTextBox1, TextBox _windowsTextBox2, TextBox _windowsTextBox3, NumericUpDown _numericUpDown1)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
string query = "SELECT [ProductCode], [Description], [Quantity], [Price] FROM [Database] WHERE [Description] = @Description";
connection.Open();
using (OleDbCommand command = new OleDbCommand(query, connection))
{
command.Parameters.Add("@Description", OleDbType.VarChar);
command.Parameters["@Description"].Value = _windowsTextBox1.Text;
using (OleDbDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
string productCode = (string)reader["ProductCode"];
string description = (string)reader["Description"];
UserInformation.Description = description;
int quantity = (int)reader["Quantity"];
int price = (int)reader["Price"];
_windowsTextBox2.Text = productCode;
_windowsTextBox3.Text = Convert.ToString(price);
_numericUpDown1.Maximum = Convert.ToDecimal(quantity);
}
reader.Close();
}
}
connection.Close();
}
}
實例形式:
private void AutoComplete()
{
AutoCompleteStringCollection _dataCollections = new AutoCompleteStringCollection();
Manager.AutoComplete(_dataCollections);
textBox2.AutoCompleteMode = AutoCompleteMode.Suggest;
textBox2.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox2.AutoCompleteCustomSource = _dataCollections;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
Manager.GetData(this.textBox2, this.textBox1, this.textBox3, this.numericUpDown1);
}
這裏是結果:
五月這個答案會幫助你!
乾杯!
加載標籤的代碼在哪裏? – andy