简体   繁体   中英

TextChanged event firing as soon as I open the app

So I am making an app in C# where it creates/edits existing .ini files. One of the features I am trying to add is that if I make changes to the .ini file via the c# app I created, and either try to close the app, open another .ini file or create a new file, it should prompt the user if they want to save the file. To accomplish this, I have a flag called dataChanged . In the TextChanged events in for the multiple textboxes, I set dataChanged = true; since changes were made to the file. However, for some reason as soon as I open the app, all the TextChange events fire up so even if I don't enter any values in the various textboxes, when I close the app, it prompts me to save the file (it shouldn't!). 应用 AUI 的一部分

App UI:

User inputs text in the textboxes.

Part of code regarding the 4 textboxes:

 private void TextBox_TextChanged(object sender, TextChangedEventArgs e) //ifrs installer
        {

            dataChanged = true;

        }

        private void TextBox_TextChanged_1(object sender, TextChangedEventArgs e) //ifrs patchfile
        {

            dataChanged = true;
        }


        private void textBox3_TextChanged(object sender, TextChangedEventArgs e) 
        {

            dataChanged = true;
        }

        private void textBox4_TextChanged(object sender, TextChangedEventArgs e) 
        {

            dataChanged = true;
        }

TextChanged event fires even when you set some Text. Apparently you set some initial text when app is loading.

You can subscribe to the event mannually after you set the initial value.

textBox4.TextChanged += textBox4_TextChanged;

or unsubscribe before you set the value and subscribe after that.

textBox4.TextChanged -= textBox4_TextChanged;

textBox4.Text = "Initial Value";

textBox4.TextChanged += textBox4_TextChanged;

Sounds to me like you're programmatically setting the textBoxN.Text properties.

What you might want to do is add an if (appInitialized) around your dataChanged = true; and only set appInitialized to true after the application is loaded, perhaps in your Form_Load event. This way, the initial loading doesn't set your variable. Another option is to only register the TextChanged event after you have already set the initial values. My guess is you registered the event using the designer and as a result it's firing for those initial settings because of where the designer adds event registration. Instead do the

textBox4.TextChanged += textBox4_TextChanged;
// Etc. for each text box

yourself after the .Text properties are set. Again, perhaps in your Form_Load .

I'm guessing you are loading the ini file when the program loads and this triggers to text changed event. I suggest doing something like this.

    private void Form1_Load(object sender, EventArgs e)
    {
        LoadData();
    }
    private bool _LoadingData = false;
    private bool _DataChanged = false;
    private void LoadData()
    {
        try
        {
            _LoadingData = true;
            // Load data
        }
        finally
        {
            _LoadingData = false;
        }
    }
    public void DataChanged()
    {
        if (_LoadingData == false)
        {
            _DataChanged = true;
        }
    }
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        DataChanged();
    }

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