-1

This is my first C# project. I have two .cs files, one which has all the GUI related stuff (Form1.cs) and one which has my processing (GMF.cs). When "Start" (a Button) on GUI is clicked by user, control passes to Form1.cs, which internally calls GMFMat() present in GMF.cs file.

In Form1.cs, I have:

private void button1_Click(object sender, EventArgs e) // Start Processing button clicked.
{
    GMF n = new GMF();
    func_out = n.GMFMat(inputs_GUI, this.progressBar1, this.textBox_imageNumber);
}

private void timer1_Tick(object sender, EventArgs e) // Increment Progress Bar
{
    progressBar1.Increment(1);
}

private void textBox_imageNumber_TextChanged(object sender, EventArgs e)
{
    this.Text = textBox_imageNumber.Text;
    Console.Write("Text = " + textBox_imageNumber.Text);
}

In GMF.cs, I have:

class GMF
{
public Tuple<string, int> GMFMat(in_params input_from_GUI, System.Windows.Forms.ProgressBar bar, System.Windows.Forms.TextBox textBox_ImgNumber) 
{
    double nth_file = 0;
    double num_files = 100
    foreach (string dir in dirs) // For our example, say 100 ierations
    {
        nth_file = nth_file + 1;
        textBox_ImgNumber.Text = nth_file.ToString();
        // Progress bar updates in each iteration, and I can see it as it progresses on the windows Form during execution. 
        bar_value_percent = nth_file / num_files * 100;
        bar.Value = int.Parse(Math.Truncate(bar_value_percent).ToString());
    }
}
}

Form1.cs[Design] has a TextBox (which I simlpy dragged and dropped from Toolbox). I renamed (Name) = textBox_imageNumber

  • The value increments by 1 every-time it enters the for loop.
  • The basic purpose of doing this is because I want the user to know (Display a message on Form Application) everytime the for loop finishes 1000 iterations.

When I run the code above, I notice the following:

  1. On the Console window, the output is as expected: Text = 1, Text = 2, ... Text = 100 as the program proceeds. - this shows that the value of variable in textBox_ImgNumber.Text is being updated inside the for loop of GMF.cs, and reaches Form1.cs.

  2. However, on the Form, the value in text box is blank while the program runs (inside the for loop). As soon as it comes out of func_out = n.GMFMat(inputs_GUI, this.progressBar1, this.textBox_imageNumber);, the textBox's value is displayed as 100.

I want the value of text box on the form to be updated as and when the for loop iterates (as I see on Console window). How can I do this?

I use Visual Studio, C# on Visual Studio 2010, .NET Framework 4 Client Profile

I know my Form is working well with respect to processing, etc since other functions are communicated well between the two .cs files.

For example, I also have a progress bar whose progress I can see when the program is being executed.

Saania
  • 625
  • 1
  • 6
  • 26

1 Answers1

0

I found the solution.

I had to include the line: Application.DoEvents();

So in Form1.cs:

private void textBox_imageNumber_TextChanged(object sender, EventArgs e)
{
     this.Text = textBox_imageNumber.Text;
     Console.Write("Text = " + textBox_imageNumber.Text);
     Application.DoEvents();
}
Saania
  • 625
  • 1
  • 6
  • 26