views:

131

answers:

4

Solved: You guys are the best! I took al the content of goyouidiot_Click and made it into a method called displayResult, and then:

 private void t1_TextChanged(object sender, EventArgs e)
       {
        displayResult();
       }

How didn't i thought of that before? lol, thx

here is original messege:

No lone ago i built a little software that calculates an averege of 15 numbers. The code starts to run when the buttun is clicked, but i want to put this code in an infinate loop that starts to run with program, so the answer will be auto updated. this is my code:

private void goyouidiot_Click(object sender,

EventArgs e) { . . . . }

and those who havn't understood:

I have 15 text-boxs, and I want the method to run when the text boxs' content changes.

+2  A: 

Putting it in a infinite loop could starve the system of CPU power, meaning you will need to introduce a pause (Thread.Sleep).

If you use a pause, you may as well use a Timer object - there is a form's based Timer or a thread based Timer (System.Windows.Forms, or System.Threading / System.Timers);

I would personally suggest using a timer to tick at a desired interval.

A thread timer uses delegates / thread pool whereas the forms based timer places messages on the message pump - both are not guaranteed to be accurate to their intervals due to the overhead in the way in which a tick is created.

Adam
+9  A: 

Rather than make an infinite loop (which will cause the application to hang unless it's on a background thread - a much bigger can of worms) you should just respond to change events.

If your numbers are being updated in text boxes, just bind the TextChanged events of each of them to your goyouidiot_Click method - which you should then rename.

Edit

As Eric points out in his answer, the reason these events work is because there is in fact an infinite loop in the background to listen for changes - the Windows message pump. This loop is started when you call the Run method on your application.

Tesserex
+1 because it's far better to use events than a periodic timer in this case
andyp
There's no need to actually loop for this kind of process at all. Just use events. If you want to push something from another source to the UI just use another thread.
the_drow
I did it, success~! But i have the feelign that I'm doing something wrong, because now i have 15 textchanged sentences.... I'm sure that theres a better way, maybe including groups or something else.
Gilad Naaman
@Gilad: You need to suppress the event of all the other textboxes while the one the user typed in is being processed. You can either change which event you are binding (i.e. to target user input to the textbox rather than change in the textbox), or you can suppress the event inside the event. You can do this either completely by removing the event or more practically by just having a variable which indicates whether the event is happening that is checked before beginning the event.
Brian
A: 

If I understand correctly (you want to update something every 15 minutes in your Winform application), then it is better to use the Timer class, which will run your code periodically.

EDIT: If you want to perform some calculations when a text is changed in one of the text boxes, you should do it in an event handler for the TextChanged event of these text boxes (you want to assign the same handler to every text box)

Grzenio
No. I have 15 text-boxs, and I want the method to run when the text boxs' content changes.
Gilad Naaman
+3  A: 

Your intuition that an infinite loop must be involved is correct. But you don't want to write that loop yourself; the runtime library has already written it for you. What you want to read up on is event-driven programming. Find a good introduction, like, say:

http://www.c-sharpcorner.com/UploadFile/sksaha/EventsinNet11152005043514AM/EventsinNet.aspx

The way event-driven programming works behind the scenes is that there is an infinite loop of code that monitors the state of a queue of messages coming in from the operating system. The messages are representing things like mouse clicks and typing. The infinite loop code then turns those messages into event firings. You can listen to those event firings and run code when particular events happen.

Eric Lippert
wow... thank you =) didn't know that
Gilad Naaman