Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

November 19, 2010

Associating Code with a Tag using Delegates II

Filed under: C#,Lambda Expressions,VB.NET,WinForms @ 11:32 am

This prior post demonstrated how to use delegates to associated code with a Tag in a WinForms control. This post shows how to accomplish the same thing using Lambda expressions to define the delegate.

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(
                          x => MessageBox.Show("Apple: " + x));

    var microsoftTreeNode = rootTreeNode.Nodes.Add("Win 7 Phone is the best");
    microsoftTreeNode.Tag = new DisplayMessage(
                          x => MessageBox.Show("Microsoft: " + x));

    treeView1.ExpandAll();
}

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(
                         Sub(x) MessageBox.Show("Apple: " & x))

    Dim microsoftTreeNode = rootTreeNode.Nodes.Add("Win 7 Phone is the best")
    microsoftTreeNode.Tag = New DisplayMessage(
                        Sub(x) MessageBox.Show("Microsoft: " & x))

    TreeView1.ExpandAll()
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 prior 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 constructor defines the code to call when the user double-clicks on the node. This code is defined using a Lambda expression. If you have not worked with a Lambda expression before, see this prior blog post for more information.

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!

1 Comment

  1.   billschen — April 7, 2011 @ 2:48 am    Reply

    在做窗体开发,正好需要根据用户点击不同的TreeView执行不同的动作,看到了这个,对我的帮助实在太大了!

RSS feed for comments on this post. TrackBack URI

Leave a comment

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