Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the problem is how can i use txtcki text in btncki_click in below code:

C#
public Form2()
        {
            int xllngth,loc=20;
             for (; ; xllngth++) 
             {                      
                    var buncoockie = new System.Windows.Forms.Button();
                    buncoockie.Name = "btncki" + xllngth;
                    buncoockie.Text ="cki"+ Convert.ToString(sheet1.Cells[xllngth, 2].value);
                    buncoockie.Size = new Size(100, 20);
                    buncoockie.Location = new System.Drawing.Point(145, loc);
                    buncoockie.BackColor = Color.AliceBlue;
                    buncoockie.Click += Btncki_Click;                                   
                    this.Controls.Add(buncoockie);
										
                   var txtcki=new System.Windows.Forms.TextBox();
                   txtcki.Name = "txtcki" + xllngth;
                   txtcki.Size = new Size(150, 20);
                   txtcki.Location = new System.Drawing.Point(245, loc);
                   this.Controls.Add(txtcki);                                      
                   loc += 20;
             }              
        }

        private void Btncki_Click(object sender, EventArgs e)
        {
           //string temp=txtcki.text;       
        }
Posted

1 solution

First, use the Button.Tag property to tie them together:
C#
var buncoockie = new System.Windows.Forms.Button();
buncoockie.Name = "btncki" + xllngth;
buncoockie.Text = "cki" + Convert.ToString(sheet1.Cells[xllngth, 2].value);
buncoockie.Size = new Size(100, 20);
buncoockie.Location = new System.Drawing.Point(145, loc);
buncoockie.BackColor = Color.AliceBlue;
buncoockie.Click += Btncki_Click;

var txtcki = new System.Windows.Forms.TextBox();
txtcki.Name = "txtcki" + xllngth;
txtcki.Size = new Size(150, 20);
txtcki.Location = new System.Drawing.Point(245, loc);

buncoockie.Tag = txtcki;
this.Controls.Add(buncoockie);
this.Controls.Add(txtcki);
loc += 20;

Then, in the event handler:
C#
private void Btncki_Click(object sender, EventArgs e)
    {
    Button buncoockie = sender as Button;
    if (buncoockie != null)
        {
        TextBox txtcki = buncoockie.Tag as TextBox;
        if (txtcki != null)
            {
            string temp = txtcki.text;
            ...
            }
        }
    }
 
Share this answer
 
Comments
mit62 20-May-13 0:13am    
thanks a lot
OriginalGriff 20-May-13 2:25am    
You're welcome!

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