Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 显示文本框。Windows窗体中带有“文本”的文本;idx“;遍历for循环_C#_.net_Winforms_Visual Studio 2010 - Fatal编程技术网

C# 显示文本框。Windows窗体中带有“文本”的文本;idx“;遍历for循环

C# 显示文本框。Windows窗体中带有“文本”的文本;idx“;遍历for循环,c#,.net,winforms,visual-studio-2010,C#,.net,Winforms,Visual Studio 2010,这是我的第一个C#项目。我有两个.cs文件,一个包含所有与GUI相关的内容(Form1.cs),另一个包含我的处理(GMF.cs)。当用户单击GUI上的“开始”(一个按钮)时,控制传递到Form1.cs,它在内部调用GMF.cs文件中的GMFMat() 在Form1.cs中,我有: private void button1_Click(object sender, EventArgs e) // Start Processing button clicked. { GMF n = new

这是我的第一个C#项目。我有两个.cs文件,一个包含所有与GUI相关的内容(Form1.cs),另一个包含我的处理(GMF.cs)。当用户单击GUI上的“开始”(一个
按钮
)时,控制传递到Form1.cs,它在内部调用GMF.cs文件中的GMFMat()

在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);
}
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());
    }
}
}
在GMF.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);
}
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());
    }
}
}
类转基因食品
{
公共元组GMFMat(从GUI、System.Windows.Forms.ProgressBar栏、System.Windows.Forms.TextBox TextBox\u imgnNumber中输入参数)
{
双N_文件=0;
double num_files=100
foreach(dirs中的string dir)//对于我们的示例,假设为100个ierations
{
第n个文件=第n个文件+1;
textBox_ImgNumber.Text=n_file.ToString();
//进度条在每次迭代中都会更新,我可以在执行期间在windows窗体上看到进度条的进展。
bar\u value\u percent=n个文件/num\u文件*100;
bar.Value=int.Parse(Math.Truncate(bar\u Value\u percent.ToString());
}
}
}
cs[Design]有一个文本框(我简单地从工具箱中拖拽)。我重命名了(名称)=文本框\u图像编号

  • 该值每次进入for循环时递增1
  • 这样做的基本目的是,每当for循环完成1000次迭代时,我希望用户知道(在表单应用程序上显示一条消息)
当我运行上面的代码时,我注意到以下几点:

  • 在控制台窗口上,输出与预期一样:
    Text=1,Text=2。。。Text=100
    随着程序的进行。-这表明textBox_ImgNumber.Text中变量的值正在GMF.cs的for循环中更新,并达到Form1.cs

  • 但是,在窗体上,当程序运行时(在for循环内),文本框中的值为空。一旦它从
    func\u out=n.GMFMat(输入\u GUI、this.progressBar1、this.textBox\u imageNumber)中出来,文本框的值显示为100

  • 我希望表单上文本框的值在for循环迭代时更新(正如我在控制台窗口中看到的)。我该怎么做

    我使用Visual Studio 2010、.NET Framework 4客户端配置文件上的Visual Studio、C#

    我知道我的表单在处理等方面运行良好,因为其他功能在两个.cs文件之间进行了良好的通信

    例如,我还有一个进度条,我可以在程序执行时看到它的进度

    我找到了解决办法

    我必须包括以下行:
    Application.DoEvents()

    因此,在Form1.cs中:

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

    GMF n=新的GMF()此表单从未显示,因此用户从未与之交互。是否尝试?谢谢您的评论。我可能会更新我的问题,以解决上述问题。