Click here to Skip to main content
15,896,539 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hello,
I am builing a wpf window application, Here i am desiging a dynamic Datagrid and in that grid i am providing a text box, when the user enters the text some calculations should be done in the grid... But my problem is that whenn the user tries to enter "100" in the text box and enter "1" means the event is firing.... so for 3 digits it is firing 3 times... so please help me to call the event only once after entering complete data into text box .... I have no problem in handling on blur events also... Please help me with providing code... here is my handler written...

DataTemplate textBoxTemplate = new DataTemplate();
FrameworkElementFactory textboxElement = new FrameworkElementFactory(typeof(TextBox));
Binding textboxBinding = new Binding("LineItemUnits");
Binding txtLineItemID = new Binding("LineItemID");
textboxElement.AddHandler(TextBox.TextChangedEvent, new TextChangedEventHandler(txtUnitCount_Changed));
textboxElement.SetBinding(TextBox.TagProperty, txtLineItemID);
textBoxTemplate.VisualTree = textboxElement;
templateColumn.CellTemplate = textBoxTemplate;
grdCharges.Columns.Add(templateColumn);

textboxElement.AddHandler(TextBox.TextChangedEvent,
newTextChangedEventHandler(txtUnitCount_Changed));


C#
private void txtUnitCount_Changed(object Sender, TextChangedEventArgs e)
       {
           }
Posted
Updated 21-Oct-13 22:08pm
v2

1 solution

Only once? I have a whole CodeProject article on this problem: Wish You Were Here… Only Once[^].

Note that you don't have to use my highly universal approach, which is created mostly to demonstrate the possibility of context-sensitive condition techniques and also for curiosity, even though my technique is not only highly universal but also quite usable practically. It's more important for deeper understanding of some fundamentals of programming. But you can use the elementary technique I overview in the very beginning. Understanding is what you actually need the most.

[EDIT]

Yes, you are right, your problem is quite different, and the problem is: you don't properly understand the concept of data input and related events.

Here is the thing: when a user enters (or even deletes) a single character, this is the event, but when the user decides that all characters are properly edited, it is not an event, because this is all in the user's head, nowhere.

It means that your whole concept is wrong. You need to require the user to actually confirm that the output is done. For example, press Enter (which you can detect handling KeyDown or KeyUp event, moves the keyboard focus (which you can detect handling LostFocus event):
http://msdn.microsoft.com/en-us/library/system.windows.uielement.keydown(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.uielement.keyup(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.uielement.lostfocus(v=vs.110).aspx[^].

Alternatively, you could detect that all positions are filled if you use something like the MaskedTextBox control, such as this one:
http://wpftoolkit.codeplex.com/wikipage?title=MaskedTextBox[^].

However, I don't think that these two approaches, which are certainly working, are the best. I think the best option would be not handling this events for such an uncertain purpose at all. Instead, I would recommend to validate all input at the very point where the input data can are about to be used. Then you can sanitize data, and, if data is unsatisfiable, focus the "offending" input control and provide the user with explanation what requirements are not met.

This approach, despite its simplicity, is more flexible. I'll explain why: what if validity of data entered in one control depends on the states of some other controls? Such complication is pretty usual. So, validation and sanitation all of the entered data at once is more universal approach.

—SA
 
Share this answer
 
v2
Comments
jing567 22-Oct-13 3:57am    
No ots not the solution yar... my problem is that it should allow the user to enter whole value into text box that he is expecting to enter... But in my case when user entering first character the event is being triggered
Sergey Alexandrovich Kryukov 22-Oct-13 11:19am    
This is not "not the solution", this is "you don't see the solution". See the difference? :-)
But I'm kidding, you are right. I'll answer...
—SA
Sergey Alexandrovich Kryukov 22-Oct-13 11:31am    
All right, thank you for this note, please see updated answer, after [EDIT].
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900