Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

C#  -- Question on "should I" and how to place a delay in TextBox1.TextChanged method

Posted on 2006-05-07
11
Medium Priority
?
452 Views
Last Modified: 2008-01-09
Hi everyone,

I have upto 25 textboxes on a form1 which each use the TextChanged event that are called using the evert args e or whatever.

Questions

1).  If I placed a 1 or 2 second delay in this textbox1.textchanged evet/method/function, does the whole project have to wait for it to finish or is this system call on a separate process/thread etc.  What if a few were being called on the same form for different textboxes at roughly the same time.  Would the delays get in each others way?

2).  How would I set up such a delay (Without using a timer cause I'm looking for an alternate to 20+ timers, one for each textbox sort of thing)  What would be an example of some "delay code in a function/Method ?

Please allow my thanks in advance for your gracious help.

Cheer,

Dynotoe
0
Comment
Question by:Dynotoe
  • 6
  • 4
11 Comments
 
LVL 11

Expert Comment

by:Expert1701
ID: 16627568
Dynotoe, I will answer your questions in reverse:

2) A simple delay can be created by calling the System.Threading.Thread.Sleep method.  For example, to create a 2 second delay you would use the following code:

  System.Threading.Thread.Sleep(2000);

1) The TextBox.TextChanged event will be processed by the UI thread.  This means that while the method is run, the UI will be suspended.  If the method contained a 2 second delay, all UI activity (e.g. typing in another TextBox, or the execution of another TextBox's TextChanged event) will be delayed.  To avoid this, you could use the following pattern:

  private void textBox1_TextChanged(object sender, System.EventArgs e)
  {
    System.Threading.Thread processThread = new System.Threading.Thread(new System.Threading.ThreadStart(Process_textBox1_TextChanged));
    processThread.Start();
  }

  private void Process_textBox1_TextChanged()
  {
    System.Threading.Thread.Sleep(2000);

    //..
  }
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 16627712
Not sure what you're end goal is here...

It seems like you want to be able to determine when the value in a TextBox hasn't been changed for two seconds and then do something with that TextBox?

How about using just ONE timer and an ArrayList like below?  This simple example has five textboxes.  When the value in any of the textboxes changes, the backcolor is changed to green.  If the value stops changing for at least two seconds then it will change the backcolor to red:

        private System.Collections.ArrayList tbs = new System.Collections.ArrayList();

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.TextChanged += new EventHandler(textBox_TextChanged);
            textBox2.TextChanged += new EventHandler(textBox_TextChanged);
            textBox3.TextChanged += new EventHandler(textBox_TextChanged);
            textBox4.TextChanged += new EventHandler(textBox_TextChanged);
            textBox5.TextChanged += new EventHandler(textBox_TextChanged);
            tbs.Add(textBox1);
            tbs.Add(textBox2);
            tbs.Add(textBox3);
            tbs.Add(textBox4);
            tbs.Add(textBox5);
            timer1.Interval = 250;
            timer1.Start();
        }

        void textBox_TextChanged(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)sender;
            tb.BackColor = Color.Green;
            tb.Tag = DateTime.Now.AddSeconds(2);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            TimeSpan ts;
            foreach (TextBox tb in tbs)
            {
                if (tb.Tag != null)
                {
                    ts = DateTime.Now.Subtract((DateTime)tb.Tag);
                    if (ts.TotalMilliseconds >= 0)
                    {                        
                        tb.BackColor = Color.Red;
                        tb.Tag = null;
                    }
                }                
            }
        }
0
 

Author Comment

by:Dynotoe
ID: 16629678
Hello!

Well this is what I am trying to accomplish....

I have a gui form that displays changing stock quotes in text boxes (One per symbol).  But too many!

I would like to ...

1)  When a quote is changed the backgroud color will change to LightGolden Yellow but only for two seconds where it should then be changed back to original color (white).  This lets your eye catch what has just changed.

2)  When the new quote comes in if it was a down change (based on what was originally in the text box the Forcolor would change to red etc.  If up forcolor is changed to green etc.  These can stay like this till a new quote is captured.


The problem is that I don't want 20-30 timers setup for each textbox and setting up a user control would be difficult becasue all the forms and textboxes are already set up.  So Im looking for an alternative.  I do have the Text changed event set up for each textbox and already have the functionality of the red Green and Yellow background color functioning.  What I don't have is the ability to have the backcolor change back to White after time has passed since a different quote changed as compared to what was previously in there.  I have those sitting in Vars also ready to go.

Make sence?

Thanks for your help.

Best,
Dynotoe
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16630337
Ok...the code I posted can handle that just fine...just change the colors to the ones you want...

When you get a new quote value, check the previous value in the TextBox and change the ForeColor accordingly.  Set the BackColor to LightGolden Yellow and set the Tag() property as I have shown in the code:

    tb.Tag = DateTime.Now.AddSeconds(2);

Then in the Timer code, change the Color to white and reset the Tag:

    tb.BackColor = Color.White;
    tb.Tag = null;
0
 

Author Comment

by:Dynotoe
ID: 16632940
Hi Idle,

Great stuff.  Quick question Regarding the update od last price (which is then referenced as prior price when the new one comes in ) how can I check which textbox has been smet into the text changed as sender?  The reason is that I have a variable for each textbox that will store the prior price in it.  Or is there a better way to reference and compare last price with current price to determin an uptick or a down tick.  etc.

Dynotoe
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16633522
Look at my code for the TextChanged() event:

        void textBox_TextChanged(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)sender;
            tb.BackColor = Color.Green;
            tb.Tag = DateTime.Now.AddSeconds(2);
        }

I cast the sender to a TextBox referenced by the "tb" variable.  If you need the name then use "tb.Name".

There are many ways to handle the previous value.  One possibility would be to use a HashTable.  Then you can lookup the previous value using the stock name as the key.

Or you could create a simple class to hold both a reference to the old value and the corresponding TextBox and place an instance of that into the HashTable.  Then you will be able to jump directly to the TextBox based on the stock name.

So many possibilities...how are you correllating the stock name to your variables right now?
0
 

Author Comment

by:Dynotoe
ID: 16634067

Hi Idle,

Unfortunately HARD CODED lol

private float EURUSD_oldValue = 0.0F;
private float USDJPY_oldValue = 0.0F;
private float GBPUSD_oldValue = 0.0F;

And so on...

The problem is that if a new symbol is added manually or dynamically then I would also have to make a var for it.  ;(

Best,

Dynotoe
0
 

Author Comment

by:Dynotoe
ID: 16634078
Then I call this method from the textChanged event...

            // Update current prices which will then be used to determin UP/Down tick for next quote
            public void updatelastprices(ref TextBox textBoxPriorPrice )
            {
                  switch (textBoxPriorPrice.Name)
                  {
                        case "EURUSD":
                              EURUSD_oldValue = Convert.ToSingle(textBoxPriorPrice.Text);
                              break;
                        case "USDJPY":
                              USDJPY_oldValue = Convert.ToSingle(textBoxPriorPrice.Text);
                              break;
                        case "GBPUSD":
                              GBPUSD_oldValue = Convert.ToSingle(textBoxPriorPrice.Text);
                              break;

etc.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16634553
How do you "lookup" the stock prices?  Are you passing the stock name to a function?

Do you have 25 hard coded lines to lookup each quote?

We can make this process more dynamic and flexible but I need a "bigger picture"...   =)
0
 

Author Comment

by:Dynotoe
ID: 16634837
I'll send you the whole picture lol :)

Dynotoe

istomarFarza  "ate" Com cast "doot" Net
Dynotoe
AKA Sean
0
 

Author Comment

by:Dynotoe
ID: 16634860
oops IshtomarFarza "ate" ...

By the way your solution to help me works terrific!

Cheers,

Sean
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question