Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

I have been wondering how to do this for a while, and i'm sure it can't be that complicated. How can I execute code from a textbox when i hit a button.

 

Example:

 

There is a form with a textbox and a button

The user types in "msgBox("Hello")" in the textbox

The user presses the button

A message box appers with "Hello" as it's text.

 

The user could do any type of code to happen when they hit the textbox.

 

How would i be able to do this? So basically:

 

Private Sub Button1_clicked(blah blah, blah) Handles Button1.click

'Code from textbox goes here

End Sub

 

Thanks in Advanced

 

--decrypt

Edited by decrypt
Posted

There is a tutorial around here about adding scripting so your app.

 

In a nutshell:

1. interop msscript.ocx

2. Instance it as a field in your form.

3 exit it in a manner similar to this:

Private Sub Button1_clicked(blah blah, blah) Handles Button1.click
_script
m_Script.Execute(textBox1.Text);
End Sub

 

The script engine can be configured to process VBScript or JScript by setting the Language Property at run time.

 

The script engine is extremely flexible and its uses are limited only by your imagination.

 

One purpose is to provide users to create batch processes. You can load your vb/c#/c++ COM objects into the engine and the user can execute their own code against them, calling the methods and procedures you have published through your interfaces. I have also used it to apply custom data transformations pre and post process.

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted
Thanks alot :)

 

I'm kind of a n00b to vb.net (i've only worked with it for 2 years part time) and i was wondering how to interop msscript.ocx. Do i just reference it and then declare it? (also, how would i declare it :P)

 

I never thought of looing up scripting, so i did. Is this a good article?:

 

http://weblogs.asp.net/rosherove/articles/DotNetScripting.aspx

boy I was tired last night. . .

'exit similar to this' should have read 'execute similar to this' and I had forgotton the actual script commands.

 

As far as your web link, that looks like a good start.

 

Divil's references will also work. The approach is a little more complex. If you decide to use dynamic .NET assembly creation (via compilation or emit) and want to run it in your app, you will have to spawn a subordinate AppDomain else you will not be able to 'reassemble' at runtime as it will be locked and loaded in your primary AppDomain.

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted (edited)

I don't want to compile .net code because i want to do something like this:

 

If blank = 1 Then
 script.ExecuteStatement(textbox1.Text)
End If
If blank = 2 Then
 script.ExecuteStatement(textbox2.Text)
End If

 

I am doing this so that the user can edit only some of the code in the project. That way they can edit it a bit to their needs.

 

Here is my current code:

   Dim Script As New MSScriptControl.ScriptControlClass
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Try
           Script.ExecuteStatement(textbox1.Text)
       Catch ex As Exception
           TextBox2.Text = Script.Error.Description & " | Line of error: " & Script.Error.Line & " | Code error: " & Script.Error.Text
       End Try
   End Sub
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Script.Language = "VBScript"
       Script.AddObject("Form1", Me, True)
   End Sub

 

(When i figure this all out i'll add it to my real project.)

 

Using the Script.AddObject("Form1", Me, True) do you know how i could add references to the script? (and also add buttons, and textboxes?)

 

definition: Script.AddObject(Name, Object, [add members as booleen = false])

 

Thanks for all of your help :)

 

edit: maybe i could add references using Script.Modules.Add(Name,[byRef Object As Object]), but how could i add buttons and textboxes to the script?

Edited by decrypt
Posted (edited)

You will need to publish out your 'Scriptable" .NET Classes with COM interfaces. look here

[mshelp=ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconexposingnetframeworkcomponentstocom.htm]Exposing .NET Framework Components to COM[/mshelp]

 

Add you COM Class instances via Script.AddObject with a name that will be unique within the session.

 

Your users program against it using this name.

In your documentation list the objects and their exposed methods.

 

In your app have places where "Entry Point" macros are executed

(Places to apply Script before and/or after methods are run.)

 

Give your users the ability to specify a sequence of Script Library Functions and parameter values for the functions that will be run at said entry points.

 

You can load the users script from textfiles, but. . .

 

I use an access database which I will attach here. I dont have any documentation for it here, but if you look at the relationships it should make sense.

 

Build a .NET interface for the DB to allow the user to create script code.

( the interface I have for the database is in Delphi . . . which rocks by the way!!! )

 

[EDIT]TEST DATA HAS BEEN REMOVED[/EDIT]

ScriptLibrary.zip

Edited by Joe Mamma

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted (edited)

wow, this is getting more complicated by the post. Are there any examples on how to do this on the internet? I find examples much better than tutorials.

 

How do i use the database file? And i was looking at it and it just said: VbScript - Dummy - Dummy (as I hit the + signs)

 

I just want it to be really flexible, and also, would it be easier to do this through .txt files?

 

Currently i am very confused. It would be very easy if I could see an example on how to do this (if you know of one).

 

I'm just wondering how i can simply do this. (even if i have to import everything seperatly.)

 

Thanks for you help

 

edit: i have attached a file of my practice solution

Scripting.zip

Edited by decrypt
Posted

How do i use the database file? And i was looking at it and it just said: VbScript - Dummy - Dummy (as I hit the + signs)

 

edit: i have attached a file of my practice solution

sorry. . .

 

I had cleared out a working product and changed a couple of things to make it non proprietary. the dummies were just to make sure I didnt screw anyting up. . . will edit it now and repost.

 

As far as it being complicated, there isnt anything real complicated in doing basic script execution.

 

But for giving COM automation control to the user via script control, it depends on how 'professional' it needs to be.

 

Are you going to manage their scripts for them?

- If not, how are you going to handle their 'stupid' mistakes such as having not loaded a text file module before calling some function they expected to be there.

 

Are you going to allow their script to interface with your app?

 

There are alot of things to consider.

 

Now the neat thing is. . . you do this right, you have created a solution that can be reused in all your applications.

 

Watch this space over the next week I will build on this.

Now is the best time for me to do this in .NET myself.

 

How's your c#?

 

Anyone feel like collaborating on this???

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

I just want to know how to add references, imports, buttons, textboxes, etc...

 

My c# is ok, but the application i'm working with has already been done in vb.net

 

And concerning the people making a mistake, i have a test button to make sure it works, and it debugs it and tells you if there is a mistake.

Posted
I just want to know how to add references, imports, buttons, textboxes, etc...

 

My c# is ok, but the application i'm working with has already been done in vb.net

 

And concerning the people making a mistake, i have a test button to make sure it works, and it debugs it and tells you if there is a mistake.

read that link about Exposing .NET to COM.

 

all public methods, properties and events are available to your script.

 

if my class has a public method like this.

 

public class Person
{
 string name;
 public string Name
 {
get { return this.name;}
set { name = value;
 }
}

 

I can add it to my scripting scope in .NET code via:

 

Person _person = new Person();
mScript.AddObject("CurrentPerson", _person, true);

 

now your runtime user can load write script like this

CurrentPerson.Name = "Joe Mamma"

 

and the name of _person will have changed.

 

now I jsut looked on my workstation and I see that the .hlp that comes with the control is not installed as part of the os. You can get that by downloading the script control addon:

 

1. locate your current msscript.ocx.

2. install this:

MS Script Control Download

3. if the original has a file data later than 'Monday, March 29, 1999' run:

regsvr32 [path to original msscript.ocx] to assure your original is the registered.

 

there are a lot of basic examples on the ms site.

 

the great thing about .NET is you can use someones code event though it is in a different language. just 'use' their assembly and derive from their clas in your favorite language.

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

nice, i got the new script control on it now.

 

Translation to vb.net (of your script):

 

Public Class Person 
  Private name As String
  
  Public Property Name() As String
     Get
        Return Me.name
     End Get
     Set
        name = value
     End Set
  End Property
End Class

Dim _person As New Person()
mScript.AddObject("CurrentPerson", _person, True)

CurrentPerson.Name = "Joe Mamma"

 

So what that does, is it stores the references in a class, and then adds them right? (i'm reading the com thing right now)

Posted

So what that does, is it stores the references in a class, and then adds them right? (i'm reading the com thing right now)

Actually, it stores a reference to the object, similar to late binding. . .

the addobject method of the script control creates a named reference to the added object inside the control.

 

the script control has no idea as to the actual interface presented by the object.

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

So what your talking about is adding buttons and textboxes etc.. to work with the script right? If i can type in "Form1.Left = Form1.Left + 200" and make it move by doing: "Script.AddObject("Form1", Me, True)" then why won't vb allow me to do "Script.AddObject("Button1", Me.Button1, True)"? So I have to do classes to add objects then?

 

(i don't know how this works:)

 

so i would go after i've made the class:

 

Dim Button1 As New Person()

mScript.AddObject("Button1", Button1, True)

 

I'm quiet confused what the person thing has to do with anything.

Posted
So what your talking about is adding buttons and textboxes etc.. to work with the script right? If i can type in "Form1.Left = Form1.Left + 200" and make it move by doing: "Script.AddObject("Form1", Me, True)" then why won't vb allow me to do "Script.AddObject("Button1", Me.Button1, True)"? So I have to do classes to add objects then?

 

(i don't know how this works:)

 

so i would go after i've made the class:

 

Dim Button1 As New Person()

mScript.AddObject("Button1", Button1, True)

 

I'm quiet confused what the person thing has to do with anything.

the fact that it is a type person has nothing to do with it. . .

the fact that person has a public property called name does. . .

 

is your button presented as a public element??? I bet not.

try (c# here)

 

public button ActionButton

{

get { return button1;}

}

 

And in your script reference Form1.ActionButton.

 

A good idea is to make an interface of all the events/methods/properties you want to be scriptable, and have your form implement the interface. that will cover all you bases.

 

do you really want to publish the button? or the method the onclick handler invokes???

 

take all the code in your on click handler, wrap it in a simple method.make it public.

In your onclick handler, call that method. And since it is public, the uiser can script it, too.

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted (edited)

Ok just to make it clear what i am doing here is what i am doing:

 

-It is an msn bot

-To make it very flexible (without giving away the source code), the person can either use a wizard to define commands (like if the sender types in !hello, the bot will respond "Hello" & sender.name), or else they can program in code, which shall allow them to collect system data like the system uptime. Or else if they really want to, they can make it so that when a command like "!changebutton1to Hello", and Button1's text will turn into "Hello"

-Using code, people can share their code with each other using code

 

Anyway's I had to say that to get you to understand what i'm doing, but here is your code translated:

Public ReadOnly Property ActionButton() As button
  Get
     Return button1
  End Get
End Property

I then did:

Script.AddObject("Button1", Me.ActionButton, True)

I get the error (in debuggin mode) and it reads:

"Additional information: Object reference not set to an instance of an object."

 

am i doing something wrong, but the way your doing this seems very smart. I just don't know why it doesn't work :S

 

edit: now in my script i tried doing "Form1.ActionButton.text = "Hey"" and it still didn't work :(

Edited by decrypt
Posted

given:

ThisForm is an instanced form (the object)

ActionButton a public property of ThisForm (the property)

 

add the object. in this case, the object, ThisForm, will be referenced by the name "AnythingYouWantToCallIt":

 

_scriptControl.AddObject("AnythingYouWantToCallIt", ThisForm, true)

 

in the script, manipulate the property of the object:

 

AnyThingYouWantToCallIt.ActionButton.Text = "New Text"

AnyThingYouWantToCallIt.ActionButton.Click

AnyThingYouWantToCallIt.ActionButton.Visible = False

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted (edited)

I did that and it still didn't work. Here is my full code:

Dim Script As New MSScriptControl.ScriptControlClass
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       TextBox1.Text = ""
       Try
           Script.ExecuteStatement(txtResult.Text)
       Catch ex As Exception
           TextBox1.Text = Script.Error.Description & " | Line of error: " & Script.Error.Line & " | Code error: " & Script.Error.Text
       End Try
   End Sub
   Dim frm1 As Form1
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       '"VB", "Visual Basic", "JScript.NET"
       Script.Language = "VBScript"
       Try
           Script.AddObject("Form1", Me, True)
       Catch ex As Exception
       End Try
   End Sub
   Public ReadOnly Property ActionButton() As Button
       Get
           Return Button1
       End Get
   End Property
   Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
       If RadioButton1.Checked = True Then
           RadioButton2.Checked = False
           Script.Language = "VBScript"
           Script.AddObject("Form1", Me, True)
       End If
   End Sub
   Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
       If RadioButton2.Checked = True Then
           RadioButton1.Checked = False
           Script.Language = "JScript"
           Script.AddObject("Form1", Me, True)
       End If
   End Sub

 

In the code i type in:

Form1.ActionButton.Text = "Bye"

I then get the following error from the debugger i built in: "Object required: 'Form1.ActionButton' | Line of error: 1" It is obviously is because it has not been made public. Or else that it's readonly. When i translated:

public button ActionButton 
{
get { return button1;}
}

 

It came out with:

 

Public ReadOnly Property ActionButton() As button
  Get
     Return button1
  End Get
End Property

I used the translator at: http://authors.aspalliance.com/aldotnet/examples/translate.aspx

 

Do you have any idea on how to make it not readonly?

Edited by decrypt
Posted

what happens when you execute this code:

			
Script.AddObject("Form1", Me, True)
Script.ExecuteStatement("Form1.Text=""New Caption""")

 

do you get the same error, or does it change the title of your .NET form???

if error, did you follow all of the steps outlined in the 'Exposing .NET Components to COM'?

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

It changes the title. That's what's really weird about it.

 

Look at this comment at the article at http://weblogs.asp.net/rosherove/articles/dotnetscripting.aspx:

 

"I got it to work, except, im using VBScript, and im trying to add a textbox as an object, so I can go like

txtbox2.write("Hey")

 

but I dont know how to make a text box an object, ive tried

 

Script.AddObject("textbox2",textBox2,True) - error, and you can use it to make a form an object, so anyone know how I make a text box an object? "

 

Script.AddObject("Form1", Me, True) is the only one that works.

 

And yes i have read that article, and it says that all of them have to be public, but it's all in c++, i can't undertand it.

Posted
what happens when you execute this code:

			
Script.AddObject("Form1", Me, True)
Script.ExecuteStatement("Form1.Text=""New Caption""")

 

do you get the same error, or does it change the title of your .NET form???

if error, did you follow all of the steps outlined in the 'Exposing .NET Components to COM'?

I am getting the same error when I try to refer to a control published as a property. . . give me a moment

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

I dont know why controls dont get published out.

 

here is a solution, it publishes out the properties of a control, instead of the control. Also calls a method of the form.

TestScript.zip

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

Wow, thanks that worked really well. Now i've got adding buttons, lables, textboxes, etc. all covered. Now i have figured out how to add references:

 

This works:

       InitializeScript()
       Dim blank As String
       script.ExecuteStatement("blank = ""Works""")
       script.ExecuteStatement(textbox1.text)

 

you have to set the strings property inside the script. This can be bad if you want to get the property from something defined outside of the script.

 

Here is how you can do this:

 

Dim blank As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       script.Language = "VBScript"
       blank = "Hey"
End Sub
   Public Property BlankString()
       Get
           Return blank
       End Get
       Set(ByVal Value)
           blank = Value
       End Set
   End Property

 

and then in the script do:

 script.ExecuteStatement("MsgBox(""Bye"" & Form1.BlankString)")

 

Thanks alot for you help. :) Doing that technique you can add as many things as you want :) Thanks alot. Bye now (and thanks for all the quick replies)

Posted

Thanks for kicking me in the *** to port over some things I have put on the back burner.

 

As soon as I get my script library manager, that runs against that db I posted, running I will post the project.

 

I found an issue with the db though. . . ado.net does not like tables named 'Language'

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

yikes, i just realized that you have to set the property of every single thing :S I'll have to find a way around that.

 

example: textbox1.text, textbox1.visible, etc...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...