⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 56.txt

📁 介绍VB里的各种控件的使用方法,窗口控制,图像编程以及OCX等内容,还提供了一个API集供参考.
💻 TXT
字号:
如何让用户自行输入方程式,并计算其结果?

假设我们要让使用者在"方程式"栏位中自由输入方程式,然后利用方程式进行计算,则引用ScriptControl控件可以很方便地做到。

ScriptControl 控件附属于VB 6.0,如果安装后没有看到此一控件,可在光盘的 \Common\Tools\VB\Script 目录底下找此一控件, 其.文件名为Msscript.ocx。将该目录下的文件拷贝到C:\windows\system目录下,再用regsvr32.exe注册后即可使用。

假设放在窗体上的ScriptControl控件名称为ScriptControl1,则在"计算"按钮的Click事件中编写如下代码: 

Dim Statement As String 
Statement = "X=" + Text1.Text + vbCrLf + _ 
                       "Y=" + Text2.Text + vbCrLf + _ 
                       "MsgBox ""计算结果="" & Y " 
ScriptControl1.ExecuteStatement(Statement)
y=0

按F5执行后,在Text1中输入数值,在Text2中输入公式,例如x*x,或x*2+1,然后按Comman1执行,你将会看到一个消息框显示计算结果

下面这个例子,是从Script控件的帮助文件中提取出来的,虽然比较复杂,但更全面地介绍了该控件的功能和用法。

首先在新的窗体中设置下列控件及属性:

控件 属性 值 
TextBox Name txtScript 
Multiline true 
ScrollBars 2-Vertical 
Height 1575 
Left 1320 
Top 240 
Width 4335 
CommandButton Name cmdAddCode 
Caption Add Code 
Height 255 
Left 0 
Top 240 
Width 1215 
ListBox Name lstProcedures 
Height 645 
Left 1320 
Top 2160 
Width 4335 
ScriptControl Name scDemo 

在工程中添加一个类模块,在其属性窗口中将名称改为MyClass,并将下述代码添加到类模块中:

Option Explicit 

Private ObjRate As Double 

Public Property Get Rate() As Double 
    Rate = ObjRate 
End Property 

Public Property Let Rate(newRate As Double) 
    ObjRate = newRate 
End Property 

Public Function TaxIt(price) As Double 
    TaxIt = price + (price * ObjRate) 
End Function

将下列代码添加到窗体的声明段中:

Private Sub Form_Load() 

' Add MyObject to the Script Control. This adds 
' the run-time functionality of the object to the 
' control. 

scDemo.AddObject "objScript", MyObject 

' Add script to Textbox control. 

txtScript.Text = "Sub SetRate()" & vbCrLf & _
    " Dim Rate" & vbCrLf & _ 
    " Rate = InputBox(""TaxRate:"",,.086)" & _ 
    vbCrLf & _ " objScript.Rate = Rate" & vbCrLf & _ 
    "End Sub" & vbCrLf & vbCrLf & _ 
    "Sub TotalPrice()" & vbCrLf & _ 
    " Dim price, ttl" & vbCrLf & _ 
    " price = InputBox(""Price:"",,100)" & _ vbCrLf & _ 
    " ttl =objScript.TaxIt(price)" & vbCrLf & _ 
    " MsgBox ttl" & vbCrLf & _ 
    "End Sub"

End Sub

把下面的代码放在命令按钮的Click事件中:

Private Sub cmdAddCode_Click() 

' Add the code in the TextBox to the control. 

scDemo.AddCode txtScript.Text 

' Clear the ListBox, and add the name of each 
' procedure in the Procedures collection. 

lstProcedures.Clear

Dim p As Procedure 

For Each p In scDemo.Procedures 
    lstProcedures.AddItem p.Name 
Next 

End Sub 

把下面的代码放在ListBox的Click事件中:

Private Sub lstProcedures_Click() 

' Run the procedure in the ListBox. 

scDemo.Run lstProcedures.Text 

End Sub 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -