C# 如何创建进度报告事件和处理程序?
我想弄明白,但这并不容易。 在班上的第一名,我做到了: 我添加了onSearchProgressC# 如何创建进度报告事件和处理程序?,c#,winforms,C#,Winforms,我想弄明白,但这并不容易。 在班上的第一名,我做到了: 我添加了onSearchProgress private EventHandler onSearchComplete; private EventHandler onSearchProgress; 然后在构造函数中: 我添加了onSearchProgress=newEventHandler(onSearchProgress) 然后 这是我想作为报告发送并以表格1形式获取的部分: public int countf = 0; public
private EventHandler onSearchComplete;
private EventHandler onSearchProgress;
然后在构造函数中:
我添加了onSearchProgress=newEventHandler(onSearchProgress)
然后
这是我想作为报告发送并以表格1形式获取的部分:
public int countf = 0;
public void AddFiles(string[] files, int startIndex, int count)
{
while (count-- > 0)
{
listBox.Items.Add(files[startIndex + count]);
countf++;
textlabel.Text = countf.ToString();
}
}
及
现在在表格1顶部:
private DirectorySearcher directorySearcher;
在Form1构造函数中:
public Form1()
{
InitializeComponent();
directorySearcher.SearchProgress += DirectorySearcher_SearchProgress;
}
及
但我如何从课堂上报告,如何在活动中获得Form1中的报告?例如,在表格1中,类似于:
private void DirectorySearcher_SearchProgress(object sender, EventArgs e)
{
label1.Text = e.textlabel;
}
也许可以在类中添加多个报告,以便我可以更新Form1中的多个标签。例如:
private void DirectorySearcher_SearchProgress(object sender, EventArgs e)
{
label1.Text = e.textlabel;
label2.Text = e.textlabel1;
int counting = e.counter;
}
您应该在AddFiles()方法的某个地方调用OnSearchProgress()。您需要创建一个从EventHandler派生的新类来传递附加信息(本例中为textLabel)或为此使用属性
public Form1()
{
InitializeComponent();
directorySearcher.SearchProgress += DirectorySearcher_SearchProgress;
}
private void DirectorySearcher_SearchProgress(object sender, EventArgs e)
{
}
private void DirectorySearcher_SearchProgress(object sender, EventArgs e)
{
label1.Text = e.textlabel;
}
private void DirectorySearcher_SearchProgress(object sender, EventArgs e)
{
label1.Text = e.textlabel;
label2.Text = e.textlabel1;
int counting = e.counter;
}