Viva
Viva

Reputation: 2075

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. Also,on winform I have a simple textbox . 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. 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++;
    }

Upvotes: 1

Views: 3475

Answers (4)

Dilshod
Dilshod

Reputation: 3331

you need to have a Selected event on your UserControl.

    //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. 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.

Upvotes: 1

Arie
Arie

Reputation: 5373

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.
  • Every UserControl has a TextBox named ["txtBox" + i]
  • on select you want to synchronize texts of txtSimple and TextBox of selected UserControl

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.

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). You can simply name it "txtBox", then access it like this:

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

Control names are unique in a Controls collection of a Control, UserControl and Form.

Upvotes: 1

Tony Hopkinson
Tony Hopkinson

Reputation: 20330

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 is an extremely bad name for it..

I'm not even going to mention the assumption that all controls have a name starting with txtBox and that no other controls have...

And the entire thing dies if more than one control is selected when it runs.

Upvotes: 1

General-Doomer
General-Doomer

Reputation: 2761

Control[] txt = ...
txtSimple.Text= txtBox[0].Text ;

May be replace txtBox[0].Text to txt[0].Text ?

Upvotes: 1

Related Questions