如何创建进度报告事件和处理程序?

时间:2017-10-19 10:32:08

标签: c# winforms

我想弄明白,但这并不容易。 在我上课的顶部: 我添加了onSearchProgress

private EventHandler onSearchComplete;
private EventHandler onSearchProgress;

然后在构造函数中: 我添加了onSearchProgress = new EventHandler(OnSearchProgress);

public DirectorySearcher()
{
    listBox = new ListBox();
    listBox.Dock = DockStyle.Fill;

    Controls.Add(listBox);

    fileListDelegate = new FileListDelegate(AddFiles);
    onSearchComplete = new EventHandler(OnSearchComplete);
    onSearchProgress = new EventHandler(OnSearchProgress);
}

然后

public event EventHandler SearchComplete;
public event EventHandler SearchProgress;

这是我要作为报告发送的部分,并在form1中获取:

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();
    }
}

private void OnSearchProgress(object sender, EventArgs e)
{
    SearchProgress(sender, e);
}

现在在Form1顶部:

private DirectorySearcher directorySearcher;

在Form1构造函数中:

public Form1()
{           
    InitializeComponent();

    directorySearcher.SearchProgress += DirectorySearcher_SearchProgress;
}

并且

private void DirectorySearcher_SearchProgress(object sender, EventArgs e)
{

}

但是如何从课堂报告,如何在活动中获得Form1中的报告?例如,在form1中,例如:

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;
}

1 个答案:

答案 0 :(得分:0)

你应该在AddFiles()方法的某个地方调用OnSearchProgress()。您需要创建一个派生自EventHandler的新类来传递您的附加信息(在本例中为textLabel)或使用属性。