简体   繁体   中英

How to make Dynamic TextBox in C#

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.

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 each number during loop in TextBox seperatly. Seems using Dynamic TextBox is a bit hard, is there any other solution to show this numbers in main form? Regards,

you need to do it in separate thread and sleep between each iteration or use a timer.

for example:

private int counter;
Timer t = new Timer();
Random random = new Random();

private void button1_Click(object sender, EventArgs e)
{
     t.Interval = 100;
         t.Tick += new EventHandler(t_Tick);
         counter = 0;
         t.Enabled = true;
     txt3.Text = "";
}

  void t_Tick(object sender, EventArgs e)
  {
     counter++;
     int randomNumber = random.Next(100, 150);
     txt3.Text = randomNumber.ToString();

     if (counter >= 50)
     {
        t.Enabled = false;
     }
  }

remember this is just one example out of million ways to do it. a lot of them are good

another way will be using threads:

private void button1_Click(object sender, EventArgs e)
{
     Thread t = new Thread(new ThreadStart(randomize));
     t.Start();
}

  private void randomize()
  {
     Random random = new Random();

     txt3.Text = "";
     for (int i = 0; i < 50; i++)
     {
        int randomNumber = random.Next(100, 150);

        Invoke(new setTxtHandler(setText), randomNumber.ToString());

        Thread.Sleep(100);
     }
  }

  private void setText(string val)
  {
     txt3.Text = val;         
  }

  private delegate void setTxtHandler(string val);

You have to put the Random outside of the loop since it is seeded with the current time and the loop executes too fast.

Random random = new Random();
for (int i = 0; i <50; i++)
{
    int randomNumber = random.Next(100, 150);
    txt3.Text = randomNumber.ToString();
}

MSDN

The random number generation starts from a seed value. If the same seed is used repeatedly, the same series of numbers is generated . One way to produce different sequences is to make the seed value time-dependent, thereby producing a different series with each new instance of Random. By default, the parameterless constructor of the Random class uses the system clock to generate its seed value , while its parameterized constructor can take an Int32 value based on the number of ticks in the current time. However, because the clock has finite resolution, using the parameterless constructor to create different Random objects in close succession creates random number generators that produce identical sequences of random numbers.

However, since the loop executes very fast you won't see each value anyway, only the last.

You can add a panel and then add textboxes onto it

  Random random = new Random();
     for (int i = 0; i <50; i++)
     {
         TextBox t=new TextBox();
         int randomNumber = random.Next(100, 150);
         t.Text = randomNumber.ToString();
    panel.Controls.Add(t);
     }

You should you separate thread and calculate numbers there and update UI then, because now your UI will be updated after calculation is finished.

Eg BackgroundWorker

Now it waits to loop finished and shows latest number.

That's because the operation is single-threaded so there's no UI update until it's completed. How you would change that depends on whether this is a Windows application or a Web application.

If this is, for example, a Windows Forms application then you want to call Application.DoEvents() to update the UI each time the loop iterates:

txt3.Text = randomNumber.ToString();
Application.DoEvents();

This will update the UI's text box each time. (Though it will be very fast, so I doubt you'll even notice it. You might throw in a Thread.Sleep() if you want to slow it down.)

If this is a web application, then you'll want to do this whole thing client-side instead of server-side. This is because "updating the UI" in a web application, at least from the server's perspective, means returning the response and waiting for another request. This would result in a lot of back-and-forth between the browser and the server just for updating a single UI element. (Which, while it would be slow enough that you'd notice it, it would be a terrible UX.) So in this case you'd want to move the code to JavaScript, which has the benefit of not being single-threaded and would update the UI as you expect it to.

If you want to show each number in the text box, you will need an additional thread which will run the code you have inside button1_Click. With your current implementation, the GUI will freeze while executing the content. As it's very quick you won't notice it though. From the additional thread, call invoke when setting txt3 to synchronize with the GUI thread. And add a Thread.Sleep(1000) or similar in the for loop in order to see the numbers change.

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