Click here to Skip to main content
12,633,990 members (22,225 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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 4-Jan-13 8:50am
sariqkhan1.4K
Updated 4-Jan-13 19: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
Krunal Rohit 6-Jan-13 8:54am
   
What error you are getting ?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Take an example of a user control like this. Below user control has 1 combobox and 1 textbox.
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.
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:
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:
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:
private void GetValue()
{
    foreach (UserControl1 c in lst)
    {
        MessageBox.Show(c.TextBox1Text);
        MessageBox.Show(c.ComboBox1Text);
    }
}
  Permalink  
v4
Comments
sariqkhan 5-Jan-13 0:48am
   
+5 sir thank you
BillWoodruff 6-Jan-13 22:06pm
   
good answer +5
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161208.2 | Last Updated 5 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100