Home > other >  Update Labels in a UserControl in TabPage
Update Labels in a UserControl in TabPage

Time:06-29

Thanks to stackoverflow I was able to dynamically create tab pages for my project. The issue I'm running into is how to update content in the UserControl I have in my TabPanel TabPages.

Here is how I'm currently creating the Tabs (there is one default tab that isn't created as that will never change all other tabs are created upon frm_load)

private void TechnicianInfo()
        {
            var techSheet = smartSheet.SheetResources.GetSheet(
                0000000000000000, // all 0's for security purposes
                null,
                null,
                null,
                null,
                null,
                null,
                null);

            // API identifies columns by ID, but it's more convenient to refer to column names
            Dictionary<string, long> technicianMap = new Dictionary<string, long>();

            // Build the column map for easier reference
            foreach (Column column in techSheet.Columns)
                technicianMap.Add(column.Title, (long)column.Id);

            // Let's loop through the rows and add in information
            foreach (Row row in techSheet.Rows)
            {
                Cell techName = row.Cells.First(cell => cell.ColumnId == technicianMap["Technician Name"]);
                Cell techCertified = row.Cells.First(cell => cell.ColumnId == technicianMap["Certified Radios"]);
                Cell techLevel = row.Cells.First(cell => cell.ColumnId == technicianMap["Technician Level"]);
                Cell techEmail = row.Cells.First(cell => cell.ColumnId == technicianMap["Email"]);

                // Dyanmiclly Create Tab Pages with correct info on them
                UserControl newTechnicianControl = new TechnicianControl
                {
                    TechnicianLevel = techLevel.DisplayValue,
                    CertifiedRadios = techCertified.DisplayValue,
                    EmailAddress = techEmail.DisplayValue

                };
                TabPage techPage = new TabPage();
                techPage.Controls.Add(newTechnicianControl);
                techPage.Text = techName.DisplayValue.ToString();
                techPage.Name = "tech_"   techName.DisplayValue.ToLower();
                tcRepairs.TabPages.Add(techPage);
            }
        }

During the creation, I'm able to access the properties of the UserControl upon defining it. I have a timer to pull updated information from smartsheets once every hour (please note I'm not pulling all the information I need right now. I want to figure out my issue first before I pull all information). Upon the timers tick event I'd like to call an update function that will update the labels of the UserControls in each TabPage.

I have the UserControl set up with three properties as of right now.

public string TechnicianLevel
        {
            get
            {
                return this.lblTechLevel.Text;
            }
            set
            {
                this.lblTechLevel.Text = value;
            }
        }

        public string CertifiedRadios
        {
            get
            {
                return this.lblCertifiedRadios.Text;
            }
            set
            {
                this.lblCertifiedRadios.Text = value;
            }
        }

        public string EmailAddress
        {
            get
            {
                return this.lblEmail.Text;
            }
            set
            {
                this.lblEmail.Text = value;
            }
        }

The only way I can think to get this to work is to remove all except the first TabPage and then re-add them. I feel like this method would work but I feel like there should be a better way of being able to update the UserControl in the TabPage.

CodePudding user response:

Thanks to @Jimi and their idea of looping through the TabPages. Below is now currently working for updating the UserControl TechnicianControl labels. Thanks a lot!

private void UpdateTechnicianInfo()
        {
            // API identifies columns by ID, but it's more convenient to refer to column names
            Dictionary<string, long> columnMap = new Dictionary<string, long>();

            var techSheet = smartSheet.SheetResources.GetSheet(
                2589985581885316,
                null,
                null,
                null,
                null,
                null,
                null,
                null);

            // API identifies columns by ID, but it's more convenient to refer to column names
            Dictionary<string, long> technicianMap = new Dictionary<string, long>();

            // Build the column map for easier reference
            foreach (Column column in techSheet.Columns)
                technicianMap.Add(column.Title, (long)column.Id);

            // Lets loop through the rows and add in information
            foreach (Row row in techSheet.Rows)
            {
                Cell techName = row.Cells.First(cell => cell.ColumnId == technicianMap["Technician Name"]);
                Cell techCertified = row.Cells.First(cell => cell.ColumnId == technicianMap["Certified Radios"]);
                Cell techLevel = row.Cells.First(cell => cell.ColumnId == technicianMap["Technician Level"]);
                Cell techEmail = row.Cells.First(cell => cell.ColumnId == technicianMap["Email"]);

                foreach (TabPage page in tcRepairs.TabPages)
                {
                    TechnicianControl technicianControl2 = page.Controls.OfType<TechnicianControl>().FirstOrDefault();
                    if (technicianControl2 != null && && page.Name == "tech_"   techName.DisplayValue.ToString())
                    {
                        technicianControl2.TechnicianLevel = techName.DisplayValue.ToString();
                    }
                }
            }            
        }
  • Related