Event - Delegates are the key feature of C#,You can say heart of C#.Look on a example :- Suppose if you have two forms and there is no communication between those even you can't create object to access then how will you send some information.In this tutorial i will teach you how to handle and play with delegates to proper communication.
Suppose you have to pass the multiple file paths to different forms and they are in different solutions or different forms :-
Step.1 :-
Make a class which will contain all the file path like this :-
public class FileOpenEventArgs : EventArgs
{
#region Private Fields
private string [] _filePath;
#endregion
#region properties
///
/// Sets/Gets the path of files.
///
public string [] FilePath
{
get
{
return _filePath;
}
set
{
_filePath = value;
}
}
#endregion
}
step.2 :- Now come to form from you have to pass the information
Declare a delegate and event like :-
public delegate void FileOpenHandler(object sender, FileOpenEventArgs e);
public event FileOpenHandler FileOpen;
step.3 :- Now do the operation suppose you have to send the file name on drag-drop or on a mouse click or on mouse hover any event then create the object of the class and set the file names and then pass like in this way :-
I have used on drag-drop so get the file names in this way :-
string[] files = (string[])drgevent.Data.GetData(DataFormats.FileDrop);
or you can do other way to get the file names.
Create the object and set the file names.
FileOpenEventArgs fileOpen = new FileOpenEventArgs();
fileOpen.FilePath = files;
OnFilesOpen(this, fileOpen);
Handles FileOpen event.
protected void OnFilesOpen(object sender, FileOpenEventArgs e)
{
if (FileOpen != null)
{
FileOpen(this, e);
}
}
Step.4 :- Now handle your event on the form where you can create the object of this form or you can handle the event here too but I handled the event on onother solution like :-
private solution.explorer lightView;
then again create delegate and event like:-
public delegate void FileOpenHandler(object sender, FileDataOpenEventArgs e);
public event FileOpenHandler FileOpen;
Now handles the event :-
lightView.FileOpen += new solution.explorer.FileOpenHandler(lightView_FileOpen);
void lightView_FileOpen(object sender, FileOpenEventArgs e)
{
string [] fileData = e.FilePath;
FileDataOpenEventArgs fileDataOpenEventArgs = new FileDataOpenEventArgs(fileData);
OnFilesOpen(fileDataOpenEventArgs);
}
Handle protected method :-
protected void OnFilesOpen(FileDataOpenEventArgs e)
{
if (FileOpen != null)
{
FileOpen(this, e);
}
}
now create the event where you have to open or do something for files :-
create the instance of above class where you handle your event.
private lightView_lightViewPanel = new lightView();
_lightViewPanel.FileOpen += new lightView.FileOpenHandler(_lightViewPanel_FileOpen);
private void _lightViewPanel_FileOpen(object sender, FileDataOpenEventArgs e)
{
if (e != null)
{
now play with your path accesss e.FilePath property of this event you will get path.
}
}
Happy to code.
Suppose you have to pass the multiple file paths to different forms and they are in different solutions or different forms :-
Step.1 :-
Make a class which will contain all the file path like this :-
public class FileOpenEventArgs : EventArgs
{
#region Private Fields
private string [] _filePath;
#endregion
#region properties
///
/// Sets/Gets the path of files.
///
public string [] FilePath
{
get
{
return _filePath;
}
set
{
_filePath = value;
}
}
#endregion
}
step.2 :- Now come to form from you have to pass the information
Declare a delegate and event like :-
public delegate void FileOpenHandler(object sender, FileOpenEventArgs e);
public event FileOpenHandler FileOpen;
step.3 :- Now do the operation suppose you have to send the file name on drag-drop or on a mouse click or on mouse hover any event then create the object of the class and set the file names and then pass like in this way :-
I have used on drag-drop so get the file names in this way :-
string[] files = (string[])drgevent.Data.GetData(DataFormats.FileDrop);
or you can do other way to get the file names.
Create the object and set the file names.
FileOpenEventArgs fileOpen = new FileOpenEventArgs();
fileOpen.FilePath = files;
OnFilesOpen(this, fileOpen);
Handles FileOpen event.
protected void OnFilesOpen(object sender, FileOpenEventArgs e)
{
if (FileOpen != null)
{
FileOpen(this, e);
}
}
Step.4 :- Now handle your event on the form where you can create the object of this form or you can handle the event here too but I handled the event on onother solution like :-
private solution.explorer lightView;
then again create delegate and event like:-
public delegate void FileOpenHandler(object sender, FileDataOpenEventArgs e);
public event FileOpenHandler FileOpen;
Now handles the event :-
lightView.FileOpen += new solution.explorer.FileOpenHandler(lightView_FileOpen);
void lightView_FileOpen(object sender, FileOpenEventArgs e)
{
string [] fileData = e.FilePath;
FileDataOpenEventArgs fileDataOpenEventArgs = new FileDataOpenEventArgs(fileData);
OnFilesOpen(fileDataOpenEventArgs);
}
Handle protected method :-
protected void OnFilesOpen(FileDataOpenEventArgs e)
{
if (FileOpen != null)
{
FileOpen(this, e);
}
}
now create the event where you have to open or do something for files :-
create the instance of above class where you handle your event.
private lightView_lightViewPanel = new lightView();
_lightViewPanel.FileOpen += new lightView.FileOpenHandler(_lightViewPanel_FileOpen);
private void _lightViewPanel_FileOpen(object sender, FileDataOpenEventArgs e)
{
if (e != null)
{
now play with your path accesss e.FilePath property of this event you will get path.
}
}
Happy to code.