|
-
Sep 15th, 2018, 07:09 AM
#1
Thread Starter
Addicted Member
[RESOLVED] MSScriptControl - Read value from TextBox
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!
Last edited by beic; Sep 15th, 2018 at 07:44 AM.
Reason: typo
-
Sep 15th, 2018, 10:09 AM
#2
Re: MSScriptControl - Read value from TextBox
What would be the purpose of the msscriptcontrol?
-
Sep 15th, 2018, 12:18 PM
#3
Thread Starter
Addicted Member
Re: MSScriptControl - Read value from TextBox
 Originally Posted by Arnoutdv
What would be the purpose of the msscriptcontrol?
I would like to make my project to be custom scriptable, I have many readings/values from microcontroller sensors and I wish to parse, validate them at run-time.
-
Sep 15th, 2018, 04:47 PM
#4
Re: MSScriptControl - Read value from TextBox
 Originally Posted by beic
I would like to make my project to be custom scriptable, I have many readings/values from microcontroller sensors and I wish to parse, validate them at run-time.
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):
Code:
ScriptControl.AddObject "frmMyForm", Me
After that, you should be able to address your TextBoxes in ScriptCode over e.g.:
Code:
MsgBox frmMyForm.Text1.Text
In case you want to export the Public Members (Functions and Properties) of your Form
as an extension to your Script-Context, you would specify an additional True-Param in the AddObject-call:
Code:
ScriptControl.AddObject "frmMyForm", Me, True
After that, you should be able to address your TextBoxes in ScriptCode, leaving out the leading frmMyForm-prefix:
Olaf
-
Sep 15th, 2018, 05:06 PM
#5
Re: MSScriptControl - Read value from TextBox
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.
-
Sep 15th, 2018, 07:52 PM
#6
Thread Starter
Addicted Member
Re: MSScriptControl - Read value from TextBox
 Originally Posted by dilettante
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.
-
Sep 15th, 2018, 08:06 PM
#7
Thread Starter
Addicted Member
Re: MSScriptControl - Read value from TextBox
 Originally Posted by Schmidt
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):
Code:
ScriptControl.AddObject "frmMyForm", Me
After that, you should be able to address your TextBoxes in ScriptCode over e.g.:
Code:
MsgBox frmMyForm.Text1.Text
In case you want to export the Public Members (Functions and Properties) of your Form
as an extension to your Script-Context, you would specify an additional True-Param in the AddObject-call:
Code:
ScriptControl.AddObject "frmMyForm", Me, True
After that, you should be able to address your TextBoxes in ScriptCode, leaving out the leading frmMyForm-prefix:
Olaf
I would like to use something like this:
Code:
ScriptControl.AddCode txtScript.Text
ScriptControl.Run "Default"
But maybe I asked or explained in a wrong way what I wish to achieve.
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.
Last edited by beic; Sep 15th, 2018 at 08:37 PM.
Reason: typo
-
Sep 15th, 2018, 09:14 PM
#8
Re: MSScriptControl - Read value from TextBox
 Originally Posted by beic
...my Script Function named "Default" need to read the value from the txtValue.Text from the frmMAIN real form.
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
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
Now click the Form, to run the Code (with the little routine, which I've named "Sub Test()".
HTH
Olaf
-
Sep 16th, 2018, 02:19 AM
#9
Re: MSScriptControl - Read value from TextBox
 Originally Posted by beic
I did not get any relevant search hits unfortunately.
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.
-
Sep 16th, 2018, 07:51 AM
#10
Thread Starter
Addicted Member
Re: MSScriptControl - Read value from TextBox
 Originally Posted by dilettante
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.
Yeah, I have the first release of 5x CDs a complete of MS VS 6.0, but I found a downloadabe ISOs on archive.org and I will download them as you mentioned above.
Thank you!
-
Sep 16th, 2018, 07:59 AM
#11
Thread Starter
Addicted Member
Re: MSScriptControl - Read value from TextBox
 Originally Posted by Schmidt
[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
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?
Code:
dim ret
do
ret = ret + 1
pause 1
text1.text = ret
loop
or just I make it like?
Code:
loop until stopscript = true
Note: "pause" is a custom function made by Sleep API
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'
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
Using it like this:
Code:
dim val
val = 234
msgbox map(val, 0, 1023, 0, 255)
Last edited by beic; Sep 16th, 2018 at 09:47 AM.
Reason: typo
-
Sep 16th, 2018, 06:49 PM
#12
Re: MSScriptControl - Read value from TextBox
 Originally Posted by beic
Is there any stop script execution command in MSScriptControl?
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):
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)
...will define such a Public Property already (as a simple-boolean-flag).
 Originally Posted by beic
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'
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
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
-
Sep 17th, 2018, 03:48 AM
#13
Thread Starter
Addicted Member
Re: MSScriptControl - Read value from TextBox
Okay, as with your proposal, everything is working excellent!
Many thanks for your help!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|