Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. circular references in controls?

circular references in controls?

Scheduled Pinned Locked Moved C#
tutorialquestionannouncement
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    amatbrewer
    wrote on last edited by
    #1

    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

    S J 2 Replies Last reply
    0
    • A amatbrewer

      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

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      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

      www.troschuetz.de

      L A 2 Replies Last reply
      0
      • S Stefan Troschuetz

        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

        www.troschuetz.de

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • S Stefan Troschuetz

          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

          www.troschuetz.de

          A Offline
          A Offline
          aSarafian
          wrote on last edited by
          #4

          or a flagging mechanism can be used here.

          1 Reply Last reply
          0
          • A amatbrewer

            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

            J Offline
            J Offline
            jayart
            wrote on last edited by
            #5

            I had a similar problem while using a combo box and grid row updation. Try using "SelectionChangeCommitted" event. This would be fired only when the control selection is changed explicitly by user.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups