Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Custom ListBox Issue

Status
Not open for further replies.

djfrear

Programmer
Jul 11, 2007
83
GB
I have been working on a program which shows data via a listbox. Unfortunately the standard listbox seems to only allow strings which all use the same font/style.

I needed the ability to either make a certain item bold or a different colour, I went for colour and wrote a very simple custom control as follows:

Code:
    public partial class exListBox : ListBox
    {
        private Brush _forecolor;
        private Font _dataFont;

        public exListBox(Brush ForeColor, Font DataFont)
        {
            _forecolor = ForeColor;
            _dataFont = DataFont;
        }

        public exListBox()
        {
            InitializeComponent();
            _forecolor = Brushes.ForestGreen;
            _dataFont = new Font(this.Font, FontStyle.Regular);
            this.DrawMode = DrawMode.OwnerDrawVariable;
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            if (this.Items.Count > 0)
            {
                exListBoxItem item = (exListBoxItem)this.Items[e.Index];
                item.drawItem(e, _forecolor, _dataFont);
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
        }
    }

//begin exListBoxItem

    public class exListBoxItem
    {
        private int _id;
        private string _data;

        public int ID
        {
            get { return _id; }
            set { _id = value; }
        }

        public string Data
        {
            get { return _data; }
            set { _data = value; }
        }

        public exListBoxItem(int id, string data)
        {
            _id = id;
            _data = data;
        }

        public void drawItem(DrawItemEventArgs e, Brush ForeColor, Font DataFont)
        {
            int pointVal =0;

            if (ID == 1)
            {
                pointVal = 0;
            }
            else
            {
                pointVal = (ID-1) * DataFont.Height;
            }

            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            {
                e.Graphics.FillRectangle(Brushes.CornflowerBlue, e.Bounds);
            }
            else
            {
                e.Graphics.FillRectangle(Brushes.White, e.Bounds);
            } 

            e.Graphics.DrawString(this.Data, DataFont, ForeColor, new PointF(1, pointVal));

            e.DrawFocusRectangle();
        }
    }

As you can see its very simple but it's the first time I've written a custom control/extended a control so if there are any obvious errors you will have to forgive me!

The control works fine apart from when the list becomes scrollable e.g. add 10 items, at 11 the scrollbar appears and the user scrolls down. When this occurs the items which were not visible don't show up - you can select them but the text remains invisible/white.

Now im not sure if there is some sort of event I should be overriding to make sure it draws those extra items or not.

Any input would be most appreciated.

Thanks.
 
Will try that when I get to work tomorrow morning, cheers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top