Using ContextMenuStrip and NotifyIcon classes with WPF

There are many ways to get a notification icon displaying in the system tray, this is how I managed to get it working using Windows Presentation Framework:

public partial class ClassName : Window

{

private System.Windows.Forms.NotifyIcon m_notifyIcon;

private System.Windows.Forms.ContextMenuStrip m_contextMenu;

public ClassName()

{

InitializeComponent();

//Initalize the context menu strip

m_contextMenu = new System.Windows.Forms.ContextMenuStrip();

System.Windows.Forms.ToolStripMenuItem mI1 = new System.Windows.Forms.ToolStripMenuItem();

mI1.Text = “Menu One”;

mI1.Click += new EventHandler(Click_Handler); //Add Click Handler

m_contextMenu.Items.Add(mI1);

System.Windows.Forms.ToolStripMenuItem mI2 = new System.Windows.Forms.ToolStripMenuItem();

mI2.Text = “Menu Two”;

mI2.Click += new EventHandler(Click_Handler); //Add Click Handler

m_contextMenu.Items.Add(mI2);

//Initalize Notify Icon

m_notifyIcon = new System.Windows.Forms.NotifyIcon();

m_notifyIcon.Text = “Application Title”;

m_notifyIcon.Icon = new System.Drawing.Icon(“Icon.ico”);

m_notifyIcon.ContextMenuStrip = m_contextMenu; //Associate the contextmenustrip with notify icon

m_notifyIcon.Visible = true;

}

}

Creating a generic list of anonymous types

Scenerio

I have a simple HTML form on a ASP.NET Web page. This form is to post a number of values including several elements that are compulsory to perform several actions on the server. As we know, when we post values to ASP.NET these are available for Request.Form. All we know is the key and value in the NameValueCollection object. I want to be able to check if several values are present and if they are not, return the respective element meta-description rather than the original name.

For Example If we post a name field called formName, we want to return “You must enter data into the fields: Form Name”.

Solution

I used a generic list of a anonymous type. My anonymous type has a Name and a Value property.

var nameValueInstance = new { Name=”formName”,Value=”Form Name”}

I created a list factory that pases in a array of generic type interfaces and returns a generic list of the generic type interface.

private static List<T> MakeList<T>(params T[] items)

{

List<T> newList = new List<T>(items);

return newList;

}

I was then able to do the following to create a generic list of anonymous types:

var fields = MakeList(new { Name = “price”, Value = “Price” }, new { Name = “productname”, Value = “Product Name” });

From here I was able to interate through the list and validate the Form elements and return the nessessary output.

References:

http://kirillosenkov.blogspot.com/2008/01/how-to-create-generic-list-of-anonymous.html

Using Nested Functions in C#

Scenario

We currently have a templating API system where this allows developers to use a combination of C# and HTML markup to customized the display of the client’s websites. In this case, I needed a way to quickly perform some calculations and formatting without having to declare the function at design-time. My problem was I was to pass in a Boolean, a integer and a string that performed specific logic to produce a outcome (a string).

Solution

I was able to use a Func delegate specifying the parameter types and the return type. With a help of Lambda expressions I was able to capture the input of the three variables and return the result.

Func<bool, int, String, String> nestedFunction = (b, i, s) => { //Do something return s; };
String foo = nestedFunction(true,1,”format string”);

References

http://www.codeguru.com/csharp/csharp/cs_misc/designtechniques/article.php/c12749/