Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Accessing dynamically created controls on windows form

Posted on 2009-07-09
1
Medium Priority
?
610 Views
Last Modified: 2013-12-17
Hi all,

I have a .net TabControl - the tabs of which I am dynamically creating.  Each tab has a form on it consisting of labels, textboxes, buttons and a datagrid.  I am able to create the tabs just fine, however once a tab is created how do I access the dynamically created controls on it (i.e. the labels, textboxes, buttons and datagrid)?

A brief section of my code is shown in the code snippet below:

How would I get the text value of pnTextBox on a particular tab?  How would I catch an event raised from a textchanged or button click even from contols on a particular tab?
Do I need to dynamically create event handler methods too?

Thanks for the help.
private void GeneratePartTabs(Int32 rmaID)
{
    RMA2MainPartList.Load(rmaID);
 
    foreach (RMA2MainPart mainPart in RMA2MainPartList.List)
    {
        TabPage tabPage = new TabPage(mainPart.PartNumber);
        tabPage = CreateTabForm(tabPage, mainPart);
        tabControl1.TabPages.Add(tabPage);
    }
}
 
private TabPage CreateTabForm(TabPage Tab, RMA2MainPart MainPart)
{
    int num = tabControl1.TabCount;
                                                     
    Label pnLabel = new Label();                    
    pnLabel.Name = "lbPartNumber" + num;     
    pnLabel.Text = "Part Number";                   
    pnLabel.Location = new Point(8, 6);          
    pnLabel.Size = new Size(75, 13);             
 
    TextBox pnTextBox = new TextBox();
    pnTextBox.Name = "tbPartNum" + num;
    pnTextBox.Location = new Point(8, 22);
    pnTextBox.Size = new Size(130, 20);
    pnTextBox.Text = MainPart.PartNumber;
 
    //Etc. etc. - multiple labels and textboxes added here.........
 
    Tab.Controls.Add(pnLabel);
    Tab.Controls.Add(pnTextBox);
 
    return Tab;
}

Open in new window

0
Comment
Question by:P1ST0LPETE
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 13

Accepted Solution

by:
drypz earned 1500 total points
ID: 24819897
Hi! You may access controls like this

            foreach (Control ctrl in tabControl1.Controls)
            {
                if (ctrl is TextBox)
                {
                    MessageBox.Show(((TextBox)ctrl).Name);
                    //your code here
                }

                if (ctrl is Label)
                {
                    MessageBox.Show(((Label)ctrl).Name);
                    //your code here
                }
            }

You may also dynamically add an event of your control, something like

            int num = tabControl1.TabCount;

            TextBox pnTextBox = new TextBox();
            pnTextBox.Name = "tbPartNum" + num;
            pnTextBox.Location = new Point(8, 22);
            pnTextBox.Size = new Size(130, 20);
            pnTextBox.Text = MainPart.PartNumber;
            pnTextBox.TextChanged += new EventHandler(pnTextBox_TextChanged);

        private void pnTextBox_TextChanged(object sender, EventArgs e)
        {
            throw new Exception("The method or operation is not implemented.");
        }
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question