Monday, January 28, 2008

C# Events

using System;
using System.Windows.Forms;
namespace EventsMadeSimple
{
    public partial class Form1 : Form
    {
        SimpleClass SimpleObject = new SimpleClass(); // create a new SimpleClass object
        public Form1() // Form1 constructor
        {
            InitializeComponent();
            Load += new EventHandler(Form1_Load); // add Form1's Load event handler
            // add our object's event handler
            SimpleObject.SimpleEvent += new OnSimpleEvent(SimpleEventOccurred);
        }
        void Form1_Load(object sender, EventArgs e)
        {
            SimpleObject.SimpleMethod(); // call the SimpleObject's method
        }
        private void SimpleEventOccurred() // method is called when the event is raised
        {
            listBox1.Items.Add("SimpleEvent was fired at " + DateTime.Now.ToString());
        }
    }
    public delegate void OnSimpleEvent(); // delegate
    public class SimpleClass
    {
        public event OnSimpleEvent SimpleEvent; // event
        public void SimpleMethod()
        {
            SimpleEvent(); // raise the event
        }
    }
}
 

There are two classes here. The standard windows generated Form1 and SimpleClass. SimpleClass is our own class with one event and one method.

Inside Form1 we've created a new instance of SimpleClass called SimpleObject.

In Form1's constructor, we've added an event handler (this makes it 'listen' for the specified event and call the specified method) for loading of Form1, and another for our SimpleObject's event. This calls the SimpleEventOccurred method in Form1 when the event is raised.

In Form1's load method we call SimpleObject's SimpleMethod, which is where the event gets raised. This method could be called from anywhere but I've placed it in the Load so it happens immediately when you run the project.

How easy was that?

Experiment Suggestions

Passing parameters: You will often see something similar to this in the signatures of methods called by events:

(object sender, EventArgs e)

If you want to do this or anything similar you will also need to give your delegate the same signature and pass valid paramaters when raising the event i.e.

public delegate void OnSimpleEvent(object sender, EventArgs e);

......

SimpleEvent(this, EventArgs.Empty); // raise the event

 

Custom EventArgs: You can create your own class that inherits from System.EventArgs

public class SimpleEventArgs : EventArgs { }

and pass an instance of this class in your parameters. This class can obviously have its own propeties/methods/constructors etc that can be manipulated and/or made accessible to the receiving method.


Return type: The method that gets called by the event handler (private void SimpleEventOccurred() in our case) can return a value type. To do this you will need to specify the same type in your delegate declaration. The call to SimpleEvent() will then be returned a value of type you specified.

Source

more info

Another simple example

Friday, January 4, 2008

Backup auto complete URLs in Internet Explorer

To get to the auto complete data, you will need to open regedit. Drill down to this key:
HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\TypedURLS

Source

How to run an external program within VB Script

When VB Script executes a command line program it opens a new window. This can be especially inconvenient when you want to incorporate an external application into your script.

Console Windows

There is a way to have your script keep the console application within the same window, and wait for it to complete.
What we will do is launch the process, and then forward all of the console output of the script. When the process has terminated we exit the loop.
Here is a function that you can drop into your script (Click here to download)

Function ExecuteShellProgram(ByVal sFileName)
Dim poShell
Dim poProcess
Dim iStatus
Set poShell = CreateObject("WScript.Shell")
Set poProcess = poShell.Exec(sFileName)
'Check to see if we started the process without error
if ((poProcess.ProcessID=0) and (poProcess.Status=1)) then
Err.Raise vbObjectError,,"Failed executing process"
end if
'Now loop until the process has terminated, and pull out
'any console output
Do
'Get current state of the process
iStatus = poProcess.Status
'Forward console output from launched process
'to ours
WScript.StdOut.Write poProcess.StdOut.ReadAll()
WScript.StdErr.Write poProcess.StdErr.ReadAll()
'Did the process terminate?
if (iStatus <> 0) then
Exit Do
end if
Loop
'Return the exit code
ExecuteShellProgram = poProcess.ExitCode
End Function

Here is a simple example. I am going to call the command interpreter, and do listing of the windows folder:

ExecuteShellProgram "%COMSPEC% /c dir c:\windows"

(%COMSPEC% is a system variable that is set to the full path and file name of the command interpreter cmd.exe)

Now you can execute your console helper applications - and use the output in your script.

Source