Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
So I've been trying to create a loop inside a switch because I'm generating dinamicaly some buttons which each of them has it's own functionality that is stored in a database of mine.

But i've been unable to do so, since the switch only accepts constants and creating loops inside of it doesn't seem to work.

Here is my source code:
C#
void button_click(object sender, EventArgs e
        {
            Button button = sender as Button;
            if (button != null)
            {
                    switch ((int)button.Tag)
                    {
                        case 1: // FIrst button clicked

                            break;


                        case 2: // Second button clicked

                            break;

                        case 3: // ... etc

                            break;

                    }
            }

        }


What I want is a cycle that creates all cases, instead of inserting them manualy, because I have an option that lets the user add buttons to his control, this way the application retrieves from the database all the stored buttons and generates them, I've successfuly done that, my problem right now is to add the functionality in a dinamicaly way that doesn't require me to change the code.

Thanks in advance
Posted
Comments
OriginalGriff 7-Feb-13 7:39am    
I'm not exactly certain what you are trying to do and why it's causing your a problem, but creating a loop inside a switch does work - a switch is just an optimised if...else if...else construct after all.
Why doesn't it work for you? What kind of loop are you trying to build? Why does the switch working with constants mess up your loop?
Perhaps more info on what you are trying to achieve would help? Remember we can't see your screen and access your HDD...
Joezer BH 7-Feb-13 8:42am    
I think he wants to create the "case n: ... code ... break;" dynamically,
an email to Microsoft dudes perhaps would be start.
Fred Flams 7-Feb-13 9:16am    
If it is what he really want to achieve, then there is no need to send an email to some Microsoft dude, the answer is CodeProvider.

If I understood your question correctly, you want to use one event handler on click event from multiple buttons generated dynamically and handle each of them in a specific way... Why to use case? Try to consider lambda expression.
Here's example.

C#
// creating a button with anonymous event handler
System.Windows.Forms.Button btn = new System.Windows.Forms.Button();
...
btn.Text = "I am a button #" + i.ToString();
btn.Tag = i + 1;
// adding an anonymous function on button creation
btn.Click += (l_sender, l_eargs) => { 
    Button b = ((Button)l_sender);
    b.Text = " #" + b.Tag.ToString() + " clicked!";
};
...
this.Controls.Add(btn);
 
Share this answer
 
Comments
Steven Borges 8-Feb-13 3:44am    
I never heard of lambda, It's only my second year in my course programing on C# so I've only heard of the case like it's used in Pascal.

Could you please explain this code:

btn.Click += (l_sender, l_eargs) => {
Button b = ((Button)l_sender);

How do I insert a certain code to the button depending on the tag with this?

Thanks for your answer.
Steven Borges 8-Feb-13 4:10am    
Oh I think I understood it already, am I supposed to do it like this?:

btn.Click += (s,e) => { my code; };

or:

btn.Click += (l_sender, l_eargs) => {
Button b = ((Button)l_sender);
[my code];
skydger 8-Feb-13 4:18am    
You're free to write both of them. It is just a function without name :). If you need a button manipulation use second code.
Here is more info about the lambda expressions:
http://msdn.microsoft.com/en-us/library/bb397687.aspx
Steven Borges 8-Feb-13 6:00am    
Thank you so much, it's working now.
skydger 8-Feb-13 6:19am    
You're welcome :)
Hello,

I think I have understood your problem:
You retrieve a list of buttons from a database accordingly to a user's profile
You display said list of buttons on some user page
You want to wire the right events handling to the buttons

My solution would be to store a method's name in the Button.Tag property rather than a sequence number. In that way you can use reflection and call the apropriate method for the button being clicked:

C#
void button_click(object sender, EventArgs e)
{
    Button btn = sender as Button;
    if (!btn == null)
    {
        this.GetType().InvokeMember(this, btn.Tag);
    }
}


Well, my use of reflection is a bit rusty but that sample should get you back to work.
 
Share this answer
 

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