如何创建动态按钮和事件处理程序 [英] How Can I Create Dynamically Button And Event Handler

查看:68
本文介绍了如何创建动态按钮和事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



i有一个主窗体和一些用户控件,我添加到主窗体

为每个用户控件我动态地在主窗体中添加一个按钮。

现在我如何为每个创建的按钮创建点击事件,显示添加的用户控件

示例:

i添加2个用户控件到主窗体控件所以我主表单上有2个按钮。所以我点击第一个按钮,我想显示第一个用户控件,添加

解决方案

首先,为什么不将按钮添加到您的用户控件而不是表格?



如果这真的是你的商业案例:

添加控件以形成 [ ^ ]

然后你需要附上一个事件处理程序。



我建议你确保Button有一个唯一的名字或者id在它的属性中,让所有按钮调用相同的处理程序。在那个处理程序中,你检查谁调用它并采取相应的行动。

示例 [ ^ ]



在这个例子中你可以修改类似这样的代码:

  public   partial   class  RoutedEventAddRemoveHandler { 
void MakeButton( object sender,RoutedEventArgs e)
{
按钮b1 = 按钮();
按钮b2 = 按钮();
b1.Content = New Button 1;
b2.Content = New Button 2;

// 将事件处理程序关联到按钮。您可以使用 - =语法而不是+删除事件
// 处理程序=。
b1.Click + = new RoutedEventHandler(OnbClick);
b2.Click + = new RoutedEventHandler(OnbClick);
// 在这里设置一些属性,如位置等。
// 确保将按钮添加到控件!
text1.Text = 现在点击第二个按钮......;

}
void OnbClick(对象发​​件人,RoutedEventArgs e)
{
if (((Button)sender).Content == 新按钮1){
text1.Text = 新按钮(b1)被点击!!;
}
else {
text1.Text = 新按钮(b2)被点击!!;
}
}





也许内容不是唯一设置的最佳属性,所以找另一个像名称或标签(取决于可用的内容)



希望这会有所帮助。


正如您所提到的那样,您正在添加用户控件和动态按钮然后你必须在创建按钮和用户控件的实例之后附加按钮的click事件的事件处理程序。



< pre lang =c#> UserControl control1 = new UserControl(); // 在此处创建用户控件的实例。
按钮button1 = new Button(); // 在此处创建按钮实例。

// 实现按钮单击事件处理程序,它将显示用户控件。
button1.Click + =(sender,eventArgs )= >
{
// 在此处显示用户控件。
};


假设:



1.你添加在运行时按钮和UserControl。



2. UserControls和按钮最初可见。



3.单击与每个UserControl关联的Button,将UserControl的可见性从隐藏切换为显示,并显示为隐藏。

  //  < span class =code-comment>测试添加八个UserControl和按钮。 

// 这个字典可以轻松查找
// 与之关联的UserControl每个按钮
私有字典< Button,UserControl1> dctBtnToUC = new Dictionary< Button,UserControl1>();

private void addUCs()
{
UserControl1 uc1;
Button btn;
int offH;

for int i = 0 ; i < 8 ; i ++)
{
offH = 80 +(i * 90 );

btn = new 按钮{Text = Hide UC,Location = new Point(offH, 100 )} ;
uc1 = new UserControl1 {Name = uc _ + i.ToString(),Location = new Point(offH, 200 ) };

this .Controls.Add(btn);
this .Controls.Add(uc1);

dctBtnToUC.Add(btn,uc1);

btn.Click + = btn_Click;
}
}

// 按钮单击EventHandler
private void btn_Click( object sender,EventArgs e)
{
Button btn = sender as Button;

// 获取相关的UserControl

// // > 按键作为键
UserControl1 uc = dctBtnToUC [btn];

uc.Visible =!uc.Visible;

btn.Text = uc.Visible? 隐藏UC 显示UC;
}


hi
i have a main form and some user control that i add to main form
for each user control i add a button in main form dynamically .
now how i can create click event for each created button that show a added user control
example :
i added 2 user control to main form controls so i have 2 button on main form. so i click first button and i want show first user control that added

解决方案

First of all, why don't you add the button to your user control instead of on the form?

if this is really your business case:
Add controls to form[^]
then you'll need to attach an event handler to it.

I suggest you make sure the Button has a unique name or id in it's properties and let all buttons call the same handler. In that handler you check who called it and act accordingly.
Example[^]

in this example you could modify the code similar to this:

public partial class RoutedEventAddRemoveHandler {
    void MakeButton(object sender, RoutedEventArgs e)
    {
        Button b1 = new Button();
        Button b2 = new Button();
        b1.Content = "New Button 1";
        b2.Content = "New Button 2";
        
        // Associate event handler to the button. You can remove the event  
        // handler using "-=" syntax rather than "+=".
        b1.Click  += new RoutedEventHandler(OnbClick);
        b2.Click  += new RoutedEventHandler(OnbClick);
        //set some properties like location etc here.
        //make sure to ADD the button to a control!
        text1.Text = "Now click the second button...";
       
    }
    void OnbClick(object sender, RoutedEventArgs e)
    {
        if(  ((Button)sender).Content == "New Button 1"){
          text1.Text = "New Button (b1) Was Clicked!!";
        }
        else{
          text1.Text = "New Button (b2) Was Clicked!!";
        }
    }



perhaps "Content" is not the best property to set uniquely, so find another like Name or Tag (depending on what is available)

hope this helps.


As you mentioned that you are adding user controls and buttons dynamically then you will have to attach an event handler for the click event of the button just after you are creating the instance of the button and user controls.

UserControl control1 = new UserControl(); // create the instance of your user control here.
Button button1 = new Button(); // create the instance of button here.

//Implement button click event handler which will show the user control.
button1.Click += (sender, eventArgs) => 
{
    //Show the user control here. 
};


Assuming:

1. you add both Buttons and UserControls at run-time.

2. UserControls and Buttons are initially visible.

3. clicking the Button associated with each UserControl switches the visibility of the UserControl from hidden to shown, and shown to hidden.

// test adding eight UserControls and buttons.

// this dictionary enables easy look-up of
// the UserControl associated with each Button
private Dictionary<Button, UserControl1> dctBtnToUC = new Dictionary<Button, UserControl1>();

private void addUCs()
{
    UserControl1 uc1;
    Button btn;
    int offH;

    for (int i = 0; i < 8; i++)
    {
        offH = 80 + (i * 90);

        btn = new Button { Text = "Hide UC", Location = new Point(offH, 100) };
        uc1 = new UserControl1 { Name = "uc_" + i.ToString(), Location = new Point(offH, 200)};

        this.Controls.Add(btn);
        this.Controls.Add(uc1);

        dctBtnToUC.Add(btn, uc1);

        btn.Click += btn_Click;
    }
}

// Button Click EventHandler
private void btn_Click(object sender, EventArgs e)
{
    Button btn = sender as Button;

    // get the associated UserControl
    // from the Dictionary using the
    // Button as the Key
    UserControl1 uc = dctBtnToUC[btn];

    uc.Visible = !uc.Visible;

    btn.Text = uc.Visible ? "Hide UC" : "Show UC";
}


这篇关于如何创建动态按钮和事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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