Is there a way to create code during runtime?

MiserableMad

Active member
Joined
Feb 2, 2007
Messages
25
Programming Experience
Beginner
I have this

VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] m_ControlCount [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] m_Location [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Point(10, 10)
 
[/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Button1_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Button1.Click
m_ControlCount += 1
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] m_ControlCount <= 5 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] x [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Button
x.Name = "Button" & m_ControlCount.ToString
x.Text = "Button" & m_ControlCount.ToString
x.Location = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Point([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].m_Location.X + 150, [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].m_Location.Y)
m_Location.Y += x.Height + 5
[/SIZE][SIZE=2][COLOR=#0000ff]AddHandler[/COLOR][/SIZE][SIZE=2] x.Click, [/SIZE][SIZE=2][COLOR=#0000ff]AddressOf[/COLOR][/SIZE][SIZE=2] myButtonHandler_Click
Controls.Add(x)
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]MsgBox("You've reached 5 controls. Clear controls to start again.", _
MsgBoxStyle.OKOnly, [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Text)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] myButtonHandler_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] EventArgs)
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]TypeOf[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]Is[/COLOR][/SIZE][SIZE=2] Button [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]MsgBox([/SIZE][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2](sender, Button).Text + " was pressed!", _
MsgBoxStyle.OKOnly, [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Text)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

I wonder if I can put a code event handler for that button click?
 
Please be more specific. You can also put code inside or call on routines out side to durive within the scope of that event handler. You also have the option to start a new process of thread from within that click event.

However; if you want the code to be written then performed when you click the button then you have a task on your hands. Can it be done? I am most sure there is a way but it may be complicated. However; if you know how to script then you can easily write script, save it, then run it from that event.

Again, please be a little more specific as to what you want b/c I'm not perfectly sure I understand.
 
The short answer is, yes. The longer answer is, but it's not going to do what you think. First off, as soon as the sub ends, the control no longer exists. Which leads me to: Secondly, you never add it to your form, or other container. You need to add it to your form's Controls collection, or to some other container's Controls collection (like a tab page, a panel, group box, etc).

Once you've got it added, it doesn't matter if the scope of the variable used to make it falls off.

-tg
 
Sorry, I want to know if there is a way to add the code during runtime. I will steralize the code and save it using XML.

Wouldn't the "Controls.Add(x)" save the button as a control, how would I add it to another control (tab ect ect)?
 
Sigh....
OK... one step at a time. You jumbled up what I said.

Creating a control is easy. You've done that in your code.
What you DIDN'T do was to add it somewhere... it has no parent. It will not show up on your form anywhere. IF you want to add it to the form it's yourform.Controls.Add X .... to add it to a tab: yourTab.Controls.Add X , etc.

As for handling the click event, again, that's something you got right:
AddHandler x.Click, AddressOf myButtonHandler_Click
That's the way to do it.

Keep in mind, nothing is saved, it's all in memory, the next time the form is opened, the buttons will be gone (unless you add them through code again as above). I'm not sure what you want to save in the XML, but there's nothing in your code that needs it.

-tg
 
TechGnome: What he wants to do is create actual lines of code inside the program while the program is running. The purpose of this I am not sure but this is what he's asking.

MiserableMad: This task is gonna cost you more time and effort than you have I do believe. You may find a simpler work around as I mentioned above by scripting, which is something programmers have been doing for years but to add actual lines of code to the existing program will definately cost you more than you want. First off, the program has to be compiled into an executable. Once that executable it ran it is compiled Once, and only once to the CLR. This means that even if you found a way to add lines of code to your program you would have re-compile it and re-start the newly compiled version because the CLR has no way to retrieve the updated lines of code.
 
Back
Top