我有这样的方法。
public void CheckEmployee()
{
ConnectionStringSettings myConnectionString = ConfigurationManager.ConnectionStrings["LibrarySystem.Properties.Settings.LibraryConnectionString"];
using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
{
myDatabaseConnection.Open();
using (SqlCommand mySqlCommand = new SqlCommand("Select * from Employee where EmployeeID = '" + EmployeeIDtextBox.Text + "' ", myDatabaseConnection))
using (SqlDataReader sqlreader = mySqlCommand.ExecuteReader())
{
if (sqlreader.Read())
{
string EmployeeID = sqlreader.GetInt32(0).ToString();
string Name = sqlreader.GetString(1);
string Address = sqlreader.GetString(2);
EmployeeIDtextBox.Text = EmployeeID;
NametextBox.Text = Name;
AddresstextBox.Text = Address;
}
}
}
}
我必须编写类似这样的代码。
private void EmployeeIDtextBox_TextChanged(object sender, EventArgs e)
{
CheckBook();
NametextBox.Clear();
AddresstextBox.Clear();
}
private void NametextBox_TextChanged(object sender, EventArgs e)
{
CheckBook();
EmployeetextBox.Clear();
AddresstextBox.Clear();
}
当我在EmployeeIDtextBox中输入时,问题是NametextBox_TextChanged触发器因为该方法从数据库获取值并将其显示为NametextBox。
只有当NametextBox被聚焦时,NametextBox_TextChanged才有可能发生吗?反之亦然。
谢谢:)
答案 0 :(得分:3)
如果TextChanged
没有集中注意力,请尝试忽略NametextBox
事件,如下所示:
private void NametextBox_TextChanged(object sender, EventArgs e)
{
if (NametextBox.IsFocused)
{
CheckBook();
EmployeetextBox.Clear();
AddresstextBox.Clear();
}
}