Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
Hi,
I have simple formApplication script that contains button and some TextBoxs.
I want when click on button, one textbox shows some numbers.
How I can make that dynamic.
C#
private void button1_Click(object sender, EventArgs e)
        {
            txt3.Text = "";
            for (int i = 0; i <50; i++)
            {
                Random random = new Random();
                int randomNumber = random.Next(100, 150);
                txt3.Text = randomNumber.ToString();
            }
        }


Now it waits to loop finished and shows latest number.
I want it shows all numbers in TextBox.
Regards,
Posted
Comments
[no name] 29-Aug-13 7:09am    
txt3.Text += randomNumber.ToString();

Have you thought about simply buying a decent book on programming and reading it? Just a thought.
Rezame 29-Aug-13 7:56am    
I have C# books, and know about this: txt3.Text += randomNumber.ToString();
But first tried next come here to ask.

Try:
C#
txt3.Text += randomNumber.ToString();
You may want to add some spacing or newlines (if this is a multiline text box)
 
Share this answer
 
Comments
Rezame 29-Aug-13 7:44am    
Thanks.
It shows all variable together.
I want it shows separately during loop.
OriginalGriff 29-Aug-13 8:34am    
You can't - or not like that. The problem is that little-or-no-time occurs between successive iterations of the loop, and while there is something you can do about that using Thread.Sleep, that freezes your UI thread as well, so the display doesn't get updated and you still can't see the interim results.

To do this, you really want to include a Timer control and set the TextBox content at a specific interval rather than using a loop at all. (The other solution is to use a separate thread, but that complicates things a lot, and makes it harder to actually show the value in the textbox, because UI controls can only be accessed from the thread that created them, not any other thread you might create. A Timer is a simpler solution)
Rezame 29-Aug-13 8:38am    
I set timer function that shows every second only one number.
But it write all numbers in same time.
OriginalGriff 29-Aug-13 9:54am    
Then take the loop out, and move just the line
txt3.Text = randomNumber.ToString();
to be the whole of the Timer.Tick event. No other code needed!
OK, try this:

C#
private delegate void DisplayTextBoxDelegate(int i)

private void button1_Click(object sender, EventArgs e)
{
     txt3.Text = "";
     var thdDoTextBoxStuff = new Thread(DoTextBoxStuff);
     DoTextBoxStuff.IsBackground = true;
     DoTextBoxStuff.Start();
}

private void DoTextBoxStuff()
{
     for (int i = 0; i <50; i++)
     {
         Random random = new Random();
         int randomNumber = random.Next(100, 150);
         txt3.Invoke(new DisplayTextboxDelegate(UpdateTextBoxValue), randomNumber);
         Thread.Sleep(500);
     }
}

private void UpdateTextboxValue(int iValue)
{
    txt3.Text = iValue.ToString();
}


Again, I've not tried the above - but it should get you moving in the right direction.
 
Share this answer
 
v4
Comments
Rezame 29-Aug-13 7:42am    
Thanks.
It shows all variable together.
I want it shows separately during loop.
OriginalGriff 29-Aug-13 8:35am    
No, it won't. You can't directly access UI controls except from the UI thread - you will immediately get a "Cross Thread" exception if you try.
hypermellow 29-Aug-13 9:30am    
Your right, ... good spot! :-)

... I can feel an updated answer involving a delegate building!
Need add this:
C#
Application.DoEvents();


Next it works.
 
Share this answer
 
Comments
OriginalGriff 29-Aug-13 9:55am    
Bad idea. Trust me on this, if you need DoEvents, then you have made some spectacularly bad decisions! :laugh:
Rezame 29-Aug-13 11:04am    
Can you say me what is problem with DoEvents ?
OriginalGriff 29-Aug-13 11:54am    
From the MSDN page: http://msdn.microsoft.com/en-us//library/system.windows.forms.application.doevents.aspx

"Caution noteCaution
Calling this method causes the current thread to be suspended while all waiting window messages are processed. If a message causes an event to be triggered, then other areas of your application code may execute. This can cause your application to exhibit unexpected behaviors that are difficult to debug. If you perform operations or computations that take a long time, it is often preferable to perform those operations on a new thread. For more information about asynchronous programming, see Asynchronous Programming Model (APM)."

For a better explanation, see here:
http://stackoverflow.com/questions/5181777/use-of-application-doevents
I cannot say it better!
Rezame 29-Aug-13 11:56am    
Thanks.
Can you say me what is best method for shows number?
Or any other Tool or function that print number in Main form?
OriginalGriff 29-Aug-13 14:12pm    
For the application the OP posted about, a Timer is the obvious solution - threading is overkill!

For other applications it will depend on exactly what you are trying to do, there are many, many options when it comes to Threads and thread related operations.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900