dcsimg
Results 1 to 13 of 13

Thread: [RESOLVED] MSScriptControl - Read value from TextBox

  1. #1

    Thread Starter
    Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    49

    Resolved [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

  2. #2
    PowerPoster
    Join Date
    Oct 2013
    Posts
    3,161

    Re: MSScriptControl - Read value from TextBox

    What would be the purpose of the msscriptcontrol?

  3. #3

    Thread Starter
    Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    49

    Re: MSScriptControl - Read value from TextBox

    Quote Originally Posted by Arnoutdv View Post
    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.

  4. #4
    PowerPoster
    Join Date
    Jun 2013
    Posts
    3,737

    Re: MSScriptControl - Read value from TextBox

    Quote Originally Posted by beic View Post
    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:
    Code:
      MsgBox Text1.Text
    Olaf

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    19,074

    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.

  6. #6

    Thread Starter
    Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    49

    Re: MSScriptControl - Read value from TextBox

    Quote Originally Posted by dilettante View Post
    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.

    Name:  msdn_search.jpg
Views: 64
Size:  59.3 KB

  7. #7

    Thread Starter
    Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    49

    Re: MSScriptControl - Read value from TextBox

    Quote Originally Posted by Schmidt View Post
    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:
    Code:
      MsgBox Text1.Text
    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.

    Name:  scripting.png
Views: 63
Size:  8.6 KB
    Last edited by beic; Sep 15th, 2018 at 08:37 PM. Reason: typo

  8. #8
    PowerPoster
    Join Date
    Jun 2013
    Posts
    3,737

    Re: MSScriptControl - Read value from TextBox

    Quote Originally Posted by beic View Post
    ...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

  9. #9
    PowerPoster
    Join Date
    Feb 2006
    Posts
    19,074

    Re: MSScriptControl - Read value from TextBox

    Quote Originally Posted by beic View Post
    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.

  10. #10

    Thread Starter
    Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    49

    Re: MSScriptControl - Read value from TextBox

    Quote Originally Posted by dilettante View Post
    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!

  11. #11

    Thread Starter
    Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    49

    Re: MSScriptControl - Read value from TextBox

    Quote Originally Posted by Schmidt View Post
    [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

  12. #12
    PowerPoster
    Join Date
    Jun 2013
    Posts
    3,737

    Re: MSScriptControl - Read value from TextBox

    Quote Originally Posted by beic View Post
    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).

    Quote Originally Posted by beic View Post
    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

  13. #13

    Thread Starter
    Member beic's Avatar
    Join Date
    Jun 2012
    Posts
    49

    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
  •  



Featured


Click Here to Expand Forum to Full Width