Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
i have a windows form in which there are controls listed below
**panel1
button1
button2**
clicking on the button1 usercontrol is added on it, usercontrol can be added as much as the button is clicked means if the button1 is clicked one times then one user contorl will be added and if usercontrol is clicked twice then 2 usercontrol is added
usercontrol contains 4 controls
combobox1
combobox2
textbox1
textbox2
i want to add the text of textbox2 of all the dynamically generated usercontrol control and set to a label on the winform how can i do that?
i have tried this code
foreach (Control ctrl in panel1.Controls)
                {
                    if (ctrl is UserControl)
                    {
                        UserControl1 myCrl = ctrl as UserControl1;
                        int g;
                        g += Convert.ToInt32(myCrl.textbox2.Text);  // but 
                 this doesnt happen an error is coming
                    }
                 }

how to get sum of all the dynamically generated control's text!
Posted
Updated 4-Jan-13 18:47pm
v2
Comments
BillWoodruff 4-Jan-13 16:25pm    
Hi,

The obvious thing that "jumps out," looking at your code, is that you are never adding Items to the ComboBoxes in the code you show: you are only modifying their 'Text property.

I think to get a helpful response you need to re-write your question: right now I have difficulty figuring out exactly which Controls you are speaking of.

Suggestions: for each Control listed: describe what it is for, and how it is supposed to be used:

1. List all the Controls on the Form.

2. List all the Controls in the UserControl.

3. Describe exactly what happens if the Button, on the Form, that creates the UserControl that is then inserted into the Panel's ControlCollection: is clicked once ?; is clicked twice?; is clicked more than twice ?

4. You show only the Text property of the ComboBoxes being modified: where are the Items in the ComboBoxes coming from: if there are no Items: then why are using a ComboBox ?

What is your main goal in this application ?

good luck, Bill
sariqkhan 5-Jan-13 1:07am    
can you solve this now... i have update that
BillWoodruff 6-Jan-13 22:13pm    
Hi Sariq Khan, I think Christian Gauss and Adriancs have shown you what you need to focus on, so I won't reply further, but if you have more questions, just ask.

What happens if you create three of your UserControls ... and I assume you'll do the right thing so they are all visible on the screen, or accessible in some way (like through a menu selection) ... and then: the user of your application enters/edits/deletes the text in the second TextBox on one of your UserControls ? Are you then going to want to change what appears on your main Form in the Label (I think you probably want to use a TextBox, not a Label ... or even better some of Control where you can see clearly which text came from which UserControl.

yrs, Bill
[no name] 6-Jan-13 8:54am    
What error you are getting ?

Take an example of a user control like this. Below user control has 1 combobox and 1 textbox.
C#
public partial class UserControl1 : UserControl
{
    ComboBox comboBox1 = new ComboBox();
    TextBox textBox1 = new TextBox();

    public string ComboBox1Text 
    { 
        get
        { 
            return comboBox1.Text;
        } 
        set
        {
            comboBox1.Text = value;
        } 
    }
    public string TextBox1Text 
    { 
        get
        { 
            return textBox1.Text;
        }
        set
        { 
            textBox1.Text = value;
        } 
    }

    public UserControl1()
    {
        InitializeComponent();
        this.Controls.Add(comboBox1);
        this.Controls.Add(textBox1);
    }
}

In your WinForm, you can declare a List to keep track of the dynamic added User Control.
C#
public partial class Form1 : Form
{
    List<UserControl1> lst = new List<UserControl1>();

    public Form1()
    {
        InitializeComponent();
    }
}

By the time you add the user control, you add it to the List too. Example:
C#
private void DynamicAddUserControl()
{
    UserControl1 uc = new UserControl1();
    panel1.Controls.Add(uc);
    lst.Add(uc);
}

When you want to access value, search it within the List.

If you assume that you will only have 1 user control added dynamically, you can access it like this:
C#
private void GetValueFromDynamicAddedUserControl()
{
    UserControl1 uc = lst[0];
    MessageBox.Show(uc.TextBox1Text);
    MessageBox.Show(uc.ComboBox1Text);
}

If there is unpredictable number of added user control, you can loop through the List. Example:
C#
private void GetValue()
{
    foreach (UserControl1 c in lst)
    {
        MessageBox.Show(c.TextBox1Text);
        MessageBox.Show(c.ComboBox1Text);
    }
}
 
Share this answer
 
v4
Comments
sariqkhan 5-Jan-13 0:48am    
+5 sir thank you
BillWoodruff 6-Jan-13 22:06pm    
good answer +5
There's either a lot of people trying to do the same thing, or you've asked this many times across many accounts.

First of all, it's bad form ALWAYS to access controls in another form. You need to write a method to expose the bit you want, so the client of the class can get the text, but not set the text, or access it both ways but not the other properties of the control. That you have dynamic controls makes this doubly true, they are dynamic so they can't have a property, but you can have a property or a method that searches for those controls and builds the correct value to return
 
Share this answer
 
Comments
BillWoodruff 4-Jan-13 18:22pm    
Happy New Year, Christian, While the description of what's going on here is unclear, and I've left some detailed feedback on how, hopefully, the OP might clarify what they are asking, I see no evidence that the OP is dealing here with a cross-form access question. Let's give him a chance to clarify.

yrs, Bill
Christian Graus 4-Jan-13 18:36pm    
True - but the same rule applies when writing a user control, in my mind at least
BillWoodruff 4-Jan-13 19:40pm    
Yes, you are right !

And, the fact the OP is creating the new UserControls in the scope of an EventHandler, and not keeping a reference to them in some way, is something they will have to deal with ultimately.

I had hoped to get the OP to clarify the question further before going into how one would expose the data inside the UserControls, so they could be accessed in the Form in which they are hosted.

Of course UserControls, like Forms, can have expose data through Public Properties; or, you can "inject" references to the Form into the UserControls: my personal preference is to always have Forms or UserControls expose only exactly what needs to be used elsewhere via Public Properties.

Your "cutting to the chase" here, may be exactly the right stragegy ! I may have been a Social Worker, and teacher, too long :)

yrs, Bill
Christian Graus 4-Jan-13 19:42pm    
He's asked, and I've answered, several times. Answering here feels like social work, sometimes
BillWoodruff 4-Jan-13 20:04pm    
Hi, I wasn't aware of that prior history, sorry. I've just picked up being active on Q&A again after a while. best, Bill

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