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.
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
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"
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.
Re: Run code from textbox
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