Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

November 19, 2010

Associating Code with a Tag using Delegates

Filed under: C#,VB.NET,WinForms @ 11:17 am

Many of the Windows Forms controls have a Tag property that you can use to store data associated with the control. For example, TextBoxes have a Tag property so you could store additional information about each TextBox in the Tag. For TreeView controls, each node has its own Tag property, so you can associate data with each node. But what if you wanted to associated code with each Tag?

This post details how to use delegates to associated code with a Tag. It uses a TreeView as an example, but any control with a Tag property could use this technique.

If you want to use a Lambda expression instead of a method to define the delegate, see this later post.

To try out this example, create a WinForms application and add a TreeView control to the form. Then add the code below.

In C#:

public delegate void DisplayMessage(string value);

private void Form1_Load(object sender, EventArgs e)
{
    var rootTreeNode = treeView1.Nodes.Add("Phones");

    var appleTreeNode = rootTreeNode.Nodes.Add("IPhone is the best");
    appleTreeNode.Tag = new DisplayMessage(AppleMessage);

    var microsoftTreeNode = rootTreeNode.Nodes.Add("Win 7 Phone is the best");
    microsoftTreeNode.Tag = new DisplayMessage(MicrosoftMessage);

    treeView1.ExpandAll();
}

public void  AppleMessage(string value)
{
    MessageBox.Show("Apple: " + value);
}
public void MicrosoftMessage(string value)
{
    MessageBox.Show("Microsoft: " + value);
}

private void treeView1_DoubleClick(object sender, EventArgs e)
{
    var messageDelegate = (DisplayMessage)treeView1.SelectedNode.Tag;
    messageDelegate.Invoke(treeView1.SelectedNode.Text);
}

In VB:

Public Delegate Sub DisplayMessage(ByVal value As String)

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim rootTreeNode = TreeView1.Nodes.Add("Phones")

    Dim appleTreeNode = rootTreeNode.Nodes.Add("IPhone is the best")
    appleTreeNode.Tag = New DisplayMessage(AddressOf AppleMessage)

    Dim microsoftTreeNode = rootTreeNode.Nodes.Add("Win 7 Phone is the best")
    microsoftTreeNode.Tag =
                     New DisplayMessage(AddressOf MicrosoftMessage)

    TreeView1.ExpandAll()
End Sub

Public Sub AppleMessage(ByVal value As String)
    MessageBox.Show("Apple: " & value)
End Sub

Public Sub MicrosoftMessage(ByVal value As String)
    MessageBox.Show("Microsoft: " & value)
End Sub

Private Sub TreeView1_DoubleClick(ByVal sender As Object,
                ByVal e As EventArgs) Handles TreeView1.DoubleClick
    Dim messageDelegate = TryCast(TreeView1.SelectedNode.Tag,
                                               DisplayMessage)
    If messageDelegate IsNot Nothing Then
        messageDelegate.Invoke(TreeView1.SelectedNode.Text)
    End If
End Sub

The first line of code declares a delegate. If you have not worked with delegates before, see this blog post for more information.

The Load event populates the Treeview with two child nodes. A new instance of the delegate is stored in the Tag property of each node. The delegate instance defines the method to call when the user double-clicks on the node.

The AppleMessage method and the MicrosoftMessage method define the two methods to execute. These methods must have a signature that matches the signature of the delegate. In this case, it is a routine that takes one string parameter and does not return a value. But the delegate could define any signature and the methods must then match the delegate’s signature.

In this example, the methods displayed a messageBox, but the method could do anything. It could perform any type of calculation or perform any processing.

The DoubleClick event then pulls the delegate from the Tag property and calls it using the Invoke method, passing the appropriate parameters.

Use this technique any time you want to define code to execute in the Tag property of a control.

Enjoy!

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2019 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs