Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am having a text box in wpf c# window application. I am creating it in c# dynamically in Data grid.I have attached a event handler to that text box.But problem is that when control is loaded into it the text box event is being called infinitely ...Complete code is being done in c# only... I am not using xaml ... every text box is being created dynamically.... Note: the text box is present in Data Grid... Please help me out yar...

C#
private void txtUnitCount_Changed(object Sender, TextChangedEventArgs e)
            {
          try
          {
              int index = 0;
              int rowIndex = -1;
              TextBox txt = Sender as TextBox;
              int Units = Convert.ToInt32(txt.Text);

              for (index = 1; index < dsBillingDetails.Tables.Count; index++)
               {
                DataRowCollection rowCol = dsBillingDetails.Tables[index].Rows;

               DataRow[] DataRows = dsBillingDetails.Tables[index].Select(find);
                  if (DataRows.Count() > 0)
                 {
                    DataRow foundRow = DataRows[0];

                     rowIndex = rowCol.IndexOf(foundRow);
                     if (rowIndex != -1)
                      {

                        //This line is causing problem i.e below line
                        dsBillingDetails.Tables[index].AcceptChanges();

                       }

                      break;
                      }
                  }


          }
          catch (Exception ex)
          {
          }
      }
Posted
Updated 22-Oct-13 20:49pm
v4
Comments
Sergey Alexandrovich Kryukov 23-Oct-13 2:25am    
Very good, now you need to create an extremely simplified but comprehensive code sample. Remove everything which is not related to this particular problem. Just one text box, probably no other controls, something like that. It's easy to screw up something in event handling, but such bugs are usually easy to spot. Add your sample code using "Improve question".
—SA
jing567 23-Oct-13 2:38am    
Yes i will provide the code , but just now i have observed that the text change event is calling infinitely... this is because in the text change event i have updated the datable which is given as the source for Datagrid... so as the source of grid is updated... again text box event is being called... so like this this loop is becoming infinite... In few minute i will provide the code
Sergey Alexandrovich Kryukov 23-Oct-13 2:44am    
I added formatting. Please, use "Improve question" again and 1) see how "pre" tags should be used for formatting code; 2) please improve your indentation to make the structure readable.

I have to sleep now, but for now I'm telling you what's really bad: exception handling. You block propagation of exceptions. Don't do it. Such construct is rarely used, very rarely, usually only to work around some "bad" library which is not accessible for your patch...

Also, your method does not show that it is used as an event handler, but probably it is. Show the code when an event handler is added to an invocation list of some event instance, to see what instance of what object. In other words, show where is your += operator.

—SA
jing567 23-Oct-13 2:47am    
K please dont sleep... wait for 5 mins... I will update the code in couple of minutes
Sergey Alexandrovich Kryukov 23-Oct-13 2:50am    
Sorry, no... tomorrow. Please leave me a comment when you are done.
—SA

1 solution

Please see my questions and clarify accordingly. For now, I'll answer to one of your follow-up questions:
jing111 wrote:
…let me know how to add handler to a text box for lost focus event…
Here is how:
C#
myElement.GotFocus += {sender, eventArgs} => {
    UIElement senderElement = (UIElement)sender;
    DoSomethingOnChangeOfTheFocusState(senderElement.IsFocused); // IsFocused will be true if got focus, false if lost
};
Note that the event name "GotFocus" is somewhat misleading. It actually covers both getting and losing focus. Same thing with the events UIElement.GotKeyboardFocus and UIElement.PreviewGotKeyboardFocus:
http://msdn.microsoft.com/en-us/library/system.windows.uielement.gotfocus%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.uielement.gotkeyboardfocus%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.uielement.previewgotkeyboardfocus%28v=vs.110%29.aspx[^].

The optional approach is to override corresponding virtual methods, with the same effect:
http://msdn.microsoft.com/en-us/library/system.windows.uielement.ongotfocus(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.uielement.ongotkeyboardfocus(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.uielement.onpreviewgotkeyboardfocus(v=vs.110).aspx[^].

—SA
 
Share this answer
 
v4
Comments
jing567 24-Oct-13 6:27am    
Hey thanks for that....its very useful... Can u say me that how to set the focus to a text box in a dynamic grid after its change event... an that to the cursor should be placed at the end position of the string present in text box... Please help me i am working out on wpf
Sergey Alexandrovich Kryukov 24-Oct-13 8:49am    
It has nothing to do with the grid. You just have some UIElement which may have Focusable==true.
In its simplest form, focusing it as simple as calling myElement.Focus(). At the same time, in general situation, setting focus in WPF is surprisingly advanced and sophisticated matter. Please see:
http://msdn.microsoft.com/en-us/library/aa969768.aspx.

Basically, the idea is to focus "in advance"; even when some element cannot have "real" keyboard focus (say, its window is not activated, disabled, etc.), if can have "logical" focus, which will become keyboard focus as soon as it is possible. Hence, different events for that, and so one. In System.Windows.Forms, similar behavior is also implemented (active control), probably, in a less comprehensive way...

—SA
jing567 24-Oct-13 10:07am    
Hey hi... I am using same synatx but it is not focusing yar.... is there any other way to focus?
Sergey Alexandrovich Kryukov 24-Oct-13 11:11am    
Well, read the MSDN article referenced above, in particular, "Navigating Focus Programmatically", there are some subtleties. Hard to say what you are missing without seeing your code.
Perhaps, read on the topic starting from that page and try to find out the solution by yourself. If you can't get satisfactory results, try to reduce it to some comprehensive code sample, as small as you can, and ask a separate question. If this happens and you want to notify me, please put a link in response to this comment.
—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