Home > database >  Why aren't the generated buttons showing in the FlowLayoutPanel?
Why aren't the generated buttons showing in the FlowLayoutPanel?

Time:10-04

So, I'm trying to create an accordion with dynamically loaded buttons. In the future, the title of the buttons will change depending on the details I've retrieved from somewhere. For now, what I'm trying to do is to load buttons to look like these:

enter image description here

I've tried doing the following below:

// Forms1.cs

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int buttonCount = 3;
            var buttons = new FontAwesome.Sharp.IconButton[buttonCount];
            for (int i = 0; i < buttonCount; i  )
            {
                var btn = new FontAwesome.Sharp.IconButton
                {
                    Text = "Button "   i,
                    TextAlign = ContentAlignment.MiddleLeft,
                    IconChar = FontAwesome.Sharp.IconChar.Book,
                    IconColor = ColorTranslator.FromHtml("#6A6A73"),
                    IconSize = 20,
                    IconFont = FontAwesome.Sharp.IconFont.Auto,
                    TextImageRelation = TextImageRelation.ImageBeforeText,
                    FlatStyle = FlatStyle.Flat
                };
                btn.FlatAppearance.BorderSize = 0;
                btn.ForeColor = ColorTranslator.FromHtml("#6A6A73");
                btn.BackColor = ColorTranslator.FromHtml("#FDFEFF");
                btn.Dock = DockStyle.Top;
                buttons[i] = btn;
            }
            flowLayoutPanel1.Controls.AddRange(buttons);
        }
    }
}
// Forms1.Designer.cs

namespace WindowsFormsApp1
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
            this.SuspendLayout();
            // 
            // flowLayoutPanel1
            // 
            this.flowLayoutPanel1.BackColor = System.Drawing.SystemColors.ControlLight;
            this.flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Left;
            this.flowLayoutPanel1.Location = new System.Drawing.Point(0, 0);
            this.flowLayoutPanel1.Name = "flowLayoutPanel1";
            this.flowLayoutPanel1.Size = new System.Drawing.Size(200, 450);
            this.flowLayoutPanel1.TabIndex = 0;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.flowLayoutPanel1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel1;
    }
}

Here's what it looks like after building and running the application:

enter image description here

What am I doing wrong?

CodePudding user response:

Form1_Load method is not subscribed to Load event of your form. Body of InitializeComponent is missing following line of code.

this.Load  = new System.EventHandler(this.Form1_Load);

Insert this line before this.ResumeLayout(false);. You can fix it in designer as well.

  • Related