【发布时间】:2020-06-04 05:59:20
【问题描述】:
这是我的第一个 C# 项目。我有两个 .cs 文件,一个包含所有与 GUI 相关的东西(Form1.cs),另一个包含我的处理(GMF.cs)。当用户单击 GUI 上的“开始”(Button)时,控制权传递给 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);
}
在 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] 有一个文本框(我简单地从工具箱中拖放了它)。我重命名了 (Name) = textBox_imageNumber
- 每次进入 for 循环时,值都会增加 1。
- 这样做的基本目的是因为我希望用户知道(在表单应用程序上显示一条消息)每次 for 循环完成 1000 次迭代。
当我运行上面的代码时,我注意到以下内容:
在控制台窗口中,输出如预期:
Text = 1, Text = 2, ... Text = 100程序继续。 - 这表明 textBox_ImgNumber.Text 中的变量值在 GMF.cs 的 for 循环内被更新,并到达 Form1.cs。但是,在窗体上,当程序运行时(在 for 循环内),文本框中的值是空白的。只要从
func_out = n.GMFMat(inputs_GUI, this.progressBar1, this.textBox_imageNumber);出来,textBox的值就显示为100。
我希望在 for 循环迭代时更新表单上文本框的值(正如我在控制台窗口中看到的那样)。我该怎么做?
我在 Visual Studio 2010 上使用 Visual Studio、C#、.NET Framework 4 客户端配置文件
我知道我的表单在处理等方面运行良好,因为其他功能在两个 .cs 文件之间可以很好地通信。
例如,我还有一个进度条,我可以在程序执行时看到它的进度。
【问题讨论】:
-
GMF n = new GMF();这个表单永远不会显示,因此用户永远不会与之交互。 -
感谢您的 cmets。我可能会更新我的问题以解决您的上述观点。
标签: c# .net winforms visual-studio-2010