Results 1 to 7 of 7

Thread: Run code from textbox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Location
    Belgium
    Posts
    156

    Run code from textbox

    So,
    I searched, and found a code.
    I added scriptcontrol1 component(and also added in references)
    I have this code:
    Code:
    scriptcontrol1.executestatement text1.text
    when i enter "msgbox "test"" in text1 and press command1 button then it shows a messagebox
    however
    when i enter "form4.caption = "test"" then it shows error 424: object required form4

  2. #2
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Run code from textbox

    You use the control's AddObject method to add host objects to the script's namespace.

    A Google search should produce a number of introduction and tutorial articles.

  3. #3
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Run code from textbox

    Do it like this. Instead of adding the script control add a reference to the Microsoft Script Control 1.0 in your project's references.
    Code:
    Option Explicit
    
    Dim script As ScriptControl
    
    Private Sub Command1_Click()
     script.ExecuteStatement Text1.Text
    End Sub
    
    Private Sub Form_Load()
     Set script = New ScriptControl
          
     script.Language = "VBScript"
     script.AddObject "Form1", Form1
       
     'Text1.Text = "MsgBox ""Hello"""
     'Text1.Text = "Form1.Command1.Caption = ""Hello"""
    
     Text1.Text = "Form1.Caption = ""Hello"""
    End Sub
    Last edited by jmsrickland; Jun 18th, 2008 at 11:23 AM.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Location
    Belgium
    Posts
    156

    Re: Run code from textbox

    The above code works fine but how can I like, change variables?

    I have

    Code:
    Public Type Player
    Attack As Integer
    Class As Integer
    DeathCount As Integer
    Defense As Integer
    EXP As Integer
    HP As Integer
    KillCount As Integer
    Level As Integer
    MaxHP As Integer
    Meteors As Integer
    Money As Integer
    Name As String
    End Type
    in a module.

    But i can't use
    Code:
    Player.Name = "test"

  5. #5
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Run code from textbox

    You will have to call a Sub that loads the fields of the UDT and you also need to dimension the UDT, which you didn't do.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Location
    Belgium
    Posts
    156

    Re: Run code from textbox

    how could i do it?

  7. #7
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Run code from textbox

    In a Module add the following
    Code:
      '
      '
    Public Type Player
      Attack As Integer
      Class As Integer
      DeathCount As Integer
      Defense As Integer
      EXP As Integer
      HP As Integer
      KillCount As Integer
      Level As Integer
      MaxHP As Integer
      Meteors As Integer
      Money As Integer
      Name As String
    End Type
      '
      '
    In the Form add the following
    Code:
      '
      '
    Dim script As ScriptControl
    
    Dim Players As Player
    
    Private Sub Command1_Click()
     Text1.Text = "Sub CallLoadPlayers" & vbCrLf & _
                  " '" & vbCrLf & _
                  " ' This is the internal Sub that the VBScript Calls" & vbCrLf & _
                  " '" & vbCrLf & _
                  " LoadPlayers" & vbCrLf & _
                  "End Sub"
    
     Set script = New ScriptControl
          
     script.Language = "VBScript"
     script.AddObject "Any text you want", Me, True
     script.AddCode Text1.Text
    
     '
     ' Run the Sub that is in the VBScript code
     '
     script.Run "CallLoadPlayers"
    End Sub
    
    Public Sub LoadPlayers()
     '
     ' This Sub us called by the VBScript in the Text1 Textbox
     '
     Players.Name = "test"
     '
     '
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width