Tuesday, July 15, 2014

Creating nth level menu by recursion


Private Sub AppendChildToMenuStrip(ByRef DropDownItems As ToolStripItemCollection, _
                                       ByVal Tag As String, _
                                       ByVal Title As String, _
                                       ByVal ParentIDAs Integer)

            For Each Item As Object In DropDownItems

                Dim SubMenu As ToolStripMenuItem = TryCast(Item, ToolStripMenuItem)

                If SubMenu.Tag = ParentID Then

                    Dim ToolStripItem As New ToolStripMenuItem

                    ToolStripItem.Tag = Tag : ToolStripItem.Text = Title

                    SubMenu.DropDownItems.Add(ToolStripItem)

                ElseIf SubMenu.HasDropDownItems Then

                    AppendChildToMenuStrip(SubMenu.DropDownItems, Tag, Title, ParentID)

                End If

            Next

    End Sub

Using MS Script control to Evaluate Arithmetic Expressions



The MS Script Control COM component provide simple way to evaluate arithmetic expressions like below.

(2*3)/3
2+3-4
(3*4)+(4+6)/4*5

Add reference of MS Script Control COM component in the project



Following is the sample code to evaluate it.

Public Function EvaluateExpression(ByVal expression As String) As Double

        Dim ScriptControl As New MSScriptControl.ScriptControl

        ScriptControl.Language = "VBSCRIPT"
        Return Convert.ToDouble(ScriptControl.Eval(expression))
     
 End Function