用CodeDom、Reflection做一个简易计算器

将代码粘贴到一个 VB 文件即可运行。我这里只是给出一种思路,按这种思路,可以做出一个强劲的计算器,也可以做出一个代码测试器或者是练习器。

Option Strict Off

Imports System.IO

Imports System.CodeDom.Compiler

Imports System.Reflection

Namespace LzmTW.Test

Public Class frmCal

Inherits System.Windows.Forms.Form

Private Code As New CodeBuilder

Private myMode As Mode

Region " Windows 窗体设计器生成的代码 "

Public Sub New ()

MyBase .New()

' 该调用是 Windows 窗体设计器所必需的。

InitializeComponent()

' 在 InitializeComponent() 调用之后添加任何初始化

End Sub

' 窗体重写 dispose 以清理组件列表。

Protected Overloads Overrides Sub Dispose( ByVal disposing As Boolean )

If disposing Then

If Not (components Is Nothing ) Then

components.Dispose()

End If

End If

MyBase .Dispose(disposing)

End Sub

'Windows 窗体设计器所必需的

Private components As System.ComponentModel.IContainer

' 注意 : 以下过程是 Windows 窗体设计器所必需的

' 可以使用 Windows 窗体设计器修改此过程。

' 不要使用代码编辑器修改它。

Friend WithEvents tbCode As System.Windows.Forms.TextBox

Friend WithEvents tbResult As System.Windows.Forms.TextBox

Friend WithEvents btnCal As System.Windows.Forms.Button

Friend WithEvents tbReturnPara As System.Windows.Forms.TextBox

Friend WithEvents Label1 As System.Windows.Forms.Label

Friend WithEvents Label2 As System.Windows.Forms.Label

Friend WithEvents tbSub As System.Windows.Forms.TextBox

Friend WithEvents Label3 As System.Windows.Forms.Label

Friend WithEvents Label4 As System.Windows.Forms.Label

Friend WithEvents Button1 As System.Windows.Forms.Button

Private Sub InitializeComponent()

Me .tbCode = New System.Windows.Forms.TextBox

Me .tbResult = New System.Windows.Forms.TextBox

Me .btnCal = New System.Windows.Forms.Button

Me .tbReturnPara = New System.Windows.Forms.TextBox

Me .Label1 = New System.Windows.Forms.Label

Me .Label2 = New System.Windows.Forms.Label

Me .tbSub = New System.Windows.Forms.TextBox

Me .Label3 = New System.Windows.Forms.Label

Me .Label4 = New System.Windows.Forms.Label

Me .Button1 = New System.Windows.Forms.Button

Me .SuspendLayout()

'

'tbCode

'

Me .tbCode.Anchor = CType ((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _

Or System.Windows.Forms.AnchorStyles.Left) _

Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)

Me .tbCode.Location = New System.Drawing.Point(8, 32)

Me .tbCode.Multiline = True

Me .tbCode.Name = "tbCode"

Me .tbCode.ScrollBars = System.Windows.Forms.ScrollBars.Both

Me .tbCode.Size = New System.Drawing.Size(584, 72)

Me .tbCode.TabIndex = 0

Me .tbCode.Text = "' 给定半径,求周长和面积 " & Microsoft.VisualBasic.ChrW(13) & Microsoft.VisualBasic.ChrW(10) & "Dim M(3)" & Microsoft.VisualBasic.ChrW(13) & Microsoft.VisualBasic.ChrW(10) & "M(0)=20" & Microsoft.VisualBasic.ChrW(13) & Microsoft.VisualBasic.ChrW(10) & "M(1)=M(0)Math.PI2" & Microsoft.VisualBasic.ChrW(13) & Microsoft.VisualBasic.ChrW(10) & "M(2)=Math.PI*Math.Pow(M(0),2)" & Microsoft.VisualBasic.ChrW(13) & Microsoft.VisualBasic.ChrW(10) & "D" & _

"im Result" & Microsoft.VisualBasic.ChrW(13) & Microsoft.VisualBasic.ChrW(10) & "Result="" 半径 :"" + M(0).Tostring +vbcrlf+"" 周长 :"" + M(1).ToString+vbcrlf +"" 面 " & _

" 积 :"" +M(2).Tostring" & Microsoft.VisualBasic.ChrW(13) & Microsoft.VisualBasic.ChrW(10) & " " & Microsoft.VisualBasic.ChrW(13) & Microsoft.VisualBasic.ChrW(10) & " " & Microsoft.VisualBasic.ChrW(13) & Microsoft.VisualBasic.ChrW(10) & "' 打开 Excel" & Microsoft.VisualBasic.ChrW(13) & Microsoft.VisualBasic.ChrW(10) & "Dim oExcel As ExcelObj" & Microsoft.VisualBasic.ChrW(13) & Microsoft.VisualBasic.ChrW(10) & "oExcel" & _

".Open" & Microsoft.VisualBasic.ChrW(13) & Microsoft.VisualBasic.ChrW(10) & "oExcel.GetData(Result)"

'

'tbResult

'

Me .tbResult.Anchor = CType (((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _

Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)

Me .tbResult.Location = New System.Drawing.Point(8, 360)

Me .tbResult.Multiline = True

Me .tbResult.Name = "tbResult"

Me .tbResult.ScrollBars = System.Windows.Forms.ScrollBars.Both

Me .tbResult.Size = New System.Drawing.Size(584, 64)

Me .tbResult.TabIndex = 1

Me .tbResult.Text = ""

'

'btnCal

'

Me .btnCal.Anchor = CType ((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)

Me .btnCal.Location = New System.Drawing.Point(448, 320)

Me .btnCal.Name = "btnCal"

Me .btnCal.Size = New System.Drawing.Size(120, 32)

Me .btnCal.TabIndex = 2

Me .btnCal.Text = " 计算 (&C)"

'

'tbReturnPara

'

Me .tbReturnPara.Anchor = CType ((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)

Me .tbReturnPara.Location = New System.Drawing.Point(104, 112)

Me .tbReturnPara.Name = "tbReturnPara"

Me .tbReturnPara.Size = New System.Drawing.Size(152, 21)

Me .tbReturnPara.TabIndex = 3

Me .tbReturnPara.Text = "Result"

'

'Label1

'

Me .Label1.Anchor = CType ((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)

Me .Label1.Location = New System.Drawing.Point(8, 112)

Me .Label1.Name = "Label1"

Me .Label1.Size = New System.Drawing.Size(72, 16)

Me .Label1.TabIndex = 4

Me .Label1.Text = " 返回值参数 "

'

'Label2

'

Me .Label2.Location = New System.Drawing.Point(8, 8)

Me .Label2.Name = "Label2"

Me .Label2.Size = New System.Drawing.Size(128, 16)

Me .Label2.TabIndex = 5

Me .Label2.Text = " 主程序: "

'

'tbSub

'

Me .tbSub.Anchor = CType (((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _

Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)

Me .tbSub.Location = New System.Drawing.Point(8, 176)

Me .tbSub.Multiline = True

Me .tbSub.Name = "tbSub"

Me .tbSub.ScrollBars = System.Windows.Forms.ScrollBars.Both

Me .tbSub.Size = New System.Drawing.Size(584, 136)

Me .tbSub.TabIndex = 6

Me .tbSub.Text = "Class ExcelObj" & Microsoft.VisualBasic.ChrW(13) & Microsoft.VisualBasic.ChrW(10) & " Private shared obj,Wb,Ws" & Microsoft.VisualBasic.ChrW(13) & Microsoft.VisualBasic.ChrW(10) & "<SPAN style="mso-sp

Published At
Categories with Web编程
Tagged with
comments powered by Disqus