简体   繁体   中英

Odd error in TextChanged Event in C#

So I have a combobox:

<ComboBox IsEditable="True" TextBoxBase.TextChanged="textBox_Changed"/>

When the text in the combobox changes, it fires off the textBox_changed function:

private void textBox_Changed(object sender, RoutedEventArgs e)
{
    LinearGradientBrush gradient = new LinearGradientBrush(Colors.MistyRose, Colors.SandyBrown, new Point(0, 0), new Point(0, 1));
    this.saveButton.Background = gradient;
}

What this function does is change the background color of a button elsewhere on the window. So far this works perfectly. The issue appears when I try to use this same function with a TextBox instead of a Combobox:

<TextBox TextChanged="textBox_Changed"/>

When I run the application, I get the following error:

Object reference not set to an instance of an object.

Very bizarre error that I can't figure out.

Issue solved! The event was getting triggered when the form was initializing, but saveButton was not initialized yet. I removed the default values of the field and all is well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM