2017-10-19 4 views
-1

私はそれを理解しようとしていますが、それは容易ではありません。私はクラスのトップで : 私はコンストラクタに続いonSearchProgress進捗レポートイベントとハンドラを作成するにはどうすればよいですか?

private EventHandler onSearchComplete; 
private EventHandler onSearchProgress; 

を追加: IはonSearchProgress =新しいイベントハンドラ(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のトップに:

Form1のコンストラクタで
private DirectorySearcher directorySearcher; 

public Form1() 
{   
    InitializeComponent(); 

    directorySearcher.SearchProgress += DirectorySearcher_SearchProgress; 
} 

そして

private void DirectorySearcher_SearchProgress(object sender, EventArgs e) 
{ 

} 

しかし、どのように私はクラスから報告すれば、どのように私はイベントでのForm1のレポートを得るのですか?たとえば、form1のようなもの:

多分、複数のレポートをクラスに追加して、Form1で複数のラベルを更新することができます。

private void DirectorySearcher_SearchProgress(object sender, EventArgs e) 
{ 
    label1.Text = e.textlabel; 
    label2.Text = e.textlabel1; 
    int counting = e.counter; 
} 

答えて

0

AddFiles()メソッドのどこかでOnSearchProgress()を呼び出す必要があります。追加情報(この場合はtextLabel)を渡すか、プロパティを使用するには、EventHandlerから派生した新しいクラスを作成する必要があります。

関連する問題