3

我有这样的方法。

    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_TextChanged 是否可能仅在 NametextBox 聚焦时发生?反之亦然。

谢谢 :)

4

1 回答 1

3

如果没有集中注意力,请尝试简单地忽略TextChanged事件NametextBox- 像这样:

private void NametextBox_TextChanged(object sender, EventArgs e)
{
    if (NametextBox.IsFocused)
    {
        CheckBook();
        EmployeetextBox.Clear();
        AddresstextBox.Clear();
    }
}
于 2013-05-13T04:00:50.690 回答