• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 490
  • Last Modified:

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

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
Dynotoe
Asked:
Dynotoe
  • 6
  • 4
1 Solution
 
Expert1701Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
DynotoeAuthor Commented:
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
DynotoeAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
DynotoeAuthor Commented:

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
 
DynotoeAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
DynotoeAuthor Commented:
I'll send you the whole picture lol :)

Dynotoe

istomarFarza  "ate" Com cast "doot" Net
Dynotoe
AKA Sean
0
 
DynotoeAuthor Commented:
oops IshtomarFarza "ate" ...

By the way your solution to help me works terrific!

Cheers,

Sean
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now