【发布时间】:2025-12-14 09:35:02
【问题描述】:
我对 Visual C# 比较陌生。
我正在创建一个程序,其中有一个图片框列表。我有一个 for 循环,它根据图片框中的图像播放 .wav 文件。我正在使用 PlaySync 方法,因此对于循环中的每个 i,都会播放一个 .wav 文件(对应于不同的图片框)直到结束,然后 i 增加,然后播放不同的音符。
对于每个音符(基本上是循环的每个 i),我想更改正在播放 .wav 文件的图片框的背景颜色。
除非我在循环中添加消息框,否则代码运行时不会为图片框着色。我很确定我在循环中需要一个中断器,以便让程序有机会为图片框着色。我曾尝试使用延迟 (Thread.Sleep()) 来破坏程序,但它不起作用。
我需要中断循环吗?如果是,什么会起作用?
这是我的代码:
*arrindex 是一个整数,表示要演奏的音符数量
*pboxlist = 图片框列表
*tempind = 图片框列表的索引
for (int i = 0; i <= arrindex; i++)
{
if (tempind < pboxlist.Count()) //coloring the pictureboxes
{
if (tempind != -1)
{
pboxlist[tempind].BackColor = Color.Transparent; //coloring the previous picturebox back to transparent
}
tempind++;
pboxlist[tempind].BackColor = Color.LightPink;
}
regwav[row, col].PlaySync(); // a matrix where the .wav files are stored (row and col are integers that determine which .wav file to play)
}
【问题讨论】:
-
在 Windows 窗体中,您可以使用 Application.DoEvents 来更新 UI,但最好将此代码移动到工作线程。
-
GUI 编程中非常重要的编程细节,您的 UI 不会更新,直到您的程序再次空闲。不要使用 for() 循环,而是使用 Timer。
标签: c#