amm
amm

Reputation: 27

How can I know Text changes for textboxes without Textchanged event

In my C# Windows form MyForm I have some TextBoxes. In these TextBoxes, we have to detect if the TextChanged event occurs, if there're changes in these TextBoxes and click close button, it will ask if we want to cancel the changes when we close the form. However, when I run the MyForm, I can't know text change for each textbox caused by user typing for without textchanged event property. But I am thinking how do I make the TextBox's TextChanged know the event cuased by user typing without textchanged event? Thanks for help. Sorry for my English.

Upvotes: 1

Views: 9363

Answers (3)

befree2j
befree2j

Reputation: 371

Why dont you use a timer which will check after a few intervals if the textboxes do contain any text

Upvotes: 0

user1693593
user1693593

Reputation:

There is no (decent) way of knowing what's typed without a TextChanged or a Leave event.

You need to use one of these events to get the typed content. Doing this enable you to set a "dirty" flag that you can check at close and clear at save.

Comparing old and new value has no point without this cause you won't know what the value should be set to without knowing something was changed.

With one exception: If your original data came from a database you could use the compare old/new approach as you would compare the textbox of that which came from the database.

Update:

Addressing this comment:

"Because Myform have many textboxes and if no text change ,this will not display the confirm message.If I catch textchanged event for all textboxes, this is so many code."

You can use a common handler to collect the changes for all textboxes in one single method. Use the sender object (cast it to Textbox) to identify which textbox is changed, if needed, or simply set a dirty flag for whatever textbox has a change.

bool isDirty = false;

void SomeInitMethod() //ie. Form_Load
{
    textbox1.TextChanged += new EventHandler(DirtyTextChange);
    textbox2.TextChanged += new EventHandler(DirtyTextChange);
    textbox3.TextChanged += new EventHandler(DirtyTextChange);
    //...etc
}
void DirtyTextChange(object sender, EventArgs e)
{
    isDirty = true;
}
void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    if (isDirty) {
        //ask user
    }
}

// to clear
void Save()
{
    SaveMyDataMethod();
    isDirty = false;
}

If you have a lot of textboxes in the form loop through the forms control collection and use typeof to address the textboxes. If you have textboxes requiring different approaches use the Tag property of the textbox to distinguish.

Upvotes: 4

user586399
user586399

Reputation:

A possible approach is using the timer. Have a timer that ticks every 1000 ms (say) and checks the textBox.Text.

A second possible approach is overriding WndProc for the textbox (by inheriting a new class) and handling the change text message. This would be the same as overriding TextBox.OnTextChanged.

Upvotes: 0

Related Questions