如何将文本从动态生成的用户控制转移到一个文本框 [英] how to transfer the text from dynamically generated user control to a textbox

查看:164
本文介绍了如何将文本从动态生成的用户控制转移到一个文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个窗口的形式在其中的按钮1 当点击一个用户控件动态添加了code是这个

  INT C = 0;
         私人无效的button1_Click(对象发件人,EventArgs的发送)
    {
        INT伏;
        V = C ++;
        panel1.VerticalScroll.Value = VerticalScroll.Minimum;
        我们的UserControl1 =新的UserControl1();
        us.Name =US+ V;
        us.Location =新点(50,5 +(30 * V));
        us.Tag = BTN;
        panel1.Controls.Add(美国);
     }

这是用户控件包含4控制2组合框和文本框2

combobox1 combobox2 TextBox1的TextBox2中

有4文本框可相同形式是

仍然TextBox1中,仍然TextBox2中,仍然textbox3,仍然textbox4

一个更多的按钮2 是那里将文本传送到组合框和texboxes即 oldcombobox1 oldcombobox2 oldtextbox1老TextBox2中

在Button1的是两次单击它会在形式添加两个用户控件
我想文本这种格式传输

oldcombobox1.text =静止textbox1.text +,+ combobox1.text(这是动态生成的)+,+ combobox1.text(这是动态生成的)等等从用户控制所有combobox1这是动态添加)

oldcombobox2.text =静止textbox2.text +,+ combobox2.text(这是动态生成的)+,+ combobox2.text(这是动态生成的)等等从用户控制所有combobox2这是动态添加)

oldtextbox1.text =静止textbox3 +,+ textboox1.text(这是动态生成的)+,+ textbox1.text(这是动态生成的)等等从中动态添加用户控制的所有TextBox1的)

意味着当仍然textbox1.text =第一
当动态用户控件添加三次它将包含3次combobox1然后

在oldcombobox1应该包含

最前一页,combobox1.text,combobox1.text,combobox1.text

我有此code,但它不工作

 的foreach(在panel1.Controls控制CTRL)
   {
     如果(CTRL为用户控件)
     {
         的UserControl1 myCrl = Ctrl键的UserControl1;
         oldcombobox1.text =静止textbox1.text +,+ myCrl.comboBox1.Text;
         oldcombobox2.Text =静止textbox2.text +,+ myCrl.comboBox2.Text;
         oldtextbox1.Text =静止textbox3.text +,+ myCrl.textBox1.Text;
         oldtextbox2.Text.Text =静止textbox4.text +,+ myCrl.textBox2.Text;
      }
    }


解决方案

您应该添加到您的类的UserControl1 (伟大的名字顺便说一句;-))这样的事情每串要从另一个对象来访问,在这种情况下的串 textBox1的

 公共字符串FirstTextBoxText
{
   {返回this.textBox1.Text; }
}

然后你就可以在你的Form类说:

 如果(CTRL为用户控件)
 {
     的UserControl1 myCrl = Ctrl键的UserControl1;
     // ...
     oldtextbox1.Text =静止textbox3.text +,+ myCrl.FirstTextBoxText;
 }

它仍然可怕code,但它会奏效。

i have a windows form in which a button1 when that is clicked a usercontrol is added dynamically the code is this

            int c = 0;
         private void button1_Click(object sender, EventArgs e)
    {
        int v;
        v = c++;
        panel1.VerticalScroll.Value = VerticalScroll.Minimum;
        UserControl1 us = new UserControl1();
        us.Name = "us" + v;
        us.Location = new Point(50, 5 + (30 * v));
        us.Tag = btn;
        panel1.Controls.Add(us);
     }

that usercontrol contains 4 controls 2 combobox and 2 textbox

i.e combobox1 combobox2 textbox1 textbox2

there are 4 textbox which are on the same form which is

still-textbox1 , still-textbox2 , still-textbox3 , still-textbox4

one more button2 is there it will transfer the text to comboboxes and texboxes which is oldcombobox1 oldcombobox2 oldtextbox1 old textbox2

when button1 is click twice it will add two usercontrol in the form i want to transfer the text in this format

oldcombobox1.text = still-textbox1.text + "," + combobox1.text (which is dynamically generated) + "," + combobox1.text (which is dynamically generated) etc all the combobox1 from the user control which is added dynamically )

oldcombobox2.text = still-textbox2.text + "," + combobox2.text (which is dynamically generated) + "," + combobox2.text (which is dynamically generated) etc all the combobox2 from the user control which is added dynamically )

oldtextbox1.text = still-textbox3 + "," + textboox1.text (which is dynamically generated) + "," + textbox1.text (which is dynamically generated) etc all the textbox1 from the user control which is added dynamically )

means when the still-textbox1.text = first and when dynamic usercontrol is added thrice it will contain 3 times combobox1 then

the oldcombobox1 should contain

fisrt,combobox1.text,combobox1.text,combobox1.text

i have made this code but it doesnt work

  foreach (Control ctrl in panel1.Controls)
   {
     if (ctrl is UserControl)
     {
         UserControl1 myCrl = ctrl as UserControl1;
         oldcombobox1.text = still-textbox1.text + "," + myCrl.comboBox1.Text;
         oldcombobox2.Text =still-textbox2.text + "," + myCrl.comboBox2.Text;
         oldtextbox1.Text = still-textbox3.text + "," + myCrl.textBox1.Text;
         oldtextbox2.Text.Text = still-textbox4.text + "," + myCrl.textBox2.Text;
      }
    }

解决方案

You should add to your class UserControl1 (great name btw ;-) ) something like this for every string you want to access from another object, in this case the string of textBox1:

public String FirstTextBoxText 
{
   get { return this.textBox1.Text; }
}

Then you can say in your Form class:

 if (ctrl is UserControl)
 {
     UserControl1 myCrl = ctrl as UserControl1;
     // ...
     oldtextbox1.Text = still-textbox3.text + "," + myCrl.FirstTextBoxText;
 }

It's still horrible code, but it will work.

这篇关于如何将文本从动态生成的用户控制转移到一个文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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