Search code examples
c#visual-studiowinforms

Adding dynamic tabs to a tab control


I have the following code

This should add tabs to a tab control... the problem is that Im unable to see the changes if I edit the font size of the panel size, etc.

Also is there a way to see or edit using the visual designer things that are dynamic? I know its hard because its not static, but since the object is declared I was wondering if there is a way to set or see properties using it instead of going through tens of properties depending on how many objects you are creating. What am I missing?

Thanks in advance !

private void AddDynamicTabs()
{
    List<Zonas> ZonasList = Database.GetZonasFromDatabase();
    Console.WriteLine("CANTIDAD DE ZONAS " + ZonasList.Count);

    TAB_ZONAS.TabPages.Clear();
    // Calculate tab width based on the width of Panel_mesas and number of zones
    int tabWidth = Panel_mesas.Width / ZonasList.Count;


    for (int i = 0; i < ZonasList.Count; i++)
    {
        Zonas zonas = ZonasList[i];

        // Create a new tab page
        TabPage tabPage = new TabPage(zonas.NOMBRE);
        tabPage.Name = $"tabPage_{zonas.NOMBRE}";
        tabPage1.Text = $"tabPage_{ zonas.NOMBRE}";

        // Set properties for the tab page
        tabPage.BackColor = Color.LightGray;
        tabPage.ForeColor = Color.Black;
        tabPage.Font = new Font("Arial", 25, FontStyle.Bold);
        tabPage.Padding = new Padding(10); // Padding around the content of the tab page
        tabPage.Margin = new Padding(5); // Margin around the entire tab page
        tabPage.Enabled = true; // Enable or disable the tab page
        tabPage.UseVisualStyleBackColor = true; // Use the default visual style for the tab page
        tabPage.AutoScroll = false; // Enable auto-scrolling for the tab page content
        tabPage.RightToLeft = RightToLeft.No; // Set the text direction for right-to-left languages
        tabPage.Tag = null; // Set an object that contains data about the tab page
        tabPage.ToolTipText = "Tooltip for the tab page"; // Set a tooltip for the tab page
        tabPage.Width = 1000;
        tabPage.Height = 4100;

        // Optionally, set other properties
        // Example:
        // tabPage.BackgroundImage = yourImage;
        // tabPage.BackgroundImageLayout = ImageLayout.Stretch;

        // Add the tab page to the TAB_ZONAS TabControl
        TAB_ZONAS.TabPages.Add(tabPage);

    }
}

Solution

  • This code works:

    private void AddDynamicTabs()
    {
        List<Zonas> ZonasList = Database.GetZonasFromDatabase();
    
        // Create TabControl
        TAB_ZONAS = new TabControl(); // Assign TAB_ZONAS here
        TAB_ZONAS.Size = new System.Drawing.Size(1112, 961);
        TAB_ZONAS.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        TAB_ZONAS.SizeMode = TabSizeMode.Fixed;
    
        // Calculate and set ItemSize based on ZonasList count
        int tabWidth = TAB_ZONAS.Width / ZonasList.Count;
        TAB_ZONAS.ItemSize = new System.Drawing.Size(tabWidth - 10, 150);
    
        TAB_ZONAS.Location = new System.Drawing.Point(654, 12);
        TAB_ZONAS.Name = "TAB_ZONAS";
        TAB_ZONAS.SelectedIndex = 0;
    
        // Add TabPages dynamically based on the list of zones
        foreach (Zonas zonas in ZonasList)
        {
            TabPage tabPage = new TabPage(zonas.NOMBRE)
            {
                ForeColor = System.Drawing.SystemColors.ControlText,
                Location = new System.Drawing.Point(4, 79),
                Name = zonas.NOMBRE, // Unique name for each tab page
                Size = new System.Drawing.Size(1104, 878),
                Text = zonas.NOMBRE, // Set the tab text to the name from the list
                UseVisualStyleBackColor = true
            };
    
            TAB_ZONAS.TabPages.Add(tabPage);
        }
    
        // Add the TabControl to the form
        this.Controls.Add(TAB_ZONAS);
    }