Home > Blockchain >  Make a CustomListBox to add BorderColor then can't use ListBox normal properties/methods
Make a CustomListBox to add BorderColor then can't use ListBox normal properties/methods

Time:10-25

I'm trying to change the border color of a ListBox.

I made this custom control where i have a function that makes the border by drawing.

After had it working, noticed that can't use ListBox.Items anymore, methods such as .Add() or .Clear().

Code of the Custom ListBox:

class CustomListBox : UserControl
    {
        //Fields
        private Color borderColor = Color.MediumSlateBlue;
        private int borderSize = 1;


        //Items
        private ListBox Listb;

        //Properties
        [Category("Custom")]
        public Color BorderColor
        {
            get { return borderColor; }
            set
            {
                borderColor = value;
            }
        }

        [Category("Custom")]
        public int BorderSize
        {
            get { return borderSize; }
            set
            {
                borderSize = value;
                this.Padding = new Padding(borderSize);//Border Size
                AdjustListBoxDimensions();
            }
        }




        public CustomListBox()
        {
            Listb = new ListBox();
            this.SuspendLayout();
            // ListBox
            Listb.BorderStyle = BorderStyle.None;
            Listb.DrawMode = DrawMode.OwnerDrawFixed;
            Listb.ForeColor = Color.FromArgb(((int)(((byte)(249)))), ((int)(((byte)(249)))), ((int)(((byte)(249)))));
            Listb.FormattingEnabled = true;
            Listb.ItemHeight = 24;
            Listb.Location = new Point(567, 64);
            Listb.Name = "CustomListBox";
            Listb.Size = new Size(235, 936);
            Listb.DrawItem  = new DrawItemEventHandler(Listb_DrawItem);

            this.MinimumSize = new Size(200, 30);
            this.Size = new Size(200, 30);
            this.Padding = new Padding(borderSize);//Border Size
            this.Font = new Font(this.Font.Name, 12F);
            this.ResumeLayout();
            AdjustListBoxDimensions();

        }

        // Highlight event
        private void Listb_DrawItem(object sender, DrawItemEventArgs e)
        {
            Color backgroundColor = Color.FromArgb(50, 50, 50);
            Color horizontalColor = Color.FromArgb(100, 100, 100);


            if (e.Index >= 0)
            {
                SolidBrush sb = new SolidBrush(((e.State & DrawItemState.Selected) == DrawItemState.Selected) ? horizontalColor : backgroundColor);
                e.Graphics.FillRectangle(sb, e.Bounds);
                string text = Listb.Items[e.Index].ToString();
                SolidBrush tb = new SolidBrush(e.ForeColor);
                e.Graphics.DrawString(text, e.Font, tb, Listb.GetItemRectangle(e.Index).Location);
            }
        }

        //Adjust Dimension (Still in test)
        private void AdjustListBoxDimensions()
        {
            Listb.Location = new Point()
            {
                X = this.Width - this.Padding.Right - Listb.Width,
                Y = Listb.Height
            };
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics graph = e.Graphics;
            //Draw border
            using (Pen penBorder = new Pen(borderColor, borderSize))
            {
                penBorder.Alignment = PenAlignment.Inset;
                graph.DrawRectangle(penBorder, 0, 0, this.Width - 0.5F, this.Height - 0.5F);
            }
        }



    }

My problem is that i can't use the ListBox properties/methods, is there a way to inherit them?

CodePudding user response:

Since you've made your own control, you have to make your own properties and methods, calling Listbox's methods and getters/setters

something like this:

public void Add(object item)
{
    this.Listb.Items.Add(item);
}

public ObjectCollection ListItems
{
    get
    {
        return this.Listb.Items;
    }
    set
    {
        this.Listb.Items.AddRange(value);
    }
}

//etc...

CodePudding user response:

 public void Add(object item)
    {
        this.Listb.BeginUpdate();
        this.Listb.Items.Add(item);
        this.Listb.EndUpdate();
    }

    public void Clear()
    {
        this.Listb.BeginUpdate();
        this.Listb.Items.Clear();
        this.Listb.EndUpdate();
    }

    public int SelectedIndex()
    {
        return this.Listb.SelectedIndex;
    }

    public object Item(int index)
    {
        return this.Listb.Items[index];
    }

Add to code: Dock=Fill This.control.add(listb)

  • Related