Click here to Skip to main content
15,915,703 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am able to create the controls at run time. I want to be able to create the event and the code for that event(click for buttons, check boxes, radio buttons) that will be able to store the results dynamically.(I know how to do that in a static code way) I may create any number of controls based upon user input.

I am getting the error
The name "btnU_Click" does not exist in the current context

sample code is below
thanks in advance

C#
var btnU = new Button();
btnU.Location = new Point(1280, 3);
btnU.Size = new Size(40, 26);
btnU.ForeColor = System.Drawing.Color.Red;
btnU.BackColor = System.Drawing.Color.White;
btnU.Font = font;
btnU.Text = "Prev";
btnU.Click += new System.EventHandler(btnU_Click); //The name "btnU_Click" does not exist in the current context
btnU.Click += (s, e) => MessageBox.Show("Hi there from btnU  PREV");
this.Controls.Add(btnU);
btnU.Show();
Posted

Why use var, when you know what type it should be ? Your event handler code needs to specify the name of the method to call. You need to be able to dynamically add code to your project, in order to add buttons that run aribtrary code. btnU_Click needs to be the name of a method with the signature of a click event, for this code to work.
 
Share this answer
 
Comments
Member 8572197 21-Aug-12 16:43pm    
I changed the var to Button, and the same error exists
Christian Graus 21-Aug-12 16:49pm    
Despite what the other poster said, this does not fix your problem, it was just a comment. Read my answer. Your code is broken and the error message is correct. You can only link to an event that exists as a method in your code.
 
Share this answer
 
The short answer is: use anonymous methods as you already do, only… Here is the more general syntax:
C#
btnU.Click += (sender, eventHandler) => { /* put some code here, use any of sender or eventHandler, or both, or none of those parameters */ }


Was that your problem? If not, please explain.

—SA
 
Share this answer
 
Comments
Member 8572197 21-Aug-12 16:59pm    
Sort of, the main issue that I have is that I can have possible 500-1000 controls generated by the user (creating a dynamic questionnaire) so I will not know until run time how many buttons, check boxes, radio buttons, text boxes will be generated.
If I create one event for each type of control, and put a case statement it it it would be huge. I would like to create the event and code only as needed.
Sergey Alexandrovich Kryukov 21-Aug-12 17:22pm    
As you can put any code inside those brackets { }, you have a chance to develop some system which takes the user data and make the response out of it.
I need to know more on your goals, user activities, and a lot more to give more specific answer, but I had a project based on a rule-based language where the user graphically specifies workflows, standards, endpoint, mappings, etc., so the host service build all required activities from this data; I had a project where a user defines a production process using some graphical language, and I had a project where I use System.Reflection.Emit to actually compose the code out of data extracted using Reflection from metadata just once, emit the code to be reused many times later.

This is all is very close to what you probably want, but I feel that you did not formulate it for yourself yet. This is normal process of creation of new architecture. Perhaps some experience like mine could be useful for you to get some ideas...

--SA
Sergey Alexandrovich Kryukov 21-Aug-12 17:24pm    
Perhaps, for now you should consider accepting my answer formally (green button).
If you have something to discuss in particular, I would gladly share my experience...
--SA
Member 8572197 21-Aug-12 17:28pm    
Sergey,
Thanks, I will click the green button, and if I need more assistance, I will add a comment to this at a later time.
Thanks,
Pete
Sergey Alexandrovich Kryukov 21-Aug-12 18:06pm    
Great. You are very welcome; your follow up questions, too.
Good luck,
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900