Check if the text field text has been changed since the form was opened

Howdy
I have a form in my application that contains 7 text fields. In the form constructor, I run the "MakeText ()" method, which inserts the values ​​from MySqlDataReader into the text fields.

The user can change the values ​​in these text files and when you click the "Submit" button, he will start the update request in the database to record all the changes made.
Everything is in order, but I believe that spending resources on querying the database, even if the user has not changed the text in the text blocks.

So, I need a way to check if the user has changed any values ​​in the text fields after loading the form.
I would like to do this at the application level, rather than letting it process the database.

How can i do this?

EDIT: I just thought about that. Perhaps I could put all the values ​​in the class lines when the form loads, and then just check them before running the request. Sometimes you just need to put the question in words to think about the answer: D

This is the best I could come up with ...

+4
source share
8 answers

You can save the initial value in the .tag value of the text field, and then simply compare the current text and the tag text

+6
source

You can try something like this.

public bool TextWasChanged = false; protected void form1_load() { MakeText(); textBox1.TextChanged += new System.EventHandler(this.textBox_TextChanged); textBox2.TextChanged += new System.EventHandler(this.textBox_TextChanged); ... ... ... } private void textBox_TextChanged(object sender, EventArgs e) { TextWasChanged = true; } void UpdateDB() { if(TextWasChanged) { // Update DB } } 
+5
source

you can have a boolean flag associated with each of your text fields and use the textchanged event of the text field when this event fires, this means that the text in the sender text field has changed, so you must set the flag to show what the text is for this text the field has changed. * if you want yoiu to use textbox.tag instead of the specified flag

 private void textBox_TextChanged(object sender, EventArgs e) { ((TextBox)sender).tag = "true" ; //you can set whatever you want here to show text has changed } 
+2
source

you can create a field

 bool test = false; 

and then check the TextChaged event for each TextBox.

This event sets test = true;

then send click, check if test = true if this is a database update

+1
source

Create a class with a property for each text field. Within this class, the logic necessary to determine whether changes have been made is implemented.

Then use data binding to bind text fields to an instance of your class.

+1
source

Here is a quick solution based on my comment, setup in linqpad (maybe you don't need a general one)

 void Main() { var test = new tester() { val = "123" }; var changeTest = new ObjectHasChanged<tester>(test,t=>t.val); changeTest.HasChanged.Dump(); //false test.val = "321"; changeTest.HasChanged.Dump(); //true } public class tester { public string val { get; set; } } public class ObjectHasChanged<T> { public bool HasChanged { get { return !this.initvalue.Equals(this.valueExpression(this.obj)); } } private object initvalue {get; set;} private Func<T,object> valueExpression { get; set; } private T obj { get; set; } public ObjectHasChanged(T obj, Func<T,object> valueExpression) { this.obj = obj; this.valueExpression = valueExpression; this.initvalue = valueExpression(this.obj); } } 

You would say something like:

 var MyTextBoxChangeTest = new ObjectHasChanged<Textbox>(this.myTextBox,t=>t.Text); 
0
source

Take a look:

 Private Sub cmdTutup_Click() If mbEditFlag = True Then If MsgBox("Apakah perubahan akan disimpan ", 4 + 32 + 256, "Perhatian") = vbYes Then sqltxt = "update tbproduk set kduser='" & vkduser & _ "',margin1=" & Val(fgproduk.TextMatrix(brsditinggaklan, 4)) & ",margin2=" & Val(fgproduk.TextMatrix(brsditinggaklan, 5)) & ",margin3=" & Val(fgproduk.TextMatrix(brsditinggaklan, 6)) & ",margin4=" & Val(fgproduk.TextMatrix(brsditinggaklan, 7)) & ",margin5=" & Val(fgproduk.TextMatrix(brsditinggaklan, 8)) & _ ",hrgjual1=" & Val(fgproduk.TextMatrix(brsditinggaklan, 9)) & ",hrgjual2=" & Val(fgproduk.TextMatrix(brsditinggaklan, 10)) & ",hrgjual3=" & Val(fgproduk.TextMatrix(brsditinggaklan, 11)) & ",hrgjual4=" & Val(fgproduk.TextMatrix(brsditinggaklan, 12)) & ",hrgjual5=" & Val(fgproduk.TextMatrix(brsditinggaklan, 13)) & _ ",discjual=" & Val(fgproduk.TextMatrix(brsditinggaklan, 14)) & ",qty1=" & Val(fgproduk.TextMatrix(brsditinggaklan, 15)) & ",qty2=" & Val(fgproduk.TextMatrix(brsditinggaklan, 16)) & ",qty3=" & Val(fgproduk.TextMatrix(brsditinggaklan, 17)) & _ " where kdproduk='" & fgproduk.TextMatrix(brsditinggaklan, 1) & "'" conn.Execute sqltxt, , adCmdText End If mbEditFlag = False End If Unload Me End Sub 
-1
source
 Private Sub cmdTutup_Click() If mbEditFlag = True Then If MsgBox("Apakah perubahan akan disimpan ", 4 + 32 + 256, "Perhatian") = vbYes Then sqltxt = "update tbproduk set kduser='" & vkduser & _ "',margin1=" & Val(fgproduk.TextMatrix(brsditinggaklan, 4)) & ",margin2=" & Val(fgproduk.TextMatrix(brsditinggaklan, 5)) & ",margin3=" & Val(fgproduk.TextMatrix(brsditinggaklan, 6)) & ",margin4=" & Val(fgproduk.TextMatrix(brsditinggaklan, 7)) & ",margin5=" & Val(fgproduk.TextMatrix(brsditinggaklan, 8)) & _ ",hrgjual1=" & Val(fgproduk.TextMatrix(brsditinggaklan, 9)) & ",hrgjual2=" & Val(fgproduk.TextMatrix(brsditinggaklan, 10)) & ",hrgjual3=" & Val(fgproduk.TextMatrix(brsditinggaklan, 11)) & ",hrgjual4=" & Val(fgproduk.TextMatrix(brsditinggaklan, 12)) & ",hrgjual5=" & Val(fgproduk.TextMatrix(brsditinggaklan, 13)) & _ ",discjual=" & Val(fgproduk.TextMatrix(brsditinggaklan, 14)) & ",qty1=" & Val(fgproduk.TextMatrix(brsditinggaklan, 15)) & ",qty2=" & Val(fgproduk.TextMatrix(brsditinggaklan, 16)) & ",qty3=" & Val(fgproduk.TextMatrix(brsditinggaklan, 17)) & _ " where kdproduk='" & fgproduk.TextMatrix(brsditinggaklan, 1) & "'" conn.Execute sqltxt, , adCmdText End If mbEditFlag = False End If Unload Me End Sub 
-1
source

Source: https://habr.com/ru/post/1341459/


All Articles