VBScript application (interaction between the script program and the Host Program)

Source: Internet
Author: User

Anyone who has done industrial control knows that the support for scripting language is a basic function (such as iFIX and Kingview) that standard configuration software should possess ). Can we implement similar functions using VB?

We all know that Microsoft provides the Micrsoft script Control 1.0 control, we also saw the use of this control in VB (for example, using the executestatement method to execute the script command executestatement ("msgbox" "")??? ). But most importantly, there is no complete solution for data interaction and transmission between ActiveX scripts and host applications.

I have also explored a little bit of data transmission and interaction skills for a long time. Now I share them for discussion and research.

Example:

1. First load the Micrsoft script Control 1.0 control.

It basically does not need to be configured, the default language VBScript, the introduction of this part, please download VBScript command set (http://blog.csdn.net/yefanqiu [ye fan resources] [03] VBScript Instruction Set)

2. Add a form (frmtest) with the following code:

'*************************************** * ******************************** <Br/> '* * Module name: frmtest <br/> '** Note: yfsoft copyright: 2004-2005 (c) <br/>' ** Creator: ye fan <br/> '** day: 2004-10-15 11:00:32 <br/>' ** revised by: <br/> '** day: <br/> '** Description: exploration of script language usage <br/>' ** version: v1.0.0 <br/> '********************************** *********************************** <br/> Option explicit <br/> '****************************** **************************************** * ** <br/>: chkrun_click <br/> '** input: None <br/>' ** output: None <br/> '** function description: script Running Control <br/> '** global variable: <br/>' ** call module: <br/> '** OPERATOR: ye fan <br/> '** day: 2004-10-15 11:19:31 <br/>' ** revised by: <br/> '** day: <br/> '** version: v1.0.0 <br/> '********************************** *********************************** <br/> private sub chkrun_click () <br/> If chkrun. value = 1 then <br/> tmrrun. interval = cmbruntime. text 'runtime interval <br/> tmrrun. enabled = true' run cyclically at a fixed interval <br/> txtedit. enabled = false <br/> else <br/> tmrrun. enabled = false' stop running <br/> txtedit. enabled = true <br/> end if <br/> end sub <br/> '******************** **************************************** * ************ <br/> '** number of letters: cmbtype_click <br/> '** input: None <br/>' ** output: None <br/> '** function description: operation Type switching <br/> '** global variable: <br/>' ** call module: <br/> '** OPERATOR: ye fan <br/> '** day: 2004-10-15 12:56:19 <br/>' ** revised by: <br/> '** day: <br/> '** version: v1.0.0 <br/> '********************************** *********************************** <br/> private sub cmbtype_click () <br/> export scdemo1_click <br/> end sub <br/> '************************ **************************************** * ******** <br/> '** letter count: cmdrun_click <br/> '** input: None <br/>' ** output: None <br/> '** function description: single run script <br/> '** global variable: <br/>' ** call module: <br/> '** OPERATOR: ye fan <br/> '** day: 2004-10-15 11:51:29 <br/>' ** revised by: <br/> '** day: <br/> '** version: v1.0.0 <br/> '********************************** *********************************** <br/> private sub cmdrun_click () <br/> call tmrrun_timer <br/> end sub <br/> '*********************** **************************************** * ********* <br/> '** letter count: export scdemo1_click <br/> '** input: None <br/>' ** output: None <br/> '** function description: parameter transfer Demonstration <br/> '** global variable: <br/>' ** call module: <br/> '** OPERATOR: ye fan <br/> '** day: 2004-10-15 11:03:29 <br/>' ** revised by: <br/> '** day: <br/> '** version: v1.0.0 <br/> '********************************** *********************************** <br/> private sub register scdemow.click () <br/> dim strtype as string <br/> 'internal variable definition is similar to the data dictionary in Kingview. <br/> valmem. clear <br/> valmem. setvalue "value1", txtvalue (0) <br/> valmem. setvalue "value2", txtvalue (1) <br/> valmem. setvalue "value3", 0 <br/> select case cmbtype. listindex <br/> case 0: <br/> strtype = "+" <br/> case 1: <br/> strtype = "-" <br/> case 2: <br/> strtype = "*" <br/> case 3: <br/> strtype = "/" <br/> end select <br/> txtedit = "dim lngvalue1" & vbcrlf <br/> txtedit = txtedit & "dim lngvalue2 "& vbcrlf <br/> txtedit = txtedit & "dim lngvalue3" & vbcrlf <br/> txtedit = txtedit & "lngvalue1 = VM. getvalue ("& CHR (34) &" value1 "& CHR (34) &") "& vbcrlf <br/> txtedit = txtedit &" lngvalue2 = VM. getvalue ("& CHR (34) &" value2 "& CHR (34) &") "& vbcrlf <br/> txtedit = txtedit &" lngvalue3 = CINT (lngvalue1) "& strtype &" CINT (lngvalue2) "& vbcrlf <br/> txtedit = txtedit &" VM. setvalue "& CHR (34) &" value3 "& CHR (34 )&", lngvalue3 "& vbcrlf <br/> end sub <br/> '************************* **************************************** * ******* <br/> '** letter count: export scdemo2_click <br/> '** input: None <br/>' ** output: None <br/> '** function description: object operation demonstration <br/> '** global variable: <br/>' ** call module: <br/> '** OPERATOR: ye fan <br/> '** day: 2004-10-15 13:41:23 <br/>' ** revised by: <br/> '** day: <br/> '** version: v1.0.0 <br/> '********************************** *********************************** <br/> private sub extends scdemo2_click () <br/> txtedit = "dim lngc" & vbcrlf <br/> txtedit = txtedit & "lngc1_clng(fm.txt demo. text) "& vbcrlf <br/> txtedit = txtedit &" lngc = lngc + 10 "& vbcrlf <br/> txtedit = txtedit &" If lngc> 255 then lngc = 0 "& vbcrlf <br/> txtedit = txtedit & "FM. picdemo. backcolor = RGB (lngc, 0, 0) "& vbcrlf <br/> txtedit = txtedit &" fm.txt demo. TEXT = CSTR (lngc) "& vbcrlf <br/> end sub <br/> '************************** **************************************** * ****** <br/> '** letter count: form_load <br/> '** input: None <br/>' ** output: None <br/> '** function description: initialization <br/> '** global variable: <br/>' ** call module: <br/> '** OPERATOR: ye fan <br/> '** day: 2004-10-15 11:13:03 <br/>' ** revised by: <br/> '** day: <br/> '** version: v1.0.0 <br/> '********************************** *********************************** <br/> private sub form_load () <br/> 'variable name that can be used internally to add a parameter <br/> sccommand. addobject "VM", valmem, true <br/> 'add an externally available object to the script <br/>' the last parameter: "True" indicates that its subclass property method can be used in the script. "false" indicates that the method property of the object is available. <br/> sccommand. addobject "FM", me, true <br/> 'script runtime Interval Settings <br/> cmbruntime. listindex = 0 <br/> 'parameter operation type <br/> cmbtype. listindex = 0 <br/> end sub <br/> '************************** **************************************** * ****** <br/> '** letter count: tmrrun_timer <br/> '** input: None <br/>' ** output: None <br/> '** function description: script run <br/> '** global variable: <br/>' ** call module: <br/> '** OPERATOR: ye fan <br/> '** day: 2004-10-15 11:29:46 <br/>' ** revised by: <br/> '** day: <br/> '** version: v1.0.0 <br/> '********************************** *********************************** <br/> private sub tmrrun_timer () <br/> on error goto toexit 'Open error trap <br/>' ---------------------------------------------- <br/> dim I as long <br/> 'parameter input <br/> valmem. setvalue "value1", txtvalue (0 ). text <br/> valmem. setvalue "value2", txtvalue (1 ). text <br/> valmem. setvalue "value3", 0 <br/> 'Run the script <br/> sccommand. executestatement txtedit. text <br/> 'parameter output <br/> lstvalueout. clear <br/> for I = 1 to valmem. count <br/> lstvalueout. additem valmem. getvalue ("", I) <br/> next <br/> '---------------------------------------------- <br/> exit sub <br/>' ---------------- <br/> toexit: <br/> txterror = "error code:" & err. number & "time:" & format (now, "YYYY-MM-DD hh: mm: SS") & vbcrlf <br/> txterror = txterror & "error message:" & err. description & vbcrlf <br/> txterror = txterror & "Error Source:" & err. source <br/> end sub <br/>

3. Add a module (mdlbase) with the following code:

'*************************************** * ******************************** <Br/> '* * Module name: mdlbase <br/> '** Note: yfsoft copyright: 2004-2005 (c) <br/>' ** Creator: ye fan <br/> '** day: 2004-10-15 11:10:58 <br/>' ** revised by: <br/> '** day: <br/> '** Description: Public Variable Area <br/>' ** version: v1.0.0 <br/> '********************************** *********************************** <br/> Option explicit <br/> Public valmem as new clsscriptio variable setting area for parameter exchange in scripting

4. Add a class (clsscriptio) named clsscriptio with the following code added:

'*************************************** * ******************************** <Br/> '* * Module name: clsscriptio <br/> '** Note: yfsoft copyright: 2004-2005 (c) <br/>' ** Creator: ye fan <br/> '** day: 2004-10-15 11:56:32 <br/>' ** revised by: <br/> '** day: <br/> '** Description: class module that interacts with script language parameters <br/>' ** version: v1.0.0 <br/> '********************************** *********************************** <br/> Option explicit <br/> private cltscriptmem as new collection 'variable setting area <br/> '***************** **************************************** * *************** <br/> '** number of letters: count <br/> '** input: None <br/>' ** output: (variant)-<br/> '** function description: memory variable count <br/> '** global variable: <br/>' ** call module: <br/> '** OPERATOR: ye fan <br/> '** day: 2004-10-15 12:00:57 <br/>' ** revised by: <br/> '** day: <br/> '** version: v1.0.0 <br/> '********************************** *********************************** <br/> Public Property get count () as variant <br/> COUNT = cltscriptmem. count <br/> end property <br/> '**************************** **************************************** * ***** <br/> '** number of letters: getvalue <br/> '** input: strkey (string)-variable name <br/>' **: Optional lngno (long = 0) -variable index <br/> '** output: (variant)-return value <br/>' ** function description: get the variable value with the specified name <br/> '** global variable: <br/>' ** call module: <br/>: ye fan <br/> '** day: 2004-10-15 12:01:59 <br/>' ** revised by: <br/> '** day: <br/> '** version: v1.0.0 <br/> '********************************** *********************************** <br/> Public Function getvalue (strkey as string, optional lngno as long = 0) as variant <br/> on error goto toexit 'opens error traps <br/>' ---------------------------------------------- <br/> If lngno> 0 then, returns the variable specified by the index number <br/> getvalue = cltscriptmem. item (lngno) <br/> else <br/> getvalue = cltscriptmem. item (strkey) <br/> end if <br/> '---------------------------------------------- <br/> exit function <br/>' ---------------- <br/> toexit: <br/> end function <br/> '***************************** **************************************** * *** <br/>: setvalue <br/> '** input: strkey (string)-variable name <br/>' **: Value (variant) -Set variable <br/> '** output: None <br/>' ** Function Description: assign values to the specified variable <br/> '** global variable: <br/> '** call module: <br/>' ** OPERATOR: ye fan <br/> '** Date: 12:07:05 <br/> '** revised by: <br/>' ** Date: <br/> '** version: v1.0.0 <br/> '********************************** *********************************** <br/> Public sub setvalue (strkey as string, value as variant) <br/> on error goto toexit 'Open the error trap <br/>' ---------------------------------------------- <br/> cltscriptmem. remove (strkey) <br/> cltscriptmem. add value, strkey <br/> '---------------------------------------------- <br/> exit sub <br/>' ---------------- <br/> toexit: <br/> resume next <br/> end sub <br/> '*********************** **************************************** * ********* <br/> '** letter count: delvalue <br/> '** input: strkey (string)-variable name <br/>' **: Optional lngno (long = 0) -index number <br/> '** output: None <br/>' ** Function Description: Delete the specified variable <br/> '** global variable: <br/> '** call module: <br/>' ** OPERATOR: ye fan <br/> '** Date: 12:08:55 <br/> '** revised by: <br/>' ** Date: <br/> '** version: v1.0.0 <br/> '********************************** *********************************** <br/> Public sub delvalue (strkey as string, optional lngno as long = 0) <br/> on error goto toexit 'open error traps <br/> '------------------------------------------ <br/> If lngno> 0 then, delete the variable specified by the index number <br/> cltscriptmem. remove (lngno) <br/> else <br/> cltscriptmem. remove (strkey) <br/> end if <br/> '---------------------------------------------- <br/> exit sub <br/>' ---------------- <br/> toexit: <br/> end sub <br/> '***************************** **************************************** * *** <br/>: clear <br/> '** input: None <br/>' ** output: None <br/> '** function description: delete variable <br/> '** global variable: <br/>' ** call module: <br/> '** OPERATOR: ye fan <br/> '** day: 2004-10-15 13:15:15 <br/>' ** revised by: <br/> '** day: <br/> '** version: v1.0.0 <br/> '********************************** *********************************** <br/> Public sub clear () <br/> on error goto toexit 'Open the error trap <br/>' ---------------------------------------------- <br/> while cltscriptmem. count> 0 <br/> cltscriptmem. remove (1) <br/> Wend <br/> '---------------------------------------------- <br/> exit sub <br/>' ---------------- <br/> toexit: <br/> resume next <br/> end sub <br/>

Function Demonstration:

 

1. PASS Parameters (click the "PASS Parameters" button to automatically add the corresponding script code. You can perform addition, subtraction, multiplication, division, and other operations.

 

Demonstrate error capture (Division by 0 error)

 

2. Object operations (click the object operation demo button to automatically add the corresponding script code. You can assign values and change the background color of a widget.

 

 

 

VBScript uses the ActiveX (r) script to talk to the Host application. With ActiveX script, browsers and other host applications no longer need special integration code for each script part. ActiveX scripts allow the host to compile scripts, obtain and call endpoints, and manage namespaces available to developers. With ActiveX script, language vendors can establish standard script runtime languages. Microsoft will provide support for running VBScript.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.