简体   繁体   English

如何添加具有独立单击事件的按钮

[英]How to add buttons with independent click event

I want to add buttons and textboxes dynamically on runtime with each button react differently. 我想在运行时动态添加按钮和文本框,每个按钮的反应都不同。

ie newbutton1 linked with texbox1 , newbutton2 linked with textbox2` newbutton1newbutton1链接texbox1 , newbutton2 linked with textbox2 linked with

Right now any button just prints from the first to the last textbox one after the other. 现在,任何按钮都只会从第一个到最后一个文本框一个接一个地打印。

Also consider that I have a button1 & textbox1 already on the form for guides 还请考虑我已经在指南的表单上有一个button1和textbox1

Here is my code : 这是我的代码:

        List<Button> buttons = new List<Button>();
        List<TextBox> textboxes = new List<TextBox>();

        int NumTextBox = 0;
        void click(object sender, EventArgs e)
        {

            MessageBox.Show(textboxes[NumTextBox].Text);
            NumTextBox++;
        }

        int x = 0;
        int y = 0;
        void AddClick(object sender, EventArgs e)
        {
                Button newButton = new Button();
                buttons.Add(newButton);
                newButton.Click += click;// 
               // newButton.Location.Y = button1.Location.Y + 20;
                newButton.Location = new Point(button1.Location.X, button1.Location.Y+25+x);
                x += 25;
                this.Controls.Add(newButton);   

                TextBox newTextBox = new TextBox();
                textboxes.Add(newTextBox);
               // newTextBox.Click += click;

                newTextBox.Location = new Point(textBox1.Location.X, textBox1.Location.Y+25+y);
                y += 25;
                this.Controls.Add(newTextBox);

        }

you can have a class like mybutton that inherits from button class, in this new class you can have a property with textbox type . 您可以拥有一个类似mybutton的类,该类继承自button类,在这个新类中,您可以拥有一个textbox type属性。 just like following code . 就像下面的代码。 and in your code when you want to Instantiated Button you can use list<mybutton> and set linkedTextbox property with a textbox. 在代码中,当您要实例化Button时,可以使用list<mybutton>并为文本框设置linkedTextbox属性。

public class myButton:Button
{
   ...
   public TextBox linkedTextBox{set;get;}
}

and in your code you should write some thing like this : 在您的代码中,您应该编写如下内容:

list<myButton> buttons=new list<myButton>();
Textbox someTextBox=new TextBox();
buttons[0].linkedTextbox=someTextBox;

and in your event you can use: 并且在您可以使用的情况下,您可以使用:

((myButton)sender).linkedTextBox.text="Some thing";

Thank you everyone , I followed @Franck's answer .So WHAT CHANGED : 谢谢大家, 我按照@Franck的回答进行了 更改

I've deleted the pre-made button1 & textbox1 and add them programatically on the Form_load so that I can add them in the Lists 我已经删除了预制的button1textbox1并以编程方式将它们添加到Form_load以便可以将它们添加到Lists

A proof screenshot : http://prntscr.com/aprqxz 证明屏幕截图: http : //prntscr.com/aprqxz

CODE: 码:

        List<Button> buttons = new List<Button>();
        List<TextBox> textboxes = new List<TextBox>();

        Button button1 = new Button();
        TextBox textBox1 = new TextBox();

        int x = 0;
        int y = 0;

        void click(object sender, EventArgs e)
        {


            var txt =  textboxes[Convert.ToInt32(((Button)sender).Tag)].Text;
            MessageBox.Show(txt.ToString());

        }

        void AddClick(object sender, EventArgs e)
        {

                Button newButton = new Button();
                newButton.Click += click;
                newButton.Location = new Point(button1.Location.X, button1.Location.Y+25+x);
                x += 25;
                newButton.Tag = buttons.Count;

                this.Controls.Add(newButton);

                buttons.Add(newButton);             
                //
                TextBox newTextBox = new TextBox();
                newTextBox.Location = new Point(textBox1.Location.X, textBox1.Location.Y+25+y);
                y += 25;
                this.Controls.Add(newTextBox);

                textboxes.Add(newTextBox);

        }
        void MainFormLoad(object sender, EventArgs e)
        {
                button1.Click += click; 
                button1.Location = new Point(55, 48);

                button1.Tag = buttons.Count;

                this.Controls.Add(button1);

                buttons.Add(button1);               
                //
                textBox1.Location = new Point(137, 50);

                this.Controls.Add(textBox1);

                textboxes.Add(textBox1);
        }

EDIT 1: As the counting starts from 0 I didn't added newButton.Tag = buttons.count+1; 编辑1:由于计数从0开始,所以我没有添加newButton.Tag = buttons.count+1; I added just newButton.Tag = buttons.count; 我只添加了newButton.Tag = buttons.count;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 Click 事件添加到 WPF NotifyIcon 中的上下文菜单按钮? - How to add to Click event to Context Menu buttons in WPF NotifyIcon? 如何从C#的UI中的模板中添加多个新按钮,以及如何为特定按钮单击事件 - How to add multiple new buttons from template in a UI from C# and call on click event for a particular buttons 如何使用按钮创建自定义控件以及如何在 silverlight 中为 windows mobile 7 添加事件按钮单击事件 - How to create custom control with buttons and how to add event button click event in silverlight for windows mobile 7 如何动态地将 Click 事件附加到多个按钮? - How to attach Click event dynamically to multiple buttons? 将右键单击事件添加到c#中动态创建的按钮中 - Add a Right-Click Event to dynamic created Buttons in c# 如何处理UserControl-Windows 8应用程序中的按钮的Click事件? - How to handle Click event for Buttons in UserControl - Windows 8 app? 如何在generic.xaml上为多个按钮引用click_event? - How to reference a click_event for multiple buttons on a generic.xaml? 如何在mvc中保持单击事件动态创建按钮 - How to keep click event dynamically created buttons in mvc 如何使用一键事件使按钮在标签中显示其文本 - How to make buttons display their text in a label using one click event 如何禁用单击事件处理程序中除一个之外的所有按钮? - How to disable all buttons except one in the click event handler?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM