Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi.i made some textbox programmatically:

C#
public Form2()
{
  .
  .
   for (int j = 0; uni[j] != "end"; j++) //number of fill row till "end"
   {
                list.Add(new System.Windows.Forms.TextBox());
                list[j].Name = "ckifile" + j;
                list[j].Size = new Size(50, 20);
                list[j].Location = new System.Drawing.Point(400, loc);
                list[j].TabIndex = 2;
                list[j].KeyDown += new KeyEventHandler(txtfilenam_keydown);
                this.Controls.Add(list[j]);
   }
}

now i want when button1 is clicked,data in clipborad be paste to Textboxes.
C#
private void button1_Click(object sender, EventArgs e)
       {
           int k=0;
           Form2 frm2 = new Form2();
           string s = Clipboard.GetText();
           string[] lines = s.Split('\n');
           foreach (string line in lines)
           {
               if (k < list.Count)
               {
                  //this part is wrong
                   frm2.list[k].Text = line;???????????????
                   frm2.Controls.Add(list[k++]);?????????????
               }
               else
                   break;
           }
       }

how can i do that?
thanks
Posted
Updated 1-Jun-13 21:33pm
v3

1 solution

It depends on what type of List you have created whether it compiles or not, but if I assume it does then you can't do it like that.
The problem is that you are creating a new instance of a Form2 in your click handler:
C#
Form2 frm2 = new Form2();
And setting the value of that - this is not the same as the form your are displaying in the same way that if you put your mobile in your car glove box, then buy a new car, you would not expect to find you mobile in it's glove box!

Assuming that the button is a part of Form2, then you do not need to specify any form when accessing the list:
C#
private void button1_Click(object sender, EventArgs e)
       {
           int k=0;
           string s = Clipboard.GetText();
           string[] lines = s.Split('\n');
           foreach (string line in lines)
           {
               if (k < list.Count)
               {
                   list[k++].Text = line;
               }
               else
                   break;
           }
       }
If it isn't, then you want to keep a reference to the instance of Form2 you have already created and shown and use that - but preferably, but passing the new text to a Property or method within the form instance and letting it do the work.
 
Share this answer
 
Comments
mit62 2-Jun-13 4:01am    
it works.thanks a lot.

private void button1_Click(object sender, EventArgs e)
{
int k=0;
string s = Clipboard.GetText();
string[] lines = s.Split('\n');
foreach (string line in lines)
{
if (k < list.Count)
{
list[k++].Text = line;
}
else
break;
}
}
OriginalGriff 2-Jun-13 4:02am    
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