How to make Dynamic TextBox in C# - 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.

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.

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.
E.g. BackgroundWorker

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.

Related

How do I make a quick program which goes through a list in a C# Windows Form Application?

I was experimenting with lists in C#'s console application, specifically a randomized int list which had its number order randomized. In this experiment I wanted to go through the randomized values from the list when I pressed enter and when it had shown all the randomized values it would stop. And it worked just as I intended: http://i.imgur.com/bNOYrZp.png[^]
Random r = new Random();
int tempValue;
List<int> number = new List<int>();
number.Add(1);
number.Add(2);
number.Add(3);
number.Add(4);
number.Add(5);
number.Add(6);
number.Add(7);
number.Add(8);
number.Add(9);
number.Add(10);
for (int i = 0; i < 10; i++)
{
tempValue = r.Next(0, number.Count);
Console.WriteLine(number[tempValue]);
number.RemoveAt(tempValue);
Console.ReadLine();
}
Now how do I do a similar thing in C#'s Windows Form Application? Instead of pressing enter to go through the list, I press a button to go through the list, and the order of the values are displayed on a label every time I press this button.
I used a similar code, but it did not work as intended. Instead of going through the randomized values it kept making a new order of values which it kept doing every time I clicked the button. What I want it to do is to go through the randomized values and after it has showed all the 10 randomized values, without duplicates, it stops.
private void button1_Click(object sender, EventArgs e)
{
List<int> number = new List<int>();
Random r = new Random();
int tempValue;
number.Add(1);
number.Add(2);
number.Add(3);
number.Add(4);
number.Add(5);
number.Add(6);
number.Add(7);
number.Add(8);
number.Add(9);
number.Add(10);
for (int i = 0; i < 10; i++)
{
tempValue = r.Next(0, number.Count);
label1.Text = number[tempValue].ToString();
number.Remove(number[tempValue]);
}
}
Since you put the list creation and for loop in the click event, its working "as-coded" (obviously not what you intended).
Remember, the entire click handler runs every time you press the button. So you need to initialize the list elsewhere and then just iterate through it on click. Something like:
private Random rng = new Random();
private List<int> numbers = new List<int>();
private void Form_Loaded(...) //Set to Form's Loaded event
{
number.Add(1);
number.Add(2);
number.Add(3);
number.Add(4);
number.Add(5);
number.Add(6);
number.Add(7);
number.Add(8);
number.Add(9);
number.Add(10);
}
private void button1_click(...)
{
tempValue = rng.Next(0, number.Count);
label1.Text = number[tempValue].ToString();
number.Remove(number[tempValue]);
}
Note that this code will have a few issues once the list runs out, there is no way to re-initialize the list, etc. I leave those as an exercise to you.
Also note that I created one instance of Random and stored it at the class level. In general, you want to use one instance per class to avoid seeding issues (though recreating it in the button click would have technically worked, since you probably can't click the button fast enough).

Using Random in multiple threads sharing same method

I'm using to following xaml to create a simple racer app
<StackPanel>
<Slider x:Name="racerOne" Maximum="1000"/>
<Slider x:Name="racerTwo" Maximum="1000"/>
<Button Content="Start Race" Click="myButton_Click"/>
</StackPanel>
I use the following code
private void myButton_Click(object sender, RoutedEventArgs e)
{
Task firstRacer = Task.Run(() => Race(racerOne));
Task secondRacer = Task.Run(() => Race(racerTwo));
}
private void Race(Slider racer)
{
int step = 0;
while (step < 1000)
{
step += new Random().Next(0, 10);
Dispatcher.BeginInvoke(new ThreadStart(() => racer.Value = step));
Thread.Sleep(new Random().Next(0, 300));
}
}
Most of the times (let's say 90% precent) both sliders appear to move together, while in debug I can see each thread generates different values for step. How come?
Random is seeded by the clock; you might want to do something like:
Random rand1 = new Random();
Random rand2 = new Random(rand1.Next());
Task firstRacer = Task.Run(() => Race(racerOne, rand1));
Task secondRacer = Task.Run(() => Race(racerTwo, rand2));
private void Race(Slider racer, Random rand)
{
int step = 0;
while (step < 1000)
{
step += rand.Next(0, 10);
Dispatcher.BeginInvoke(new ThreadStart(() => racer.Value = step));
Thread.Sleep(rand.Next(0, 300));
}
}
This creates two separate Random instances with different seeds (by using the 1st to seed the second), and then passes these into the Race as arguments. This removes any risk of overly-predictable behaviour due to timing.
You should initialize your random generator outside of the loop.
var rand = new Random();
while (step < 1000)
{
step += rand.Next(0, 10);
Dispatcher.BeginInvoke(new ThreadStart(() => racer.Value = step));
Thread.Sleep(rand.Next(0, 300));
}
For more details please go through this Jon Skeet's article:
https://msmvps.com/blogs/jon_skeet/archive/2009/11/04/revisiting-randomness.aspx
Almost every Stack Overflow question which includes the words "random"
and "repeated" has the same basic answer. It's one of the most common
"gotchas" in .NET, Java, and no doubt other platforms: creating a new
random number generator without specifying a seed will depend on the
current instant of time. The current time as measured by the computer
doesn't change very often compared with how often you can create and
use a random number generator – so code which repeatedly creates a new
instance of Random and uses it once will end up showing a lot of
repetition.
When you create a new Random object, it seeds it from the system clock. This has a resolution of only a few milliseconds, so if you create a new Random object more frequently than that, it will produce the same random sequence as the previous one.
The solution is to create only ONE random object (and use a lock to serialise access to it if more than one thread is accessing it at once).

Class variable set by BackgroundWorker not persisting.

I am brand new to the idea of BackgroundWorkers, so this has me a little perplexed.
So I created a new WPF Application and created a BackgroundWorker and List class variable:
public partial class MainWindow : Window
{
private BackgroundWorker bw = new BackgroundWorker();
private List<int> tempList = new List<int>();
...
Then I use the BackgroundWorker to populate that list: (In the same class btw)
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
Random r = new Random();
for (int j = 1; j <= 100; j++)
{
for (int i = 0; i < 1000; i++)
{
tempList.Add(r.Next(100));
}
...
}
}
Now here is the part that's getting me...
The code that populates that list seems to be working fine. When I step through its execution, *it behaves as I have intended until the exact moment the code exits the bw_DoWork method.* After that point, it reverts back to an empty list. I changed the list to static at one point but nothing changed.
So why is this List not persisting throughout the programs execution?
I am(was) almost certain that this is some issue with the list being allocated in different memory regions for each thread, but I simply know far too little about about BackgroundWorker and MultiThreading in general to diagnose it myself.
Any help would be appreciated.
Before you start using more expensive options such as locking or thread safe collections. Try out Threading Tasks. If they work then you have some kind of a problem with your BackgroundWorker, if they don't then your code touches the list somewhere and you'll have to trace that.. (I just think Tasks are so much easier to work with)
private void bw_DoWork()
{
Task.Factory.StartNew(
() =>
{
var r = new Random();
for (int j = 1; j <= 100; j++)
{
for (int i = 0; i < 1000; i++)
{
tempList.Add(r.Next(100));
}
//the rest of whaterver you're doing...
}
});
}
#Stephen Marsh like #Douglas said you need to wait until work is finish.
See this:
// this execute the DoWork asynchronously.
bw.RunWorkerAsync();
// asynchronously means the next line may be executed
// before the DoWork fill the list. In fact can be executed
// before the DoWork begin.
MessageBox.Show("Without wait: " + tempList.Count.ToString());
To correct you can add this line before call RunWorkerAsync:
bw.RunWorkerCompleted += bw_RunWorkerCompleted;
and put this in any place of the MainWindows class.
void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("Completed: " + tempList.Count.ToString());
}
In my tests always the result was:
"Without wait: 0"
"Completed: 100000"

C# Slow down random number GUI display

I have an application that generates random numbers for about 20 seconds and shows the random number on the fly in a label in the screen.
I want to show the numbers in the same label but then slow down the display of the numbers so like 5 seconds before stoping the process, the display of the number should smoothly slow down more and more until it stops in the final number. Like a raffle.
Any clue?
I can start by telling you what not do to. Do not use Thread.Sleep -- doing so is almost always a "worst practice" and will make your UI unresponsive.
If you use Thread.Sleep on a second thread, as mcl suggests, you won't freeze your UI but you are burning an extremely expensive thread to do very little work.
If you are using C# 4 or earlier then I would create a timer set to tick, say, four times a second. Handle the tick event, and if enough time has passed since the last tick event, change the label. Or, change the interval of the timer each time it ticks.
If you are using C# 5, you can just use await Task.Delay(x):
async void Animate()
{
int delay = 5;
for(int i = 1; i < 10; ++i)
{
UpdateLabel();
await Task.Delay(delay);
delay = delay * 2;
}
}
So now you start with a 5ms delay, then 10, then 20...
Here's a working program to get you started. It changes the Text of the Form for 3 seconds quickly, after which it gets slower. That's achieved by using one Timer to start decelerating the Timer which shows the random numbers.
public partial class Form1 : Form
{
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer() { Interval = 10 };
System.Windows.Forms.Timer timerForStartingSlowDown = new System.Windows.Forms.Timer() { Interval = 3000 };
bool slow = false;
Random random = new Random();
public Form1()
{
InitializeComponent();
timer.Tick += timer_Tick;
timerForStartingSlowDown.Tick += timerForStartingSlowDown_Tick;
Shown += Form1_Shown;
}
void timerForStartingSlowDown_Tick(object sender, EventArgs e)
{
slow = true;
timerForStartingSlowDown.Enabled = false;
}
void Form1_Shown(object sender, EventArgs e)
{
timer.Enabled = true;
timerForStartingSlowDown.Enabled = true;
}
void timer_Tick(object sender, EventArgs e)
{
if (timer.Interval > 350) timer.Enabled = false;
else
{
if (slow) timer.Interval += 10;
Text = random.Next(1, 100).ToString();
}
}
}
Consider generating those numbers on a different thread. You can use BackgroundWorker for that and report the progress as you generate each number. When you begin to reach the end use Thread.Sleep(miliseconds) to "slow" (freez) the BackgroundWorker's job thread that is generating the numbers for a specified amount of miliseconds increasing those miliseconds as you aproach the final number. That should do the trick.
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
Set the BackgroundWorker to report progress and use that to "push" the generated number to the UI thread.
You can also drag and drop the BackgroundWorker component on your form from the Toolbox.

Simple multithreaded program in C# not working

I am trying to write a simple multithreaded program in C#. It has a button pressing which creates a new label on form, and then a for loop runs displaying loop value in label. So if you press button 3 times, it will create 3 threads with 3 labels on form with loop.
When I press the button once, it works fine. But when I press it more than once to create more labels, it runs into following problems:
As soon as button is pressed more than once, it stops the loop in previous thread and runs loop of new thread. If it is multithreaded then it should not stop first loop.
When loop of second label is finished, it gives following error
Object reference not set to an instance of an object
Here is my complete code. The line which throws error is at the end "mylabel[tcount].Text = i.ToString();".
Screenshot of program: http://i.imgur.com/IFMIs.png
Screenshot of code http://i.imgur.com/sIXtc.png
namespace WindowsFormsApplication2{
public partial class Form1 : Form{
public Form1(){
InitializeComponent();
}
private int tcount = 0;
private int y_point = 0;
Thread[] threads = new Thread[5];
Label[] mylabel = new Label[5];
private void button1_Click(object sender, EventArgs e){
threads[tcount] = new Thread(new ThreadStart(work));
threads[tcount].Start();
}
private void work(){
if (this.InvokeRequired){
this.Invoke(new MethodInvoker(delegate{
mylabel[tcount] = new Label();
mylabel[tcount].Text = "label" + tcount;
mylabel[tcount].Location = new System.Drawing.Point(0, y_point + 15);
y_point += 25;
this.Controls.Add(mylabel[tcount]);
for (int i = 0; i < 10000; i++){
mylabel[tcount].Text = i.ToString();
Application.DoEvents();
}
}));
}
tcount++;
}
}
}
If it is multithreaded then it should not stop first loop.
But it is not multithreaded.
this.Invoke(new MethodInvoker(delegate{
This switches via invoker the context back to the UI Thread, so while you open a lot of threads in the background, you basically then put all the processing back into one main thread.
This:
Application.DoEvents();
Then gives other queued work a chance. Still only on the UI thread.
And finally you never parametrize the threads so they all work on the same variables. There is only one non thread save (no lock, no volatile) variable named tCount - bang.
Basically you demonstrate:
Your problem is not solvable multi threaded - any UI element manipulation HAS to happen on the UI thread (which is why you invoke) and as this is all you do you basically can not multithread.
You lack a basic understanding on how UI programs work with threads and the message pump.
You lack a basic understanding on variable scoing and access patterns between threads.
Back to reading documentation I would say.
The problem is the scope of tcount, as all threads acces the same instance of it, so as soon as the second thread starts the first thread also wirtes into the second label.
Also you invoke your whole worker method which will let it run in the UI-Thread again -> not actually multithreaded...
Your worker method should look something like this:
private void work()
{
int tIndex = tCount; //store the index of this thread
tcount++;
mylabel[tIndex] = new Label();
mylabel[tIndex].Text = "label" + tcount;
mylabel[tIndex].Location = new System.Drawing.Point(0, y_point + 15);
y_point += 25;
Invoke((MethodInvoker)delegate() { this.Controls.Add(mylabel[tIndex]); });
for (int i = 0; i < 10000; i++)
{
//doWork
Invoke((MethodInvoker)delegate() { mylabel[tIndex].Text = i.ToString(); });
}
}
Jep, you need to copy tcount to a local variable. As soon as you hit the button twice while a thread has not yet terminated, it is manipulating the second one.

Categories