Witam,
Chciałem sobie zrobić w mojej apce otwieranie plików poprzez np. metodę przeciągnięcia pliku na forma.
Tutaj jest super przykład:
http://snipplr.com/view/47126/
Zastanawia mnie (po prostu nie czuję tego i prosiłbym o wytłumaczenie) czemu musimy użyć delegacji?
private void Form1_DragDrop(object sender, DragEventArgs e) {
//good idea to use try-catch block, in case something goes wrong
try {
Array a = (Array)e.Data.GetData(DataFormats.FileDrop);
if (a != null) {
// Extract string from first array element
// (ignore all files except first if number of files are dropped).
string s = a.GetValue(0).ToString();
// Call OpenFile asynchronously.
// Explorer instance from which file is dropped is not responding
// the entire time that the DragDrop handler is active, so we need to return
// immidiately (especially if OpenFile shows MessageBox).
this.BeginInvoke(_openFileDelegate, new Object[] { s });
this.Activate(); // in the case Explorer overlaps this form
}
}
catch (Exception ex) {
Trace.WriteLine("Error in DragDrop function: " + ex.Message);
// don't show MessageBox here - Explorer is waiting !
}
}
Czemu nie można tego zrobić tak(bez użycia delegacji)
private void Form1_DragDrop(object sender, DragEventArgs e) {
//good idea to use try-catch block, in case something goes wrong
try {
Array a = (Array)e.Data.GetData(DataFormats.FileDrop);
if (a != null) {
// Extract string from first array element
// (ignore all files except first if number of files are dropped).
string s = a.GetValue(0).ToString();
// Call OpenFile asynchronously.
// Explorer instance from which file is dropped is not responding
// the entire time that the DragDrop handler is active, so we need to return
// immidiately (especially if OpenFile shows MessageBox).
OpenFile(s); //ZAMIANA!!
this.Activate(); // in the case Explorer overlaps this form
}
}
catch (Exception ex) {
Trace.WriteLine("Error in DragDrop function: " + ex.Message);
// don't show MessageBox here - Explorer is waiting !
}
}