简体   繁体   中英

Check if text changes in textbox in VB.NET

Ok, so many examples out there but they are not what I am looking for.

My VB.NET app loads up and reads from a XML file to populate a text box. Then, the user has the ability to change the text and save it. BUT if that user goes and does something else, like changes a selection in a drop down box, I want it to pop a dialog that warns them that the text was not saved and give them the option to Say discard changes, save changes, etc.

Any ideas on how to best implement this? Do I create hidden text boxes and just compare them? I feel like I am over engineering something that can be done very easily.

Thanks in advance.

[UPDATE] - Since the text is a small quantity, I am going with two text boxes and then compare the results but here is an issue I am having with the MsgBox. No matter what, it always thinks I clicked Yes. thoughts or should this go to a new Q?

Here is my test code:

   Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    If txtDetails.Text <> txtDetailsCOPY.Text Then
        MsgBox("You changed text." & vbCrLf & "To save it, click  YES" & vbCrLf & "To discard the changes, click  NO.", MsgBoxStyle.YesNo, "Text Changed!")
        If MsgBoxResult.Yes Then
            MsgBox("You clicked YES")
        ElseIf MsgBoxResult.No Then
            MsgBox("You clicked NO")
        End If
    ElseIf txtDetails.Text = txtDetailsCOPY.Text Then
        'MsgBox("Nothing changed!")
        Exit sub
    End If
End Sub

You can add a boolean flag say, textNotSaved as a flag. In your textbox's text_changed event, you can then set the flag to true. Whenever the text is saved, you can set it to False. And whenever any other event occurs, say for example, the click event on a button, check if the flag is true; if it is, then prompt the user, for example, with the help of a message box. Hope this helps.

I would defer the decision to save until the user hits the Cancel or OK button on the dialog containing the text box. If that is not possible, then make the text box read-only, provide an edit button, and open a modal dialog to edit the text box.

The problem with message-boxing a user when they leave the text box is that it is extremely disruptive. Message boxes are the most expensive possible cognitive element from a user perspective; they represent a complete interruption. Everything stops while the user evaluates what to do about the message box. This is why users often ignore them, and dismiss them without evaluating them properly, even when they are important. So don't use them for this.

If you have more than one text box, consider implementing "dirty tracking." https://www.google.com/search?q=form.dirty+winforms

In general you will have a model IE an in memory representation of the data you are displaying. When the text box is updated it sets a property in that model. The model then know that it changed by setting a dirty flag or my keeping a copy of the original model. When you go to load a new model you check what ever mechanism you have created to nofity you that the previous model has not been saved. Then you pop up you dialog. Do not use the text boxes as data stores.

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