Hi there,
I wonder, is there any chance to interact with MSScriptControl to read value from TextBox on the existing Form and then send Click to button on the same Form at run-time?
Thank you! ;)
Printable View
Hi there,
I wonder, is there any chance to interact with MSScriptControl to read value from TextBox on the existing Form and then send Click to button on the same Form at run-time?
Thank you! ;)
What would be the purpose of the msscriptcontrol?
You can always add an external COM-instance (like e.g. your VB6-Form-Object)
into the Scripting-Context - e,g. by using (from inside your Form):
After that, you should be able to address your TextBoxes in ScriptCode over e.g.:Code:ScriptControl.AddObject "frmMyForm", Me
In case you want to export the Public Members (Functions and Properties) of your FormCode:MsgBox frmMyForm.Text1.Text
as an extension to your Script-Context, you would specify an additional True-Param in the AddObject-call:
After that, you should be able to address your TextBoxes in ScriptCode, leaving out the leading frmMyForm-prefix:Code:ScriptControl.AddObject "frmMyForm", Me, True
OlafCode:MsgBox Text1.Text
This sort of thing is covered in your MSDN Library documentation. Use the Search tab and search on "script control" (with the quotes). The first few hits will be the most relevant.
I did not get any relevant search hits unfortunately.
Attachment 161841
I would like to use something like this:
But maybe I asked or explained in a wrong way what I wish to achieve.Code:ScriptControl.AddCode txtScript.Text
ScriptControl.Run "Default"
1. I have a standard VB6 project with Form named frmMAIN
2. On that Form I have a textbox named txtValue.txt
3. And I have another textbox named txtScript.Text and a commandbutton named cmdRun
Now, when I put some basic function into the txtScript.Text and Run it, my Script Function named "Default" need to read the value from the txtValue.Text from the frmMAIN real form.
Is that possible to make it work?
So, basically I want to run script from the txtScript.Text and the script need to read value from the textbox from real Form.
EDIT: Made a quick example project what I meant to do.
Attachment 161843
Since you quoted, what I wrote in my first post about ScriptControl.AddObject,
why not just use this call with your Form-Instance (represented by the 'Me' Keyword)?
Here is a working example, which only needs two things on the Form:
- a ScriptControl-instance, named ScriptControl
- a single TextBox on that Form, named Text1
Now click the Form, to run the Code (with the little routine, which I've named "Sub Test()".Code:Option Explicit
Private Sub Form_Load()
Text1.Text = "12.34"
ScriptControl.AddObject "Me", Me, True
End Sub
Private Sub Form_Click()
ScriptControl.AddCode "Sub Test(): x = 12: MsgBox Val(Text1)-x: End Sub"
ScriptControl.Run "Test"
End Sub
'Public Functions which are defined in an added Object (like this Form-Instance)
'will be available as well - so one can use them, to expand the VBScript-language
Public Function Val(ByVal S As String)
Val = VBA.Val(S)
End Function
HTH
Olaf
Do you have the really ancient 1998 edition installed? That is missing tons of stuff. Ideally you should have the October 2001 edition instead, the final update that included VB6 integration. Without that you are sort of stuck since the online versions of many articles have been expunged over time.
Thank you Olaf, after a few modification to fit to my need, it's working as expected! ;)
Another thing:
If I make my script like this, how can I stop the continuous Loop?
Is there any stop script execution command in MSScriptControl?
or just I make it like?Code:dim ret
do
ret = ret + 1
pause 1
text1.text = ret
loop
Note: "pause" is a custom function made by Sleep APICode:loop until stopscript = true
My other issue is that I can't pass and get value from my function (it's located in the main form), I'm getting error: 13, Type mismatch: 'map'
Using it like this:Code:Public Function map(x As Long, in_min As Long, in_max As Long, out_min As Long, out_max As Long) As Long
map = (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
End Function
Code:dim val
val = 234
msgbox map(val, 0, 1023, 0, 255)
Easier would be, when you define a Public Property on the Class-Object you priorily passed into the Script-Context,
to be able to influence conditional Loop-Behaviour "from the outside".
E.g. (in your form-objects declaration-section):
...will define such a Public Property already (as a simple-boolean-flag).Code:Public ExitMainLoop As Boolean 'such Public Defs can be used for simple shared values (with read/write access in both - the VB6-Form, as well as the Script-Code)
VBScript operates entirely in "Variant-Space" (Value-type-wise).
For that reason - it will have problems to pass params to (external, COM-Class-defined) function-arguments directly
(when a ByRef-argument in a function-def demands just that).
Two ways to avoid that problem in the external Object:
1) - use Variant-typed argument-defs in your Public function-def
2) - or change VBs (implicite) ByRef-args to ByVal-args in the function-signature
From inside the Script-Context one can avoid running into such type-mismatch errors by doing explicit casts
(via CStr(...) - or as in your case CLng(...)-wrapping of the passed args).
I'd fix that on the external Objects Function-def though (points 1 or 2), to keep the Script-code cleaner
(the less obstacles for "inexperienced scripters", the better)
Olaf
Okay, as with your proposal, everything is working excellent!
Many thanks for your help! ;)