last day (15 days later) » 

4:15 PM
1
A: How do I add the tag value and the text value of multiple TextBox, only when the text has changed in WPF?

JauchI think a good solution is, first, to put your dictionary somewhere else (a private in the form, maybe)., and populate it when showing the form. This will allow you to update the dictionary when the text box is changed. Assuming this, I would create a single method to be called every time the us...

 
But how do I actually check if the text is changed? That would still be the issue tha I am will be facing. I am assuming there is somthing that I need to do with the TextChanged event handler, and my if statement above needs to find it.
 
Every time you press a key and CHANGE the text in a text box, the TextChanged event will fire. In the end, the user can let the text as the original, and you will have made something everytime. If you let the verification just to when the user "leaves" the code, it will be done only once.
I'll add some code to the answer, to show how you can check to see if the text changed.
 
Great! One thing I see that may be a problem here is that every time the user leaves a textbox, if it writes the tag and text to the dictionary, if the user goes back and reedits the textbox, that dictionary entry will not be overwritten, so I may end up with too many entries. The ultimate goal is to use the dictionary to poulate a database table with Tag = the heading and Text = the value to be input under the heading.
 
@antman1p, no. If you look to the code, if the TAG already exists, the value associated will be UPDATED. In any case, if you try to ADD a TAG into a Dictionary<>, and the tag already exists, it would throw an exception.
 
I just saw that you covered that in the code. Thank you!
 
4:15 PM
Don't need to thank, just upvote and set as the answer if you think it solves your problem ;) hehe
 
would the be the "TouchLeave" Event?
Cool, will do. Can you upvote my question please?
 
Can be... There are any other "Leave" event?
 
DragLeave
 
Nope
 
It is a WPF page
 
4:16 PM
Let me take a look. Just a moment
 
LostFocus?
 
Ok.
Probably you will have to use more than one.
Because there are more than one way to "leave"
I'm looking into it to be sure
 
I'm thinking SelectionChanged?
 
No.
Try two. LostFocus first.
The event will be called EVERY time you leave, but this is not a problem.
I'm looking into another option
 
ok.
I'm thinking that it may be easier to do it how I had it, but just check to see if the TextChanged for each TextBox after the submit button is clicked.
 
4:23 PM
Here you have a list of all events:
And a very nice explication on all of them.
Depending on how the text will be set, like for example, with copy paste, etc, maybe the lost focus will not be enough
You can try, for example, the "KeyboardLostFocus"
 
Right.
Seems like a lot of unecasary event handling to do it that way.
 
I think the correct name is LostKeyboardFocus
Yes.
You can use TextChanged also.
It could be simplier
But it isn't, because for example, you can change a lot and finish letting the text exactly as it was. And to know that, you have to use even more logic
 
Plkus I noticed that your code only checks if the textbox.Text is null. Problem with that is that the Texboxes will have text in them by default bc they are populated by a database
That's why I must know if the TextChanged handler has fired
 
Look again.
Into the code ;)
You will notice that it do more than one verification
The if (text != null) is about the TextBox itself, not the text in the text box.
It is just to be sure that it is the TextBox that is calling the method
 
So is that checking for a change?
 
4:29 PM
After that, it checks if the TAG associated with the text is present, if not it checks if there is text and add the tag
If the tag already exists in the dictionary,
it will compare with the text, and will update only if the text in the textbox is different from the text in the dictionary
:)
Yes, it is checking if the text in the textbox is different from the text in the dictionary :)
 
OK.
 
What you have to do is to populate the dictionary with the values of the text boxes in the beginning, if you want that every text box be in the dictionary from the start :)
Or,
 
But I really think it would be much easier to check for the change event
 
If you want TAGS in the dictionary ONLY
 
I just don't know how to do that
 
4:32 PM
Imagine one thing.
You have one text box with the word "string" by default
than, the user go there and press "s"
Now you have strings.
If you use the TextChanged event
It will fire
And you will update the DIC, because the text changed. Than, the user press the backspace and erase the s
 
I get the logic, but I am not too concerned with editiing the database entry with the same entry as much as I am writing a LOT of code for something this simple. LOL
 
Oh..
This is the reason?
Don't worry.
Put everything there, unless you have 357 fields to update, this will not matter ;)
 
I mean I can do it this way, but there will be a lot of event handlers
 
Just update everything :)
You can do a different thing
Using only ONE event
 
I can update evrything, but I don't really want to
 
4:35 PM
The same you are already using
 
All I want ot do is check if the TextChange event has fired.
 
I'll put another option in the answer. I know now what you want to do ;)
 
it is sort of an inbetween the two kind of thing.
 
Just wait a couple of minutes :)
 
ok
 
4:42 PM
It's there
Just populate your dictionary with the "default values" you store in the text box, and when you call the buttom click event, compare the values in the text boxes with the values in the dictionary :)
If they are different, you "do your thing" ;)
With this "second option", you don't have to worry with all the events. Just populate the dictionary in the beginning, and compare in the end :)
 
Yep, I was actually just coding that! LOL
 
lol
 
I think we had the same idea
 
Nice :)
 
I actually already had a dictionary that I used topopulate the Textboxes from the database
 
4:48 PM
Nothing like a bit of conversation with some total stranger to came up with ideas haha
Perfect :)
So you already have everything
 
so I am comparing the key value to the tag and if the text doesn't match the n it won't write to the new dictionary
 
Just use the same dictionary in the button method to compare :)
 
Yep
Got it! Thanks!
 
I think is the contrary
You use the TAG of the text box to find the entry in the dictionary, and if the text do not match, you update the database :)
Ah!
 
Yep
 
4:50 PM
You have to write to the new dictionary (the one you will use to update the database) (to update the database) when the text in the textbox do not match the text in the dictionary (the one you used to populate the text box controls in the beginning)
 
Right.
So what I'm doing is foreach KeyValuPair in the old dictionary if the key matches the Tag of the textbox being checked(foreach TextBox) then it will check the Value of the old dictionary with the matching key. if the value is different from the TextBox.Text, then it will add the the new key the tag and to the new value, the text of the textbox.
 
Basically, yes :) Only that you don't need to iterate through the old dictionary. Just use the same code I posted: if (dic[(string)textbox.Tag] != textbox.Text) { update the new dic }
The dic[key] will return the text value stored for the key. As you are iterating throug the controls, you have the key already, from the textbox.Tag :)
 
Yeah, but you're still using the multiple event handlers.
Look at my code and see how this works with that.
 
 
1 hour later…
6:07 PM
Hello, sorry, I was in a meeting.
I dind't understand the part of "multiple events handlers"...
In my second suggestion, after the edit, you need to use just the buttom click event. Nothing more...
 

last day (15 days later) »