我想编辑一个小的程序,该程序的执行要手工键入命令,以回车为执行此命令,但如何接收来自于你输入的指令呢?再有,输入命令的地方是在FORM上还是在TEXT里或者其他的地方,还是都可以。请各位帮助!

解决方案 »

  1.   

    输入命令放在Text中,在Text_KeyPress事件中判断是否按了回车键回车键的AscII值是13
      

  2.   

    把命令放在Text中,在Text_KeyPress事件中回车键执行
    sub op(aa as string)
    msgbox aa,64,"提示"
    endif
    Text_KeyPress事件
    if keyascii=13 then
    op text1.text
    endif
      

  3.   

    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)End Sub
    要设置form的keypreview=true
      

  4.   

    K创建简单的脚本工程
    为了说明在 Script 控件中添加部件对象模型,可以使用下列步骤创建一个简单的名为 SimpleScript 的 Visual Basic 工程。
    创建 SimpleScript 工程 
    在"File"菜单上,单击"New Project"。
    在"New Project"对话框中,双击"Standard Exe"图标。
    按下 F4 键打开"Properties"窗口。双击 Name 属性,将窗体名改为 frmSimpleScript。
    双击 Caption 属性,将其设为 Simple Script Demo。
    在"Project"菜单上,单击"Add Class Module"。
    在"Add Class Module"对话框中,双击"Class Module"图标。 
    这时会将对象模型加入到 Script 控件中。
    在"Properties"窗口中,双击"Name",将名称改为 MyClass。
    将下列代码添加到类模块中:
    Option Explicit
    Private ObjRate As DoublePublic Property Get Rate() As Double
        Rate = ObjRate
    End PropertyPublic Property Let Rate(newRate As Double)
        ObjRate = newRate
    End PropertyPublic Function TaxIt(price) As Double
        TaxIt = price + (price * ObjRate)
    End Function
    将 CommandButton1、TextBox1 和 ListBox1 控件添加到窗体中,并将它们的属性设为下表所示的值。 
    控件属性值
    CommandButton1 Name cmdAddCode
    Caption AddCode
    Height 255
    Left 0
    Top 240
    Width 1215
    TextBox1 Name txtScript
    MultiLine True
    ScrollBars 2 - Vertical
    Height 1575
    Left 1320
    Top 240
    Width 4335
    ListBox1 Name lstProcedures
    Height 645
    Left 1320
    Top 2150
    Width 4335
    在工具箱上单击右键并单击"Components",显示"Components"对话框。双击"Microsoft Script Control 5.0",然后单击"OK"。
    在工具箱上双击 ScriptControl 图标将其添加到窗体中。
    将Script 控件重新命名为 scDemo。
    将下列代码粘贴到窗体的Declaration部分。
    Option Explicit
    Private MyObject As New MyClassPrivate Sub Form_Load()
    ' 将 MyObject 添加到 Script 控件中。
    ' 这会将对象模型添加到
    ' 控件中。
    scDemo.AddObject "objScript", MyObject
    ' 向 TextBox 控件中添加脚本。
    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 SubPrivate Sub cmdAddCode_Click()
    ' 将 TextBox 中的代码添加到控件中。
    scDemo.AddCode txtScript.Text' 清除 ListBox,将Procedures集合中各个过程的
    ' 名称加入其中。
    lstProcedures.Clear
    Dim p As Procedure
    For Each p In scDemo.Procedures
    lstProcedures.AddItem p.Name
    NextEnd SubPrivate Sub lstProcedures_Click()
    ' 运行列表框中的过程。
    scDemo.Run lstProcedures.Text
    End Sub
    运行 SimpleScript 工程 
    按下 F5 键运行工程。
    单击"AddCode"向控件中添加代码。ListBox 控件会显示刚刚加入的过程名称。
    在 ListBox 控件中单击"SetRate"。对话框显示缺省值 8.6,改变比率或者只单击"OK"。
    在 ListBox 控件中,单击"TotalPrice"。对话框显示缺省值 100。改变值并单击"OK"。将会显示总值加上税率。 
    在 TextBox 控件中编辑代码
    在将代码加入控件之前也可以对其进行修改。 
    在 TextBox 控件中,将 MsgBox 的 SetRate 缺省值改为 4.2。
    单击"AddCode"。
    在 ListBox 控件中,单击"SetRate"。缺省值已经改变。 
    在 TextBox 控件中添加过程
    试着在 Textbox 控件中插入自己的代码。 
    在 Textbox 控件中,粘贴如下代码:
    Sub VariableTax()
       Dim Years, results, ttlInterest
       Dim i, principal, payment, interest, mortgage
       principal = InputBox("Borrow:",,100000)
       Years = InputBox("How long:",,15)
       results = "Payment" & vbtab & "Interest" & vbcrlf
       For i = 1 to Years * 12
        interest =  (principal * objScript.Rate) / 12
        principal = principal - interest
        ttlInterest = ttlInterest + interest
        results = results & i & vbtab & formatNumber(interest,2) & vbcrlf
        Next 
        MsgBox results
        MsgBox "Total Interest Paid:" & "$" & formatNumber(ttlInterest,2)
      End Sub
    单击"AddCode"。
    单击"SetRate",改变比率,然后单击"OK"。
    单击"VariableTax"运行新过程。