Changing The Label Text in Button Click - Understanding For Loop - c#

When I clicked button starts for loop from i=0 and I want to see on the label value of i. However I only see last values of i.
public partial class Form1 : Form
{
int i;
public Form1()
{
InitializeComponent();
}
private void btnclick_Click(object sender, EventArgs e)
{
for ( i = 0; i < 3; i++)
{
lblForLoopExample.Text = i.ToString();
System.Threading.Thread.Sleep(1000);
}
}
}
when I run the my code I only see on the label ; 2 .
I want to see such a like below;
When the started For loop, i = 0, I must see the 0 on the Label.Text. An then when the i = 1, I must see 1 on the label.Text. And when i = 2, I must see 2 on the Label.Text.
I added Thread.Sleep(1000) however, result didn't change.
Where I am make mistake?
Please help me,
if you help me , I will appreciate you.
Thanks,

You need to append the lbl to get all the values. Right now, it finishes the loop and give you the last value in your label
public partial class Form1 : Form
{
int i;
public Form1()
{
InitializeComponent();
}
private void btnclick_Click(object sender, EventArgs e)
{
for ( i = 0; i < 3; i++)
{
lblForLoopExample.Text + = i.ToString();
}
}
}

Your problem is that you're doing work on the UI thread while expecting the UI thread to update your form.
While you process your loop, the UI thread is actually executing this code. The UI thread is therefore unable to update the form with the intermediate values you are setting within the loop. Once your code completes, the UI thread is then free to update the form. That's why you see the last value only.
You can see this better if you updated your code to loop ten million times instead of 3. Your form will become unresponsive and will appear locked up. That's because Windows knows your UI thread is locked in an intensive process and is unable to update the UI.
The solution is to use a background thread to run your process and synchronize updates with the UI thread. You'll also have to slow your loop down to see the changes, as others have suggested.
To learn more about how the UI thread works, and how to synchronize background threads with it, read this article (it's about WPF, but it covers the general case).

Each loop you re-write the string. Instead of saying
lblForLoopExample.Text = i.ToString();
You need to add to the string on each iteration. I'd create a variable to hold the value, and so something like this:
string myString = string.Empty;
for(i=0;i < 3; i++)
{
myString += i.ToString() + ", ";
}
lblForLoopExample.Text = myString.substring(0, (myString.length - 1));
substring is just so you don't have that trailing comma. It's kind of dirty code, but it will work.

First of all, your code won't compile as you are missing the exact format of the ToString() method in i.ToString along with the ;
lblForLoopExample.Text = i.ToString();
^^^
As per your code, you should try this:
for ( i = 0; i < 3; i++)
{
lblForLoopExample.Text += i.ToString() + ", ";
}

Related

c# coding a timed operation within a for loop

I am writing a piece of code that takes a user through a guided script. The user will have a few seconds to answer before the answer will be displayed.
So far, my code looks like this:
GuidedExercise3 exercise3 = new GuidedExercise3();
string AntonioAnswer = string.Empty; // expected answer
int upperBound = exercise3.Script.Count - 1; // zero-based counting
for(int i = 0; i < upperBound; i += 2)
{
labelInstructions.Text = exercise3.Script[i].TextToSpeak;
AntonioAnswer = exercise3.Script[i+1].TextToSpeak; // answer
SetTimer(AntonioAnswer, txtAntonio); // set timer sending in the answer and the TextBox object.
sysTimer.Start();
}
The odd lines of a List contain the question and the even lines contain the expected answer. My question is how do I display the question for X seconds and then get the user's answer in this WinForms app and then display the answer when the timer elapses, keeping the user from going to the next step of the script but allowing them to answer the question (which is in a Textbox).
I examined this StackOverflow question, but it doesn't match: Implementing a loop using a timer in C#
Here's how I would handle something like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
MoveNextQuestion();
timerAnswer.Interval = 5000;
timerAnswer.Start();
}
private string[] _questionsAndAnswers = new[]
{
"What colour is the sky?",
"Blue",
"What do chickens lay?",
"Eggs",
};
private int _currentIndex = -2;
private void timerAnswer_Tick(object sender, EventArgs e)
{
MoveNextQuestion();
}
private void buttonAnswer_Click(object sender, EventArgs e)
{
MoveNextQuestion();
}
private void MoveNextQuestion()
{
_currentIndex += 2;
if (_currentIndex < _questionsAndAnswers.Length)
{
labelQuestion.Text = _questionsAndAnswers[_currentIndex];
}
else
{
timerAnswer.Stop();
}
}
}
I was able to get this working fairly easily with a BackgroundWorker object. See the following article at MSDN for the exact coding. BackgroundWorker Class. In particular they have two examples in the documentation and the first example is sufficient. The BackgroundWorker class is allowing my UI to continue to accept user input while waiting on a timed answer. It displays the correct answer on the RunWorkerComplete event. So the BackgroundWorker's RunAsync gets called in my for loop.
I've run into the additional issue of the BackgroundWorker not returning control to my loop. I'm looking into that problem separately.

Why doesn't my counter count?

This really suprised me. One of the simplest things doesn't work, making a counter. I'm making a kinda game application in c# and there is also a timer who counts the time. Very simple right? I know how to code this and this is something I did before but I don't understand why it isn't working now.
int i = 0;
i++;
label1.Text = i.ToString();
label1.Text turns in to 1 and nothing else happens. Also tryied this with a timer but it freeezes in 1. I know this post isn't really going to help other people but it is very frustrating.
Why you are always getting 1 in your label1 text?
The reason is very simple, each time you are getting to the first line, i is 0:
// Line 1
int i = 0; // declaring and setting i to 0
// Line 2
i++; // incrementing i to 1
// Line 3
label1.Text = i.ToString(); // displaying i (which is equal to 1)
and then again you are getting to the Line 1 and setting i=0, etc...
I presume you have a UI application (win form, web form etc...)
You already mentioned you have a timer that works fine and a label where you output the incremented i variable.
As already commented in order to see a change in your label you can use a loop as following:
int length = 100; // for example
for (int i = 0; i < length; i++)
{
label1.Text = i.ToString();
}
The output in label1 text will be 0, then 1, then 2 .... and finally 99.
Obviously you won't be able to see all those values except the last one 99 at run-time but you can debug and see how it works.
I presume, what you needed is to have your label text changing each time the timer will tick.
Following a code example how you could implement it:
private int i = 0; // initialized once in this UI class
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = i.ToString();
i++; // will increment by one each time the timer is ticking
}
Set your timer interval to ~1000 so you could clearly see how your text label increments at run-time.
If you want to increment it you have to add the incrementing logic in a loop like for or while loop:
If you want to use timer to count something please refer to this question : here

Background worker for search and highlight feature

I have a simple while loop that allows me to find text within a textbox, but when I am searching for a word that appears several times in the textbox, it locks up the interface for a while. I'd like to move it into a background worker, but I don't think this can be done because the interface elements (i.e. textbox3.text) are on the main thread. How can I make a background worker when the main interface elements are involved?
I found decent information on the web, but I am having trouble implementing other solutions I have read into my particular situation.
public void button2_Click(object sender, EventArgs e)
{
//Highlight text when search button is clicked
int index = 0;
while (index < dragDropRichTextBox1.Text.LastIndexOf(textBox3.Text))
{
dragDropRichTextBox1.Find(textBox3.Text, index, dragDropRichTextBox1.TextLength, RichTextBoxFinds.None);
dragDropRichTextBox1.SelectionBackColor = Color.Orange;
index = dragDropRichTextBox1.Text.IndexOf(textBox3.Text, index) + 1;
}
}
Thanks for the help.
I guess what you want to do is create sub thread doing the job not to block the UI thread ( pseudo-code, aka not tested ) :
public void button2_Click(object sender, EventArgs e)
{
// Copy text in a non-thread protected string, to be used within the thread sub-routine.
string searchText = textBox3.Text;
string contentText = dragDropRichTextBox1.Text;
// Create thread routine
ThreadPool.QueueUserWorkItem(o => {
// Iterate through all instances of the string.
int index = 0;
while (index < contentText.LastIndexOf(searchText))
{
dragDropRichTextBox1.Invoke((MethodInvoker) delegate {
// Update control within UI thread
//Highlight text when search button is clicked
dragDropRichTextBox1.Find(searchText, index, contentText.Length, RichTextBoxFinds.None);
dragDropRichTextBox1.SelectionBackColor = Color.Orange;
}
// Go to next instance
index = contentText.IndexOf(searchText, index) + 1;
}
});
}
Again, this is untested, but that would give you the idea.
-- EDIT --
You don't need threading at all, doing all the work between a dragDropRichTextBox1.SuspendLayout() and dragDropRichTextBox1.ResumeLayout() is enough.
private void button1_Click(object sender, EventArgs e)
{
// Copy text in a non-thread protected string, to be used within the thread sub-routine.
string searchText = textBox1.Text;
string contentText = richTextBox1.Text;
// Suspend all UI refresh, so time won't be lost after each Find
richTextBox1.SuspendLayout();
// Iterate through all instances of the string.
int index = 0;
while (index < contentText.LastIndexOf(searchText))
{
//Highlight text when search button is clicked
richTextBox1.Find(searchText, index, contentText.Length, RichTextBoxFinds.None);
richTextBox1.SelectionBackColor = Color.Orange;
// Go to next instance
index = contentText.IndexOf(searchText, index) + 1;
}
// Finally, resume UI layout and at once get all selections.
richTextBox1.ResumeLayout();
}
You just need to use invoke when you're manipulating the UI elements in your background thread
https://msdn.microsoft.com/en-us/library/vstudio/ms171728(v=vs.100).aspx
You can call the Invoke Method on the control.
dragDropRichTextBox1.Invoke((MethodInvoker) delegate {
//Your code goes here for whatever you want to do.
}
);
This should fix your problem.

Looping Text label in C#?

i have a timer that changes a label's text each tick. For some reson, it stop and does not continue looping. Why?
private int count = 0;
private void timer1_Tick(object sender, EventArgs e)
{
string[] arr4 = new string[3]; // 4
arr4[0] = "one";
arr4[1] = "two";
arr4[2] = "three";
if (count == 4)
{
count = 0;
}
toolStripStatusLabel1.Text = arr4[count];
count++;
}
Also, when my form loads, the label's text is blank. Then it goes to arr4[0]. When it loops again, the text starts at arr[0]. Why is the text blank first, and how do i fix it?
Looks like your original question was answered in the comments. I'll answer your second question from the comments.
Your timer1_Tick event doesn't execute immediately when your program starts. The first time it executes is after 5000ms, in your case. So the label will show blank at first, then change to the value of arr4[0]. If you don't want that, you could:
set the value of the label in the designer at design time
set the value of the label in the constructor at run time
pull the creation of the array out of the timer tick event so you're not recreating it every 5 seconds, make it a class variable, and create it in the constructor and then set the label to arr4[0] immediately after creating it

WPF - print during a loop

I have a simple issue, but the solution appears to be tricky. I want to print using the WPF control canvas during a loop; but for each iteration, I want to udpate the canvas control.
If I want to print a canvas control in WPF, I can simply call
PrintDialog dialog = new PrintDialog();
dialog.PrintVisual(this.canvas, "");
And it prints as expected to my default printer. Wonderful.
However, if I want to perform this multiple times in a loop and make an update to the canvas during each iteration, only the final iteration of the loop is printed.
private void methodName()
{
for (int i = 0; i < 2; i++)
{
updateTextBox(i.ToString());
PrintDialog dialog = new PrintDialog();
dialog.PrintVisual(this.canvas, "");
}
}
private void updateTextBox(string text)
{
txtTextBox.Text = text;
}
Any idea what I need to do to ensure that I get 2 print outs, the first with the txtTextBox.Text value of 0, the second time it has the value of 1?
I am about to implement something similar in my application and found out that my previous answer wasn't good enough. The problem for me was that although the canvas is updated in each iteration, it has not yet rendered itself before being sent to PrintVisual. It surprises me that you get your final iteration printed, I only got the first one. Anyway, this is how I made it work, basically queueing the print command after the already pending render operation:
for (int i = 0; i < 2; i++)
{
updateTextBox(i.ToString());
this.canvas.InvalidateVisual(); // Maybe not needed in your case
PrintDialog dialog = new PrintDialog();
this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Render, (Action)delegate()
{
dialog.PrintVisual(this.canvas, "" + i);
});
}
Yes it's somewhat similar (but not identical) to SalGad's answer and the post you're referring to, but I'm not able to comment that answer, so please try this out, it works for me.
I also had problems with disappearing prints when using empty description for the print jobs, thus the + i. Don't know if that is a generic problem, or just something with my printer setup.
I got the idea from this post, which also mentions an alternative solution using ViewBox.
OK
I solved it.
I removed all the dispatcher object methods so it runs on a single thread.
To update the canvas, I used the canvas.UpdateLayout() method.
I also ensured that the print had finished before updating the next canvas (the next iteration).
private void methodName()
{
for (int i = 0; i < 2; i++)
{
updateTextBox(i.ToString());
this.canvas.UpdateLayout();
PrintDialog dialog = new PrintDialog();
dialog.PrintVisual(this.canvas, "ABC");
dialog.PrintQueue.Refresh();
while (dialog.PrintQueue.NumberOfJobs != 0)
{
bool isQueued = false;
foreach (var job in dialog.PrintQueue.GetPrintJobInfoCollection())
{
if (job.Name == "ABC")
isQueued = true;
}
if (!isQueued)
break;
Thread.Sleep(500);
dialog.PrintQueue.Refresh();
}
}
}
private void updateTextBox(string text)
{
txtTextBox.Text = text;
}
I also could have just done thread.sleep(3000) - this worked as it was enough time to ensure the print job had completed, but it was also a little bit 'hopeful' and I wanted something more secure.
Thank you to everyone for your suggestions.
If you are going to call PrintVisual multiple times you have to look into PrintDocument and DocumentPaginator.
Just a guess, but it might be worth trying RenderTargetBitmap to force rendering the canvas in each iteration, and then create an Image with that source, which then can be sent to PrintVisual. See this post for code example:
Printing viewport
Just taking a shot here, but can you try refreshing the WPF canvas controls at the start of every for loop iteration? Here is the code snippet:
// declare this
public static class ExtensionMethods
{
private static Action EmptyDelegate = delegate() { };
public static void Refresh(this UIElement uiElement)
{
uiElement.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);
}
}
// the loop
for (int i = 0; i < 2; i++)
{
updateTextBox(i.ToString());
PrintDialog dialog = new PrintDialog();
dialog.PrintVisual(this.canvas, "");
}
}
// update this method
private void updateTextBox(string text)
{
txtTextBox.Text = text;
txtTextBox.Refresh();
Thread.Sleep(500);
}
I am sourcing this idea from here

Categories

Resources