Windows Develop Bookmark and Share   
 index > Windows Forms General > How to display controls on the fly
 

How to display controls on the fly

On my user control I have a list companies in a datagridview. When a company is selected I currently get the company information. Most of the information I can pull directly from my Company object and set the label value like so. lblWebsiteValue.Text = company.CompanyWebsite.

My issue now is how to handle the company phone numbers? Right now in my Company object I have a companyPhone entry that is of type DataRowCollection. I did this because a company will most likely have at least one business number, and one fax number, possible more.

Each phone row will have the following columns: phoneType, phoneNumber, ext. the phoneType and phoneNumber cannot be null.

I would want the in formation to display like this How do I do this?

Business: 123.123.1234 Ext 103

Fax: 123.123.1235

Here is my method that builds the company details. Currently only the first line displays

        /// <summary>
        /// Builds and displays the currently selected company details.
        /// </summary>
        private void buildCompanyDetails()
        {
            Company company = Company.getCompany((Int16)dataGridView1.Rows[((CurrencyManager)BindingContext[dataGridView1.DataSource]).Position].Cells[0].Value, out company);

            lblCompanyNameValue.Text = company.CompanyName;
            lblIndustryTypeValue.Text = company.CompanyIndustry;
            lblWebsiteValue.Text = company.CompanyWebsite;
            lblStatusValue.Text = company.CompanyStatus;

            pnlPhone.Controls.Clear();
            Label newItem = new Label();

            foreach (DataRow phoneDataRow in company.CompanyPhone)
            {                
                string[] phoneDetails = new string[phoneDataRow.ItemArray.Count()];
                int columnIndex = 0;               
                
                newItem = new Label();
                newItem.Width = 200;
                StringBuilder stringBuilder = new StringBuilder();

                foreach (object column in phoneDataRow.ItemArray)
                {
                    phoneDetails[columnIndex] = Convert.ToString(column);                    
                    if(phoneDetails[columnIndex] != null && phoneDetails[columnIndex].Length > 0)
                    {
                        stringBuilder.Append(phoneDetails[columnIndex]);
                        stringBuilder.Append(" ");                   
                    }
                    columnIndex++;
                }
                newItem.Text = stringBuilder.ToString();               
                pnlPhone.Controls.Add(newItem);
            }
        }

AJ720  Monday, June 15, 2009 12:56 AM
The issue that I suspect is:
You are adding the controls(Labels)but you are not setting the Location for them.
As such they are being added one, on top of other.

Please set the location specifically for the Labels, something like:

        private void buildCompanyDetails()
        {
            Company company = Company.getCompany((Int16)dataGridView1.Rows[((CurrencyManager)BindingContext[dataGridView1.DataSource]).Position].Cells[0].Value, out company);

            lblCompanyNameValue.Text = company.CompanyName;
            lblIndustryTypeValue.Text = company.CompanyIndustry;
            lblWebsiteValue.Text = company.CompanyWebsite;
            lblStatusValue.Text = company.CompanyStatus;

            pnlPhone.Controls.Clear();
            //Label newItem = new Label();    //Remove these code from here.
            //newItem.Width = 200;
            //newItem.Height = 150;
            StringBuilder stringBuilder = new StringBuilder();
            Point locatiioOfFirstLabel = new Point(100, 100);   //New Variable

            foreach (DataRow phoneDataRow in company.CompanyPhone)
            {
                string[] phoneDetails = new string[phoneDataRow.ItemArray.Count()];
                int columnIndex = 0;

                Label newItem = new Label();    //Place the above code here.
                newItem.Width = 200;
                newItem.Height = 150;
                newItem.Location = new Point(locatiioOfFirstLabel.X, locatiioOfFirstLabel.Y + 20);

                foreach (object column in phoneDataRow.ItemArray)
                {
                    phoneDetails[columnIndex] = Convert.ToString(column);
                    if (phoneDetails[columnIndex] != null && phoneDetails[columnIndex].Length > 0)
                    {
                        stringBuilder.Append(phoneDetails[columnIndex]);
                        stringBuilder.Append(" ");
                    }
                    columnIndex++;
                }
                stringBuilder.Append("\n");
                newItem.Text = stringBuilder.ToString();
                //pnlPhone.Controls.Add(newItem);
            }
            pnlPhone.Controls.Add(newItem);
        }

Regards, Lakra :) - If the post is helpful or answers your question, please mark it as such.
  • Marked As Answer byAJ720 Monday, June 15, 2009 1:57 PM
  •  
Abhijeet Lakra  Monday, June 15, 2009 2:44 AM

Well, from my understanding-

Try the below code:

        private void buildCompanyDetails()
        {
            Company company = Company.getCompany((Int16)dataGridView1.Rows[((CurrencyManager)BindingContext[dataGridView1.DataSource]).Position].Cells[0].Value, out company);

            lblCompanyNameValue.Text = company.CompanyName;
            lblIndustryTypeValue.Text = company.CompanyIndustry;
            lblWebsiteValue.Text = company.CompanyWebsite;
            lblStatusValue.Text = company.CompanyStatus;

            pnlPhone.Controls.Clear();
            Label newItem = new Label();

            StringBuilder stringBuilder = new StringBuilder();   //New placement of the code.

            foreach (DataRow phoneDataRow in company.CompanyPhone)
            {
                string[] phoneDetails = new string[phoneDataRow.ItemArray.Count()];
                int columnIndex = 0;

                newItem = new Label();
                newItem.Width = 200;
                //StringBuilder stringBuilder = new StringBuilder();   //Remove this line from here at place it outside the parent foreach loop, as shown.
                                                               //Reason: This re-initializes your "stringBuilder" to a new one, with the each new loop.

                foreach (object column in phoneDataRow.ItemArray)
                {
                    phoneDetails[columnIndex] = Convert.ToString(column);
                    if (phoneDetails[columnIndex] != null && phoneDetails[columnIndex].Length > 0)
                    {
                        stringBuilder.Append(phoneDetails[columnIndex]);
                        stringBuilder.Append(" ");
                    }
                    columnIndex++;
                }
                newItem.Text = stringBuilder.ToString();
                pnlPhone.Controls.Add(newItem);
            }
        }

Regards, Lakra :) - If the post is helpful or answers your question, please mark it as such.
Abhijeet Lakra  Monday, June 15, 2009 1:20 AM
Lakra, thank you for the reply.

I am trying to add a label for each phone data row in the database related to the company. For each label I build a string with the column values of the row and set that to the label. The problem I am having is that the second label does not display on the panel.
When I step through the code the pnlPhone panel control has two labels in it's collection, but only the first one displays.

I did try moving the stringBuilder out and just have one string and label for all the phone numbers, and it works! But I would like to understand why the second label in my panel control does not display in the original code I posted.

        /// <summary>
        /// Builds and displays the currently selected company details.
        /// </summary>
        private void buildCompanyDetails()
        {
            Company company = Company.getCompany((Int16)dataGridView1.Rows[((CurrencyManager)BindingContext[dataGridView1.DataSource]).Position].Cells[0].Value, out company);

            lblCompanyNameValue.Text = company.CompanyName;
            lblIndustryTypeValue.Text = company.CompanyIndustry;
            lblWebsiteValue.Text = company.CompanyWebsite;
            lblStatusValue.Text = company.CompanyStatus;

            pnlPhone.Controls.Clear();
            Label newItem = new Label();
            newItem.Width = 200;
            newItem.Height = 150;
            StringBuilder stringBuilder = new StringBuilder();

            foreach (DataRow phoneDataRow in company.CompanyPhone)
            {                
                string[] phoneDetails = new string[phoneDataRow.ItemArray.Count()];
                int columnIndex = 0;               
                
                //newItem = new Label();
                //newItem.Width = 200;
                //newItem.Height = 150;
                //StringBuilder stringBuilder = new StringBuilder();

                foreach (object column in phoneDataRow.ItemArray)
                {
                    phoneDetails[columnIndex] = Convert.ToString(column);                    
                    if(phoneDetails[columnIndex] != null && phoneDetails[columnIndex].Length > 0)
                    {
                        stringBuilder.Append(phoneDetails[columnIndex]);
                        stringBuilder.Append(" ");                   
                    }
                    columnIndex++;
                }
                stringBuilder.Append("\n");
                newItem.Text = stringBuilder.ToString();               
                //pnlPhone.Controls.Add(newItem);
            }
            pnlPhone.Controls.Add(newItem);
        }

AJ720  Monday, June 15, 2009 2:09 AM
The issue that I suspect is:
You are adding the controls(Labels)but you are not setting the Location for them.
As such they are being added one, on top of other.

Please set the location specifically for the Labels, something like:

        private void buildCompanyDetails()
        {
            Company company = Company.getCompany((Int16)dataGridView1.Rows[((CurrencyManager)BindingContext[dataGridView1.DataSource]).Position].Cells[0].Value, out company);

            lblCompanyNameValue.Text = company.CompanyName;
            lblIndustryTypeValue.Text = company.CompanyIndustry;
            lblWebsiteValue.Text = company.CompanyWebsite;
            lblStatusValue.Text = company.CompanyStatus;

            pnlPhone.Controls.Clear();
            //Label newItem = new Label();    //Remove these code from here.
            //newItem.Width = 200;
            //newItem.Height = 150;
            StringBuilder stringBuilder = new StringBuilder();
            Point locatiioOfFirstLabel = new Point(100, 100);   //New Variable

            foreach (DataRow phoneDataRow in company.CompanyPhone)
            {
                string[] phoneDetails = new string[phoneDataRow.ItemArray.Count()];
                int columnIndex = 0;

                Label newItem = new Label();    //Place the above code here.
                newItem.Width = 200;
                newItem.Height = 150;
                newItem.Location = new Point(locatiioOfFirstLabel.X, locatiioOfFirstLabel.Y + 20);

                foreach (object column in phoneDataRow.ItemArray)
                {
                    phoneDetails[columnIndex] = Convert.ToString(column);
                    if (phoneDetails[columnIndex] != null && phoneDetails[columnIndex].Length > 0)
                    {
                        stringBuilder.Append(phoneDetails[columnIndex]);
                        stringBuilder.Append(" ");
                    }
                    columnIndex++;
                }
                stringBuilder.Append("\n");
                newItem.Text = stringBuilder.ToString();
                //pnlPhone.Controls.Add(newItem);
            }
            pnlPhone.Controls.Add(newItem);
        }

Regards, Lakra :) - If the post is helpful or answers your question, please mark it as such.
  • Marked As Answer byAJ720 Monday, June 15, 2009 1:57 PM
  •  
Abhijeet Lakra  Monday, June 15, 2009 2:44 AM
Thanks Lakra, I finally got this to work. Setting the location was indeed the issue

 /// <summary>
        /// Builds and displays the currently selected company details.
        /// </summary>
        private void buildCompanyDetails()
        {
            Company company = Company.getCompany((Int16)dataGridView1.Rows[((CurrencyManager)BindingContext[dataGridView1.DataSource]).Position].Cells[0].Value, out company);

            lblCompanyNameValue.Text = company.CompanyName;
            lblIndustryTypeValue.Text = company.CompanyIndustry;
            lblWebsiteValue.Text = company.CompanyWebsite;
            lblStatusValue.Text = company.CompanyStatus;

            pnlPhone.Controls.Clear();
            Point locationOfFirstLabel = new Point(0, 0);
            int yLocation = 0;

            foreach (DataRow phoneDataRow in company.CompanyPhone)
            {               
                string[] phoneDetails = new string[phoneDataRow.ItemArray.Count()];
                int columnIndex = 0;               
                
                Label newItem = new Label();
                newItem.Width = 200;
                newItem.Height = 15;
                newItem.Location = new Point(locationOfFirstLabel.X, locationOfFirstLabel.Y + yLocation);
                yLocation = yLocation + 20;
                StringBuilder stringBuilder = new StringBuilder();               

                foreach (object column in phoneDataRow.ItemArray)
                {
                    phoneDetails[columnIndex] = Convert.ToString(column);                    
                    if(phoneDetails[columnIndex] != null && phoneDetails[columnIndex].Length > 0)
                    {
                        stringBuilder.Append(phoneDetails[columnIndex]);
                        stringBuilder.Append(" ");                   
                    }
                    columnIndex++;
                }
                newItem.Text = stringBuilder.ToString();               
                pnlPhone.Controls.Add(newItem);
            }
        }

  • Edited byAJ720 Monday, June 15, 2009 1:57 PMupdate
  •  
AJ720  Monday, June 15, 2009 1:55 PM

You can use google to search for other answers

Custom Search

More Threads

• Property Grid with file path
• How to validate a datagridview cell when save button is clicked
• Autofit printed text
• When adding new row in Data gridView Preivous Row data got cleared? Why?
• how to make the form grow dynamically based on the number of records.
• Async WebRequest not firing Callback
• Treeview control problem: Selected node is not retained when parent node is closed.
• Capturing form into bitmap
• Clearing a Windows Forms panel containing a DirectX movie
• WS_EX_TRANSPARENT cause flickering...