是否有可能只在textBox聚焦时才发生textchanged事件?

时间:2013-05-13 03:47:38

标签: c# events textbox textchanged

我有这样的方法。

    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才有可能发生吗?反之亦然。

谢谢:)

1 个答案:

答案 0 :(得分:3)

如果TextChanged没有集中注意力,请尝试忽略NametextBox事件,如下所示:

private void NametextBox_TextChanged(object sender, EventArgs e)
{
    if (NametextBox.IsFocused)
    {
        CheckBook();
        EmployeetextBox.Clear();
        AddresstextBox.Clear();
    }
}