簡體   English   中英

如何添加具有獨立單擊事件的按鈕

[英]How to add buttons with independent click event

我想在運行時動態添加按鈕和文本框,每個按鈕的反應都不同。

newbutton1newbutton1鏈接texbox1 , newbutton2 linked with textbox2 linked with

現在,任何按鈕都只會從第一個到最后一個文本框一個接一個地打印。

還請考慮我已經在指南的表單上有一個button1和textbox1

這是我的代碼:

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

        }

您可以擁有一個類似mybutton的類,該類繼承自button類,在這個新類中,您可以擁有一個textbox type屬性。 就像下面的代碼。 在代碼中,當您要實例化Button時,可以使用list<mybutton>並為文本框設置linkedTextbox屬性。

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

在您的代碼中,您應該編寫如下內容:

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

並且在您可以使用的情況下,您可以使用:

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

謝謝大家, 我按照@Franck的回答進行了 更改

我已經刪除了預制的button1textbox1並以編程方式將它們添加到Form_load以便可以將它們添加到Lists

證明屏幕截圖: http : //prntscr.com/aprqxz

碼:

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

編輯1:由於計數從0開始,所以我沒有添加newButton.Tag = buttons.count+1; 我只添加了newButton.Tag = buttons.count;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM