circular references in controls?
-
Lets say you have two text boxes, and you want to update one any time the other changes, regardless of which changes. For example converting between Miles and Km. How do you prevent the endless loop?
David Wilkes
-
Lets say you have two text boxes, and you want to update one any time the other changes, regardless of which changes. For example converting between Miles and Km. How do you prevent the endless loop?
David Wilkes
Remove the event handler that listens for changes of the other textbox before you change its value and readd it afterwards.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
Remove the event handler that listens for changes of the other textbox before you change its value and readd it afterwards.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
The XXXchanged events only fire when the value really changes, assigning a value that is not different from the current value does not fire the event. So it all boils down to the question: is my calculation numerically stable, i.e. will it converge to a single value. If yes (as with linear transformations, e.g. your unit conversions), dont do anything special. If no (or unknown), break the loop explicitly (e.g. by removing and reinstalling one of the event handlers). The following example has two TextBoxes, showing a number and its square; for some of the values (e.g. 50) it will take two iterations, but it always reaches a stable result:
public class CPTest\_ChangedEvent: CPTest { TextBox tb1=new TextBox(); TextBox tb2=new TextBox(); System.Windows.Forms.Timer timer=new System.Windows.Forms.Timer(); public override void Run() { tb1.Text="1"; tb2.Text="1"; tb1.TextChanged+=new EventHandler(tb1\_TextChanged); tb2.TextChanged+=new EventHandler(tb2\_TextChanged); timer.Interval=1000; timer.Tick+=new EventHandler(timer\_Tick); timer.Start(); } private void tb1\_TextChanged(object sender, EventArgs e) { string s=tb1.Text; log("tb1\_TextChanged: "+s); double i1=double.Parse(s); double i2=i1\*i1; tb2.Text=i2.ToString(); } private void tb2\_TextChanged(object sender, EventArgs e) { string s=tb2.Text; log("tb2\_TextChanged: "+s); double i2=double.Parse(s); double i1=Math.Sqrt(i2); tb1.Text=i1.ToString(); } private void timer\_Tick(object sender, EventArgs e) { string s=tb2.Text; double i2=double.Parse(s)+1; tb2.Text=i2.ToString(); if (i2>100) timer.Stop(); } }
:)
Luc Pattyn
-
Remove the event handler that listens for changes of the other textbox before you change its value and readd it afterwards.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
Lets say you have two text boxes, and you want to update one any time the other changes, regardless of which changes. For example converting between Miles and Km. How do you prevent the endless loop?
David Wilkes