创建开关案例的循环 [英] loop that creates switch cases

查看:88
本文介绍了创建开关案例的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在尝试在交换机内创建一个循环,因为我正在生成一些按钮,每个按钮都有自己的功能,存储在我的数据库中。



但我一直无法这样做,因为交换机只接受常量并在其中创建循环似乎无法正常工作。



这是我的源代码:

  void  button_click( object  sender,EventArgs e 
{
Button button = sender as Button;
if (button!= null
{
switch (( int )button.Tag)
{
case 1 // 点击第一个按钮

break ;


case 2 // 点击第二个按钮

break ;

case 3 / / ... etc

break ;

}
}

}





我想要的是创建所有案例的循环,而不是手动插入它们,因为我有一个选项,允许用户向他的控件添加按钮,这样应用程序从数据库中检索所有存储的按钮并生成它们,我已成功完成那,我现在的问题是以dinamicaly的方式添加功能,不需要我更改代码。



提前谢谢

解决方案

如果我理解你的问题,你想在动态生成的多个按钮上使用一个事件处理程序,并以特定方式处理每个按钮......为什么要使用案件?尝试考虑lambda表达式。

这里的例子。



  //  创建一个带有匿名事件处理程序的按钮 
System.Windows.Forms.Button btn = new System.Windows.Forms.Button();
...
btn.Text = 我是一个按钮# + i.ToString();
btn.Tag = i + 1 ;
// 在按钮创建时添加匿名函数
btn.Click + = (l_sender,l_eargs)= > {
按钮b =((按钮)l_sender);
b.Text = + b.Tag.ToString()+ 点击!;
};
...
this .Controls.Add(btn);


< blockquote>你好,



我想我已经理解你的问题:

你从数据库中检索一个按钮列表,相应于用户'的个人资料

您在某个用户页面上显示所述按钮列表

您想要将正确的事件处理连接到按钮



我的解决方案是将方法的名称存储在Button.Tag属性中而不是序列号中。这样你就可以使用反射并调用适当的方法来点击按钮:



  void  button_click( object  sender,EventArgs e)
{
Button btn = sender as 按钮;
if (!btn == null
{
< span class =code-keyword> this .GetType()。InvokeMember( this ,btn.Tag);
}
}





好​​吧,我对反射的使用有点生疏,但样本会让你回来工作。


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:

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

解决方案

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.

// 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);


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:

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.


这篇关于创建开关案例的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆