Panel having label position and border issue

Rishabh aggarwal 40 Reputation points
2023-11-09T18:10:57.5433333+00:00

I am being tried to create a panel which looks exactly like the same .

User's image

but i found out that the default panel control doesn't fit my requirements . Any expert here, can please help me creating a panel in which the upper border is slightly bold and the below border is slim . another problem is how to place labels at a fixed positions just at above . i would be highly thankful to the expert as i am very poor in gdi+ and new to visual studio environment

Developer technologies | Windows Forms
Developer technologies | C#
{count} votes

Accepted answer
  1. Anonymous
    2023-11-10T07:29:04.8633333+00:00

    Hi @Rishabh aggarwal , Welcome to Microsoft Q&A, You can use borderline and flowlayoutpanel to achieve your needs. Then put the label in it. The simplified code is as follows:

    using System.Windows.Forms;
    
    namespace _11_10_x
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                InitializeCustomComponents();
            }
            private void InitializeCustomComponents()
            {
                //Create panel
                Controls.Add(new Panel
                {
                    BorderStyle = BorderStyle.None,
                    Dock = DockStyle.Fill,
                    Controls =
    {
         // top border
         new Label
         {
             BackColor = System.Drawing.Color.Black,
             Dock = DockStyle.Top,
             Height = 1
         },
         // FlowLayoutPanel
         new FlowLayoutPanel
         {
             Dock = DockStyle.Top,
             Height = 30,
             FlowDirection = FlowDirection.LeftToRight,
             Padding = new Padding(10),
             Controls =
             {
                 // tag 1
                 new Label
                 {
                     Text = "Label 1",
                     Anchor = AnchorStyles.Top
                 },
                 // tag 2
                 new Label
                 {
                     Text = "Label 2",
                     Anchor = AnchorStyles.Top
                 },
                 // tag 3
                 new Label
                 {
                     Text = "Tag 3",
                     Anchor = AnchorStyles.Top
                 }
             }
         },
         // bottom border
         new Label
         {
             BackColor = System.Drawing.Color.Black,
             Dock = DockStyle.Top,
             Height = 3
         }
    }
                });
            }
        }
    }
    

    enter image description here

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.