简体   繁体   中英

How to generate random integers in a text box in C#

How do I randomly generate integer values in a text box for every one second till I click on a button.

I came up with following code (Clicking on Button 1 should generate random integers for every 1 second in textBox1 till Button2 is clicked) and its not working (Output is empty text box).

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Timers;

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        bool buttonclicked = false;
        System.Timers.Timer myTimer;
        System.Random r = new System.Random();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            while (buttonclicked == false)
            {
                myTimer = new System.Timers.Timer();
                myTimer.Elapsed += new ElapsedEventHandler(rnd);
                myTimer.Interval = 1000;
                myTimer.Start();
            }
        }
        public void rnd(Object sender, EventArgs e)
        {
            textBox1.Text = r.Next(0, 1000).ToString();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            buttonclicked = true;
            myTimer.Stop();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

Looks like it is because you are using a while loop instead of an if block. Your condition should look like this:

if( buttonclicked == false)
{
    ....
}

Your while(buttonclicked == false) will create an infinite loop once you click the button.You can just do

    private void button1_Click_1(object sender, EventArgs e)
    {
            myTimer = new System.Timers.Timer();
            myTimer.Elapsed += new System.Timers.ElapsedEventHandler(rnd);
            myTimer.Interval = 1000;
            myTimer.Start();
    }

    private void button2_Click_1(object sender, EventArgs e)
    {
        myTimer.Stop();
    }

and you need to make a safe thread call to the textbox by doing :

    delegate void SetTextCallback(string text);
    private void SetText(string text)
    {
        if (this.textBox1.InvokeRequired)
        {
            SetTextCallback d = new SetTextCallback(SetText);
            this.Invoke(d, new object[] { text });
        }
        else
        {
            this.textBox1.Text = text;
        }
    }

And to set the text just use SetText(r.Next(0 ,1000).ToString());

I think you were right to a loop - but not the timer.

No need to declare the System.Random at the class level either - unless you're using it elsewhere.

Using a for loop.

private void button1_Click_1(object sender, EventArgs e)
{
    var r = new System.Random();

    for (var i = 0; i < 1000; i++)
    {
        textBox1.Text = r.Next(0, 1000).ToString();
    }
}

Using a while loop.

private void button1_Click_1(object sender, EventArgs e)
{
    var r = new System.Random();
    var i = 0;

    while (i < 1000)
    {
        textBox1.Text = r.Next(0, 1000).ToString();
        i++;
    }
}

If you really want to add a new number each second, you could use async and await (to stop UI blocking) and add a System.Threading.Thread.Sleep(1000); .

Or even Task based would be better than a Timer (in my opinion). It's good to understand how Tasks work.

A Timer is okay for this scenario - but in real applications they can get messy, especially with multiple timers.

Here is an example of how the above would work using Tasks.

private void button1_Click(object sender, EventArgs e)
{
    var uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();

    Task.Factory.StartNew(() => AddRandomNumbers(uiScheduler));
}

private async Task AddRandomNumbers(TaskScheduler uiScheduler)
{
    var r = new Random();

    for (int i = 0; i < 1000; i++)
    {
        await Task.Factory.StartNew(
            () => textBox1.Text = r.Next(0, 1000).ToString(), 
            CancellationToken.None, 
            TaskCreationOptions.None,
            uiScheduler);

            Thread.Sleep(1000);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM