none
ListBox doesn't filled completely when placed inside a panel RRS feed

  • Question

  • I am having an panel in a Forms and placed a listbox dock filled inside the panel, but it doesn't filled properly and listbox height was decreased. I have applied padding to be 1 and Panel and listbox are of same height and width.

    

         // 
                this.listBox_Words.FormattingEnabled = true;
                this.listBox_Words.Name = "listBox_Words";
                this.listBox_Words.BorderStyle = BorderStyle.None;
                //
                // panel
                //
                this.panel.Size = new System.Drawing.Size(329, 95);
                this.panel.Location = new System.Drawing.Point(11, 133);
                this.panel.Controls.Add(listBox_Words);
                this.panel.BorderStyle = BorderStyle.None;
                this.panel.Padding = new Padding(1);
                this.panel.AutoSize = true;
                this.listBox_Words.Dock = DockStyle.Fill;
                this.panel.BackColor = Color.Yellow;
                // 
                // CustomDictionaryMetroEditor
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(349, 307);
                this.Controls.Add(this.panel);


    Friday, April 12, 2019 10:44 AM

Answers

  • Hello,

    See if this works for you, add the following class to a project, compile then with a form showing at the top of the IDE toolbox you will find the custom ListBox which permits setting the Border color.

    Change the namespace to match your project's namespace

    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public class MyListBox : ListBox
        {
            [Flags()]
            private enum RedrawWindowFlags : uint
            {
                Invalidate = 0x1,
                InternalPaint = 0x2,
                Erase = 0x4,
                Validate = 0x8,
                NoInternalPaint = 0x10,
                NoErase = 0x20,
                NoChildren = 0x40,
                AllChildren = 0x80,
                UpdateNow = 0x100,
                EraseNow = 0x200,
                Frame = 0x400,
                NoFrame = 0x800
            }
            [DllImport("user32.dll")]
            private extern static bool ReleaseDC(IntPtr hWnd, IntPtr hDC);
            [DllImport("user32.dll")]
            private extern static bool RedrawWindow(
                IntPtr hWnd, 
                IntPtr lprcUpdate, 
                IntPtr hrgnUpdate, 
                RedrawWindowFlags flags);
    
            [DllImport("User32.dll", 
                EntryPoint = "GetWindowDC", 
                ExactSpelling = true, 
                CharSet = CharSet.Ansi, SetLastError = true)]
    
            private static extern IntPtr GetWindowDC(IntPtr hwnd);
            public const int WM_NCPAINT = 0x85;
            private Color _BorderColor = SystemColors.ControlDark;
            [Description("Set border color"), Category("Appearance")]
            public Color BorderColor
            {
                get => _BorderColor;
                set => _BorderColor = value;
            }
            public MyListBox()
            {
                BorderStyle = BorderStyle.Fixed3D;
            }
            protected override void OnResize(System.EventArgs e)
            {
                base.OnResize(e);
                RedrawWindow(
                    Handle, 
                    IntPtr.Zero, 
                    IntPtr.Zero,
                    RedrawWindowFlags.Frame | 
                    RedrawWindowFlags.UpdateNow | 
                    RedrawWindowFlags.Invalidate);
            }
            protected override void WndProc(ref Message m)
            {
                base.WndProc(ref m);
                if (m.Msg == WM_NCPAINT)
                {
                    IntPtr hDC = GetWindowDC(m.HWnd);
                    var thePen = new Pen(BorderColor);
                    using (var g = Graphics.FromHdc(hDC))
                    {
                        g.DrawRectangle(thePen, new Rectangle(0, 0, Width - 1, Height - 1));
                        g.DrawRectangle(SystemPens.Window, new Rectangle(1, 1, Width - 3, Height - 3));
                    }
                    ReleaseDC(m.HWnd, hDC);
                    thePen.Dispose();
                }
            }
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, April 12, 2019 10:53 PM
    Moderator

All replies

  • Try setting the IntegralHeight property of listbox to False.

    If you do not like partial items, then you probably have to disable the docking and resize the panel according to listbox.

    Friday, April 12, 2019 11:36 AM
  • Hello,

    See if this works for you, add the following class to a project, compile then with a form showing at the top of the IDE toolbox you will find the custom ListBox which permits setting the Border color.

    Change the namespace to match your project's namespace

    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public class MyListBox : ListBox
        {
            [Flags()]
            private enum RedrawWindowFlags : uint
            {
                Invalidate = 0x1,
                InternalPaint = 0x2,
                Erase = 0x4,
                Validate = 0x8,
                NoInternalPaint = 0x10,
                NoErase = 0x20,
                NoChildren = 0x40,
                AllChildren = 0x80,
                UpdateNow = 0x100,
                EraseNow = 0x200,
                Frame = 0x400,
                NoFrame = 0x800
            }
            [DllImport("user32.dll")]
            private extern static bool ReleaseDC(IntPtr hWnd, IntPtr hDC);
            [DllImport("user32.dll")]
            private extern static bool RedrawWindow(
                IntPtr hWnd, 
                IntPtr lprcUpdate, 
                IntPtr hrgnUpdate, 
                RedrawWindowFlags flags);
    
            [DllImport("User32.dll", 
                EntryPoint = "GetWindowDC", 
                ExactSpelling = true, 
                CharSet = CharSet.Ansi, SetLastError = true)]
    
            private static extern IntPtr GetWindowDC(IntPtr hwnd);
            public const int WM_NCPAINT = 0x85;
            private Color _BorderColor = SystemColors.ControlDark;
            [Description("Set border color"), Category("Appearance")]
            public Color BorderColor
            {
                get => _BorderColor;
                set => _BorderColor = value;
            }
            public MyListBox()
            {
                BorderStyle = BorderStyle.Fixed3D;
            }
            protected override void OnResize(System.EventArgs e)
            {
                base.OnResize(e);
                RedrawWindow(
                    Handle, 
                    IntPtr.Zero, 
                    IntPtr.Zero,
                    RedrawWindowFlags.Frame | 
                    RedrawWindowFlags.UpdateNow | 
                    RedrawWindowFlags.Invalidate);
            }
            protected override void WndProc(ref Message m)
            {
                base.WndProc(ref m);
                if (m.Msg == WM_NCPAINT)
                {
                    IntPtr hDC = GetWindowDC(m.HWnd);
                    var thePen = new Pen(BorderColor);
                    using (var g = Graphics.FromHdc(hDC))
                    {
                        g.DrawRectangle(thePen, new Rectangle(0, 0, Width - 1, Height - 1));
                        g.DrawRectangle(SystemPens.Window, new Rectangle(1, 1, Width - 3, Height - 3));
                    }
                    ReleaseDC(m.HWnd, hDC);
                    thePen.Dispose();
                }
            }
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, April 12, 2019 10:53 PM
    Moderator
  • Hi,

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer.So that it will help other members to find the solution quickly if they face the similar issue.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 18, 2019 6:38 AM
    Moderator