English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have the code to create a new button during runtime. NOT VISIBLE = TRUE!

I did a
AddHandler x.Click, AddressOf myButtonHandler_Click

Me.Controls.Add(x)

Then I created a Sub(ByVal sender As Object, ByVal e As EventArgs)

I want to know if there is a way to take the users code in a textbox and use that code as the buttons CLICK event code handler. Did any of that make sense?

BASICLY...what I want to do, is to be able to write the code during runtime and use that code when ever the btn.Click event is initiated.

2007-03-22 03:39:19 · 2 answers · asked by Anonymous in Computers & Internet Programming & Design

2 answers

Ok I tried to paste demo code in here but Yahoo restricts line lengths so you get elipses (i.e. ...) on many lines which restricts you from just copying and pasting the whole thing. Check the references for a link to a text file with the demo code. I would of zipped it but I'd rather not link to executables or zip files.

What this code does is:
1. Has an Interface that defines a simple method we can run
2. Takes code supplied in the text box and wraps it into a class that implements the interface and the simple method (code is put in the method)
3. Creates a VB.NET compiler and compiles the code into memory
4. Finds the ICode interface and runs the one method it defines, thereby executing your code supplied in the textbox.

I tried to comment it as best I could. Sorry if my VB is sloppy, I haven't written anything in it since .net came out =). I use C# mostly and this code is adapted from my script executing application that is in C#.

Using this as a framework, you can easily make far more complex runtime code.

EDIT: I missed the part about adding a control to your form, so I modified the code to give access to the form in the method. The first link has my original code, the second link is modified to pass the form object through to the method. Try using this code in the textbox:

Dim btn As New Button
btn.Text = "Test"
form.Controls.Add(btn)

And it'll add a new button to your form. I know you'll want to respond to events the button makes at runtime, but I am not sure how to add event handler at runtime in VB, I do in C# but VB uses that "Handles object.event" at the end of a method which doesn't appear to be very dynamic. If I have time to look into it I'll post another edit with an event handler.

EDIT2: I found the way to dynamically add event handlers ( http://www.samspublishing.com/articles/article.asp?p=23020&rl=1 ). I added demo3.txt and another file with the code to put in the textbox at runtime to demonstrate it. The code became too long to put in here just like the form code. Please post an addendum to your question if you have issues with this demo code.

2007-03-22 04:35:44 · answer #1 · answered by Anonymous · 0 0

Interesting idea...of course the code the user types in would have to be perfect syntax. A memo box might be better than a text box.

How about if you append a SUB name to the text...something like:

frmMain.aspx.vb.SubUser =

"Private Sub SubUser" & txtBoxUser.text & "END SUB"

Do that step first then call SubUser in the click event

I'm going to STAR this question...I'll be interested if someone comes up with a method to make this work.

good luck

2007-03-22 04:25:11 · answer #2 · answered by rod 6 · 0 0

fedest.com, questions and answers