locked
EventArgs data usage RRS feed

  • Question

  • I have three classes, the FileManagerClass contains two events which are EventHandler<FileTransferEventArgs> FileMove;, and EventHandler<FileTransferEventArgs> FileMoveError;. I then have a custom EventArgs class named FileTransferEventArg that inherits from the base EventArgs class. I then have a third class called FolderDirectoryModel that then listens for the events to be raised and responds to it with either a OnFileMove(object sender, FileTransferEventArg arg) or OnFileMoveError(object sender, FileTransferEventArg arg). My problem is that I would like to use the properties inside my FileTransferEventArgs class to display the file name that was moved and if it was successful or not detailing a reason that will be inside the message property. However when I get every thing wired up im not able to view the data that is passed along. I am not sure if i am improperly using the events or I am trying to do something that is not possible.

    class FileTransferManager { public event EventHandler<FileTransferEventArgs> FileMove; public event EventHandler<FileTransferEventArgs> FileMoveError; } public class FileTransferEventArgs : EventArgs { string FileName { get; set; } string Message { get; set; } internal FileTransferEventArgs(FileModel file, string message) { this.FileName = file.FileName; this.Message = message; } } public class FolderDirectoryModel { void TransferFile(FileModel toBeTransfered, string destinationPath) { if (!File.Exists(toBeTransfered.FilePath)) { File.Move(toBeTransfered.FilePath, destinationPath); FileTransferEvents.FileTransferManager TransferAgent = new FileTransferEvents.FileTransferManager(); TransferAgent.FileMove += new EventHandler<FileTransferEventArgs>(OnFileMove); } else { FileTransferEvents.FileTransferManager TransferAgent = new FileTransferEvents.FileTransferManager(); TransferAgent.FileMoveError += new EventHandler<FileTransferEventArgs>(OnFileMoveError); } } //Handler Methods private void OnFileMoveError(object sender, FileTransferEventArgs args) { //what i want to happen

    MessageBox.Show($"File {args.FileName} has not been moved succefully to {args.FilePath} because of...."); } private void OnFileMove(object sender, FileTransferEventArgs args) {

    //what i want to happen

    MessageBox.Show($"File {args.FileName} has been moved successfully to {args.FilePath} } }




    Recht


    • Edited by Rechtfertigung Saturday, June 20, 2020 1:47 PM fixed variable names and added )
    Saturday, June 20, 2020 1:43 PM

Answers

  • Hello,

    Here is a basic example.

    public class DelegatesModule
    {
        public delegate void MonitorHandler(MonitorArgs args);
    }

    EventArgs

    public class MonitorArgs : EventArgs
    {
        protected string _text;
        protected int _value;
    
        public MonitorArgs(string text, int value)
        {
            _text = text;
            _value = value;
    
        }
        public string Text => _text;
        public int Value => _value;
    
    }

    Then in a class

    public class Operations
    {
        public event MonitorHandler OnMonitor;

    In a method of the class above

    public async Task<int> Run(int value, System.Threading.CancellationToken token)
    {
        var currentIndex = 0;
    
        while (currentIndex <= value -1)
        {
    
            OnMonitor?.Invoke(new MonitorArgs("Working", currentIndex));
    
            currentIndex += 1;
    
            await Task.Delay(1, token);
            
            if (token.IsCancellationRequested) token.ThrowIfCancellationRequested();
        }
    
        OnMonitor?.Invoke(new MonitorArgs("Done", currentIndex));
        return currentIndex;
    }

    Here we subscribe to the event

    var ops = new Operations();
    ops.OnMonitor += OnMonitor;

    Here a progressbar uses Value from MonitorArgs and Text is used one the operation is done.

    private void OnMonitor(MonitorArgs args)
    {
        progressBar1.Value = args.Value;
        if (args.Value == _totalIterations)
        {
            MessageBox.Show(args.Text);
        }
    }



    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    • Edited by KareninstructorMVP Sunday, June 21, 2020 1:39 AM bolded code
    • Proposed as answer by Naomi N Sunday, June 21, 2020 12:46 PM
    • Marked as answer by Rechtfertigung Sunday, June 21, 2020 2:14 PM
    Sunday, June 21, 2020 1:38 AM