如何更改textbox'texts是以编程方式 [英] how to change textbox'texts are made programmatically

查看:72
本文介绍了如何更改textbox'texts是以编程方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi.i以编程方式制作了一些文本框:



  public  Form2()
{


for int j = 0 ; uni [j]!= end; j ++) // 填充行数到结束
{
list.Add( new System.Windows.Forms.TextBox());
list [j] .Name = ckifile + j;
list [j] .Size = new 大小( 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]);
}
}



现在我想要点击button1,clipborad中的数据会粘贴到Textboxes。

< pre lang =c#> private void button1_Click( object sender,EventArgs e)
{
int k = 0 ;
Form2 frm2 = new Form2();
string s = Clipboard.GetText();
string [] lines = s.Split(' \\\
');
foreach string line in 行)
{
if (k < list.Count)
{
// 此部分错误
frm2.list [ k] .Text = line; ??????????????
frm2.Controls.Add(list [k ++]); ?????????????
}
else
break ;
}
}



我该怎么做?

谢谢

解决方案

这取决于你创建的列表类型是否编译,但是如果我认为它确实存在那么你就不能这样做。

问题是你在点击处理程序中创建了一个Form2的新实例:

 Form2 frm2 =  new  Form2(); 

并设置其值 - 这与您显示的表格不同,如果您将手机放入汽车手套箱,然后购买新车,你不会期望在它的手套箱中找到你的手机!



假设按钮是Form2的一部分,那么你不需要指定访问列表时的任何形式:

  private   void  button1_Click( object  sender,EventArgs e)
{
int k = 0 ;
string s = Clipboard.GetText();
string [] lines = s.Split(' \\\
');
foreach string line in 行)
{
if (k < list.Count)
{
list [k ++]。Text = line;
}
else
break ;
}
}

如果它不是,那么你想保留对你已经创建并显示的Form2实例的引用并使用它 - 但最好是,但是将新文本传递给表单实例中的属性或方法,并让它完成工作。


hi.i made some textbox programmatically:

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.

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

解决方案

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:

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:

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.


这篇关于如何更改textbox'texts是以编程方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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