edit: Solved
I have a label that gets populated with a value from a database. If the user enters this value into a textbox below, I want to change the background. The label displays the value fine on screen, but when I try to match the values in the textbox's textchanged event, it shows as null.
public void button1_Click(object sender, RoutedEventArgs e)
{
txtAnswer.Clear();
txtAnswer.Background = Brushes.White;
int number = r.Next(3) + 1;
string queryEnglish = "SELECT englishVerb FROM verbTable WHERE (verbID = " + number + ")";
string queryFrench = "SELECT frenchVerb FROM verbTable WHERE (verbID = " + number + ")";
using (SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\verbs.mdf;Integrated Security=True;User Instance=True"))
{
con.Open();
using (SqlCommand command = new SqlCommand(queryEnglish, con))
{
this.lblEnglishVerb.Content = (string)command.ExecuteScalar();
}
using (SqlCommand command = new SqlCommand(queryFrench, con))
{
this.lblFrenchVerb.Content = (string)command.ExecuteScalar();
}
}
}
public void txtAnswer_TextChanged(object sender, TextChangedEventArgs e)
{
if (txtAnswer.Text == lblFrenchVerb.Content.ToString())
txtAnswer.Background = Brushes.LightGreen;
开发者_JS百科 if (txtAnswer.Text == "test")
txtAnswer.Background = Brushes.AliceBlue;
}
Textchanged will probably get triggered the moment 'nothing' is placed into the Content. So on the first txtAnswer_TextChanged you might get nothing.
精彩评论