Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Executing code

Status
Not open for further replies.

Honda

Programmer
Feb 23, 1999
10
0
0
AU
I want to be able to execute a line of code that has been typed into a text box by a user (say form1.show) when a button is pressed. I've tried placing text1.text in the click event of the button, however that does not work. Any ideas or work arounds would be greatfully appreciated.
 
first of all you would have to make sure that users were aware of the names of the events they are trying to trigger. YOu would have to use a series of if..then statements to check the text in text1. If the if...then statement finds a match you could fire of the event.<br>
<br>
private sub Command1_Click()<br>
<br>
if text1.text = "form1.show" then<br>
form1.show<br>
elseif text1.text = "end" then<br>
else<br>
msgbox "Not an executable string", vbinformation<br>
end <br>
end if<br>
<br>
end sub
 
Ok<br>
Say there is a valid event in the textbox ie form1.show , is there any way of executing that without looping through the "if.. then" routine??
 
<br>
I think that you should consider using "VBScript". <br>
<br>
A colleague demonstrated a VB5 app to me. A form, a textbox and a button. Type into the text box click the button and it executed (interpreted) the code. No need to predict the code entered with the "if ... then" stuff above.<br>
<br>
My colleague can't help at the present as he has his hands full. But why not visit as a starting point. You'll need to download the Microsoft Script Control.<br>
 
My colleague says...<br>
<br>
By using the vbscript control, you can expose objects in your application to the global namespace.<br>
<br>
So you would expose your form as say theForm and in the textbox type <br>
<br>
sub() <br>
theForm.Show <br>
end sub <br>
<br>
On your lostfocus event on the textbox or a button click , you should take the text and get the script control to execute it, and hey presto, Bob's your aunt's live in lover, <br>
it should all work. It also allows you to save the vbscript to file, to be loaded back in at run time and executed if you wish, thus giving you a fully customisable environment<br>
<br>
Hope this helps<br>
<br>
<br>
Mark Ruse
 
I just tried it and it works.<br>
Here's an example.<br>
<br>
In a new project drop a ScriptControl(see below), 2 Labels, 2 big TextBoxs & 2 CommandButtons onto a new form. Then paste in the following code<br>
------------------------------------------------------------<br>
Option Explicit<br>
<br>
Private Sub Command1_Click()<br>
ScriptControl1.Language = "VBScript"<br>
ScriptControl1.AddCode (Text1.Text)<br>
ScriptControl1.Run ("Test")<br>
End Sub<br>
<br>
Private Sub Command2_Click()<br>
ScriptControl1.Language = "VBScript"<br>
ScriptControl1.ExecuteStatement (Text2.Text)<br>
End Sub<br>
<br>
Private Sub Form_Load()<br>
'Expose the form as frmMainWindow<br>
ScriptControl1.AddObject "frmMainWindow", Me <br>
Text1 = "sub Test()frmMainWindow.label1=""hello"" end sub"<br>
Text2 = "frmMainWindow.label2=""world"""<br>
End Sub<br>
------------------------------------------------------------<br>
The script control can be found, amongst other places, on the VB6 CD in \common\tools\vb\script. The readme.txt is quite straightforward.<br>
<br>
Sean.
 
I cant thank U enough for such a comprehensive answer !!!<br>
Cheers.<br>
Honda
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top