简体   繁体   English

在 Windows 窗体中显示 textBox.Text 并使用“idx”迭代 for 循环

[英]Display textBox.Text in Windows Form with "idx" iterating through for loop

This is my first C# project.这是我的第一个 C# 项目。 I have two .cs files, one which has all the GUI related stuff (Form1.cs) and one which has my processing (GMF.cs).我有两个 .cs 文件,一个包含所有与 GUI 相关的内容(Form1.cs),另一个包含我的处理(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.当用户单击 GUI 上的“开始”(一个Button )时,控制传递到 Form1.cs,后者在内部调用 GMF.cs 文件中的 GMFMat()。

In Form1.cs, I have:在 Form1.cs 中,我有:

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:在 GMF.cs 中,我有:

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). Form1.cs[Design] 有一个 TextBox(我简单地从 Toolbox 拖放)。 I renamed (Name) = textBox_imageNumber我重命名 (Name) = textBox_imageNumber

  • The value increments by 1 every-time it enters the for loop.每次进入 for 循环时,该值都会增加 1。
  • 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.这样做的基本目的是因为我希望用户在每次 for 循环完成 1000 次迭代时都知道(在表单应用程序上显示一条消息)。

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.在控制台窗口中,输出如预期:随着程序的进行Text = 1, Text = 2, ... Text = 100 - 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. -这表明textBox_ImgNumber.Text 中的变量值正在GMF.cs 的for 循环内更新,并到达Form1.cs。

  2. However, on the Form, the value in text box is blank while the program runs (inside the for loop).但是,在窗体上,当程序运行时(在 for 循环内),文本框中的值是空白的。 As soon as it comes out of func_out = n.GMFMat(inputs_GUI, this.progressBar1, this.textBox_imageNumber);只要出来func_out = n.GMFMat(inputs_GUI, this.progressBar1, this.textBox_imageNumber); , the textBox's value is displayed as 100. ,textBox 的值显示为 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).我希望表单上文本框的值在 for 循环迭代时更新(如我在控制台窗口中看到的)。 How can I do this?我怎样才能做到这一点?

I use Visual Studio, C# on Visual Studio 2010, .NET Framework 4 Client Profile我在 Visual Studio 2010 上使用 Visual Studio、C#、.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.我知道我的表单在处理等方面运行良好,因为其他功能在两个 .cs 文件之间进行了良好的沟通。

For example, I also have a progress bar whose progress I can see when the program is being executed.例如,我还有一个进度条,我可以在程序执行时看到进度条。

I found the solution.我找到了解决方案。

I had to include the line: Application.DoEvents();我必须包括这一行: Application.DoEvents();

So in Form1.cs:所以在 Form1.cs 中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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