Monday, April 30, 2007

How to make a math expression evaluator for VB.NET

Just found a C# one, and converted it for VB.NET

Private Function CalculateEval(ByVal command As String) As String
Dim myCodeProvider As New CSharpCodeProvider
Dim cp As CompilerParameters = New CompilerParameters()
Dim TempModuleSource As String = "namespace ns{" + "using System;" + "class class1{" + "public static double Evaluate(){return " + command.ToString + ";}}} "
Dim cr As CompilerResults = myCodeProvider.CompileAssemblyFromSource(cp, TempModuleSource)
If (cr.Errors.Count > 0) Then
Throw New ArgumentException("Expression cannot be evaluated, please use a valid C# expression")
Dim Methinfo As MethodInfo = cr.CompiledAssembly.GetType("ns.class1").GetMethod("Evaluate")
Return Methinfo.Invoke(Nothing, Nothing)
End If
Catch ex As Exception
End Try
Return ""
End Function

Enjoy IT!

Thursday, April 05, 2007

VB Helper: HowTo: Change the color of an MDI parent form's background in VB .NET

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

' Set the color in the MDI client.
For Each ctl As Control In Me.Controls
If TypeOf ctl Is MdiClient Then
ctl.BackColor = Me.BackColor
End If
Next ctl
' Display a child form.
Dim frm As New Form2
frm.MdiParent = Me
frm.Width = Me.Width \ 2
frm.Height = Me.Height \ 2
End Sub

Enjoy IT!