Is there a way to add code to a control's event at run time? For example. I have 2 commandbuttons on my form and when i press the first one, I want it to place MsgBox("Hi") in the second one's click event.
Printable View
Is there a way to add code to a control's event at run time? For example. I have 2 commandbuttons on my form and when i press the first one, I want it to place MsgBox("Hi") in the second one's click event.
No is the short answer.
what are you trying to do, there may be a way round it.
trying to do this would be a MONUMENTAL WASTE OF TIME.
declare a boolean variable and change it to true when you click the first button. then in the second button's click event, put an if then else block to only show the "HI" message if the variable is TRUE.
what i'm trying to do is create an exe completey from code.
i just add the controls with code, set the properties with code, and code everything with code.
This thread my give you some ideas.
http://forums.vb-world.net/showthrea...threadid=15757
You might also explore scripting options, though, what with the "I love you" & Herbie screw-ups, scripting may soon take a less accessable, open route.
There is actually a way:
Create a label. And in the command button, put this:
It won't display a msgbox until the command is pushed twice.Code:Label1.caption = Label1.caption + 1
If Label1.caption = "2" Then
Msgbox "Hi"
End If
Hope you get the idea.
But he'll get some problems when he does complex code with IF THEN statements. I think it's best to use Mongo's method.
There is kind of a way, you need to break all the code you might want to write into smalll functions and put them as public functions in a standard module and have a big array with their addresses, then you can call them with the callwindowproc API, they all need the same interface though, and it's not an easy thing to do, but running actual VB code is impossible.
if you convince your father to give me a billion $$ or so I will tell you whats wrong with your code...
oh well, I'll tell ya anyway..
thats not CHANGING the code at runtime, what you have is using DIFFERENT code at runtime.
also, to be safe, you should do this
Code:Label1.caption = Clng(Label1.caption) + 1
If Label1.caption = "2" Then
Msgbox "Hi"
End If
OR
Label1.caption = Val(Label1.caption) + 1
If Label1.caption = "2" Then
Msgbox "Hi"
End If
Are you trying to get it to add another line of code for each time they click on the first button? If so you could get around it by placing the following line of code in the first button click event:
and this in the second one:Code:MsgNumber=MsgNumber+1
Code:For I = 1 To MsgNumber
MsgBox("Hi")
Next I
Or if you're wanting it to execute one line of code before pressing the first button, another after then you could insert the following code in the first button's click event:
and this in the second's:Code:Clicked = True
I bet I really just don't know what you're talking about, but I thought I'd just throw in my two cents worth.Code:If Clicked = False Then
'Do this
Else
MsgBox("Hi")
End If
If you want to create a program from scratch, keep in mind that there are some things that you just can't do. for example, you cannot change Read-Only values. Also, you cannot have access to some events like the Change event in the TextBox.
Ah yes, forgot about the Val function. Sorry!
But remember, i had the idea of doing this first!
Sorry, I don't think I can spare dad's money.
[Edited by Matthew Gates on 05-27-2000 at 08:40 PM]