Hi everyone,

I'm working on an app that needs to log all of the events of every control and place them into a checkedlistbox. For example, if you click a button on the form, it should show the information in the checkedlistbox like 'button1 Clicked: x amount of times,' and so on for other controls. Is there a way to do this? I haven't really found much info for this specific purpose. Thanks.


Here's the example my professor gave:
http://img188.imageshack.us/img188/8611/checkedlistbox.jpg

Hmm, it seems that this person made some 'replay / play' functionality into the form. I dont really understand how that was done, as the code is still a bit above my head. I'm looking for the easier route. I only want the event of something to be displayed into the checked list box whenever it is fired. So the 'button' event wouldn't go into the listbox until it was clicked, etc. Is there a more simple way to do this? My professor said I should make some sort of method to log the events called 'LogIt(sender).' Any ideas?

Ok, so this was a bit more simple than I thought... Here's the method I created to display the control's event info

private void LogIt(object sender)
{
checkedList.Items.Add((string)sender.ToString());
}

Then after the click event of every control I simply put LogIt(sender) and it sends the info into the listbox.
My new problem is that the checked list box is on a different form called 'frmEventInfo.' I have a button on the main form that displays frmEventInfo. I want to keep the LogIt method and have all of the events that it has saved sent onto the listbox on frmEventInfo. How can I do that?

The thread I referred to did have a replay function BUT in order to replay it had to log every event -- which is what you want. Just remove the replay functionality part of the code and away you go.

Ok, so this was a bit more simple than I thought... Here's the method I created to display the control's event info

private void LogIt(object sender)
{
checkedList.Items.Add((string)sender.ToString());
}

Then after the click event of every control I simply put LogIt(sender) and it sends the info into the listbox.
My new problem is that the checked list box is on a different form called 'frmEventInfo.' I have a button on the main form that displays frmEventInfo. I want to keep the LogIt method and have all of the events that it has saved sent onto the listbox on frmEventInfo. How can I do that?

Here is an example that you can see how to make your method a delegate passed to another form. Copy this code into a source file's class and just call the YourClassName.TestDelegate() method while stepping through the code and you will see what is happening:

public delegate void Delegate_LogIt(object sender);

        public class MyMainForm
        {
            private void LogIt(object sender)
            {
                // commenting out your code line so this will compile
                //checkedList.Items.Add((string)sender.ToString());
                Console.WriteLine(sender.ToString());
            }
            public void Run()
            {
                RunBogusForm2();
            }

            public void RunBogusForm2()
            {
                // pass in method LogIt to new form as delegate
                MyCalledForm bogusForm = new MyCalledForm(LogIt);
                int justAnObject = 3000;
                bogusForm.RaiseFakeEvent(justAnObject);
            }
        }
        public class MyCalledForm
        {
            public Delegate_LogIt delegate_LogIt;
            public MyCalledForm(Delegate_LogIt delegate_LogIt)
            {
                this.delegate_LogIt = delegate_LogIt;
            }

            // mimicking an event
            public void RaiseFakeEvent(object sender)
            {
                delegate_LogIt(sender); // will call your LogIt(sender) method
            }
        }
        public static void TestDelegate()
        {
            MyMainForm bogusForm = new MyMainForm();

            bogusForm.Run();
        }

Thank's for the help :P

If you can understand what is happening in the example I gave you, then you should be able to achieve your objective as you have described it. I also highly recommend you look at the thread that Scott directed you to, which is a fine display of how you can also "wire" the delegate to events (if I remember it correctly).

Yes I used some code I got from Scott's example.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.