简体   繁体   English

获取所选动态文本框的文本c#

[英]get text of a selected dynamically textbox c#

I have on my winform an usercontrol and I create multiple usercontrols at every button click(at runtime).My usercontrol has an textbox . 我在winform上有一个usercontrol ,我在每次单击按钮时都会创建多个usercontrols(在运行时)。我的usercontrol有一个textbox Also,on winform I have a simple textbox . 另外,在winform上我有一个简单的文本框。 I want ,when I select an usercontrol,the text from the dynamical textbox to appear also in the simple textbox. 我想,当我选择一个用户控件时,动态文本框中的文本也出现在简单的文本框中。 In my code it says that the textbox from usercontrol is not in the current context. 在我的代码中,它表示来自usercontrol的文本框不在当前上下文中。 My code: 我的代码:

private void Gettext()
{
    int i = 0;
    Control[] txt = Controls.Find("txtBox" + i.ToString(), true);//here I search for the dynamical textbox
    foreach (Control c in panel1.Controls)
    {
        if (c is UserControl1) 
        {
            if (((UserControl)c).Selected)
                 txtSimple.Text= txtBox[0].Text ;
        }
        i++;
    }
Control[] txt = ...
txtSimple.Text= txtBox[0].Text ;

May be replace txtBox[0].Text to txt[0].Text ? 可以将txtBox [0] .Text替换为txt [0] .Text?

Well for a start 好一开始

Control[] txt = Panel1.Controls.Find("txtBox" + i.ToString(), true)

Then 然后

foreach (Control c in txt) // txt???
{
    UserControl1 uc = c as UserControl1;
    if (uc != null) 
    {
        if (uc.Selected) txtSimple.Text= uc.Text ;
    }
}

Then if if you are are testing for UserControl1, you should also cast to UserControl1 not UserControl 然后,如果您正在测试UserControl1,您还应该转换为UserControl1而不是UserControl

UserControl1 is an extremely bad name for it.. UserControl1是一个非常糟糕的名字..

I'm not even going to mention the assumption that all controls have a name starting with txtBox and that no other controls have... 我甚至不会提到所有控件都有一个以txtBox开头的名称并且没有其他控件具有的假设......

And the entire thing dies if more than one control is selected when it runs. 如果在运行时选择了多个控件,则整个过程会死亡。

I don't know if I understood your question correctly: 我不知道我是否理解你的问题:

The structure of your form looks something like this: 表单的结构如下所示:

  • Your form has a Panel panel1 that has many UserControls of the type UserControl1, created on runtime, and one TextBox txtSimple. 您的表单有一个Panel panel1,它有许多UserControl1类型的UserControl,在运行时创建,还有一个TextBox txtSimple。
  • Every UserControl has a TextBox named ["txtBox" + i] 每个UserControl都有一个名为[“txtBox”+ i]的TextBox
  • on select you want to synchronize texts of txtSimple and TextBox of selected UserControl 在选择您要同步所选UserControl的txtSimple和TextBox的文本

Then: 然后:

int i=0;
foreach (Control c in panel1.Controls)
{
    if (c is UserControl1) 
    {
        if (((UserControl)c).Selected)
        {
             TextBox dynTxtBox = (TextBox)c.Controls["txtBox" + i];
             txtSimple.Text= dynTxtBoxe.Text;
        }
    }
    i++;
}

If you can't find your TextBox this way, it probably means that its name is not set correctly. 如果您无法以这种方式找到TextBox,则可能意味着其名称未正确设置。

Also, if you have only one TextBox on your UserControl then there's normally no need to name it in such a specific way (I mean from your code I assumed you have txtBox0 on your first user control, txtBox1 on your second and so on). 此外,如果你的UserControl上只有一个TextBox,那么通常不需要以这种特定的方式命名它(我的意思是你的代码我假设你的第一个用户控件上有txtBox0,第二个用txtBox1等等)。 You can simply name it "txtBox", then access it like this: 您可以简单地将其命名为“txtBox”,然后像这样访问它:

txtSimple.Text = selectedUserControl.Controls["txtBox"].Text;

Control names are unique in a Controls collection of a Control, UserControl and Form. 控件名称在Control,UserControl和Form的Controls集合中是唯一的。

you need to have a Selected event on your UserControl . 你需要在UserControl上有一个Selected事件。

    //in UserControl
    public event EventHandler Selected;

    private void textBox1_MouseClick(object sender, MouseEventArgs e)
    {
        if(Selected!=null)
            Selected(this,null);
    }

now subscribe to Selected event of UserControl when you dynamically create it. 现在,当您动态创建UserControl时,它会订阅SelectedControl的Selected事件。 Like this: 像这样:

    UserControl control = new UserControl();
    control.Selected += myControl_Selected;


    private void myControl_Selected(object sender, EventArgs e)
    {
        UserControl control = (UserControl)sender;
        textBox2.Text = control.Text;
    }

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM