Reputation: 2989
I have method like this.
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;
}
}
}
}
And I have to write a code something like this.
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();
}
The problem is NametextBox_TextChanged triggers also when i input in EmployeeIDtextBox because the method gets a value from database and display it NametextBox.
Is it possible for NametextBox_TextChanged to occur only when NametextBox is focused? And vice versa.
Thank you :)
Upvotes: 3
Views: 1042
Reputation: 8938
Off the top of my head, try simply ignoring the TextChanged
event if NametextBox
is not focused - something like this:
private void NametextBox_TextChanged(object sender, EventArgs e)
{
if (NametextBox.IsFocused)
{
CheckBook();
EmployeetextBox.Clear();
AddresstextBox.Clear();
}
}
Upvotes: 3