views:

756

answers:

3

Hi,

I've written a simple control which basically displays a few words with an image next to it.

I want the containing items to strech when the parent form is resized and as you can see from my commented out code, I don't want to use a loop as it flickers.

Any idea on how to get the items to grow and shrink with the form in a nice way?

   public class IconListBox : FlowLayoutPanel  {

        private const int ITEM_PADDING = 2;
        private const int MAX_IMAGE_SIZE = 64;


        List<FlowLayoutPanel> _listItems;


        public IconListBox()    {

            this.SizeChanged += new EventHandler(IconListBox_SizeChanged);
            this.AutoScroll = true;
            this.HorizontalScroll.Enabled = false;
            this.HorizontalScroll.Visible = false;
            this.VerticalScroll.Enabled = true;
            this.VerticalScroll.Visible = true;

            _listItems = new List<FlowLayoutPanel>();
        }

        void IconListBox_SizeChanged(object sender, EventArgs e) {

            //foreach (FlowLayoutPanel item in _listItems) {
            //    item.Width = this.Width - 10;
            //}
        }

        public void AddItem(string itemText) {

            PictureBox pic = new PictureBox();
            pic.Image = MyWave.Properties.Resources.mywave_icon;
            pic.Width = pic.Height = MAX_IMAGE_SIZE;
            pic.SizeMode = PictureBoxSizeMode.Normal;
            pic.Enabled = false;

            FlowLayoutPanel p = new FlowLayoutPanel();
            p.Width = this.Width;
            p.Height = pic.Image.Height + (ITEM_PADDING * 4);
            p.BackColor = Color.White;
            p.Padding = new Padding(ITEM_PADDING);
            p.Margin = new Padding(0);

            Label l = new Label();
            l.Margin = new Padding(10, 5, 0, 0);
            l.Width = this.Width - ITEM_PADDING - MAX_IMAGE_SIZE;
            l.Height = p.Height - (ITEM_PADDING * 2);
            l.Text = itemText;
            l.Enabled = false;
            //l.BorderStyle = BorderStyle.FixedSingle;


            p.Controls.Add(pic);
            p.Controls.Add(l);


            p.MouseEnter += new EventHandler(p_MouseEnter);
            p.MouseLeave += new EventHandler(p_MouseLeave);
            p.MouseClick += new MouseEventHandler(p_MouseClick);

            this.Controls.Add(p);
            _listItems.Add(p);

            p.Anchor = AnchorStyles.Right;

        }

        void p_MouseClick(object sender, MouseEventArgs e) {
            //throw new NotImplementedException();
        }

        void p_MouseLeave(object sender, EventArgs e) {
            ((Panel)sender).BackColor = Color.White;
        }

        void p_MouseEnter(object sender, EventArgs e) {
            ((Panel)sender).BackColor = Color.LightBlue;
        }

        public void AddItem(string itemText, Image icon) {

        }
    }
A: 

Anchor right and bottom on the form and maybe dock them.

xoxo
I tried to Anchor and the control refused to resize
Sir Psycho
+2  A: 

Avoid triggering a redraw every time you resize a child control by embedding your foreach in SuspendLayout() and ResumeLayout():

this.SuspendLayout();

foreach (FlowLayoutPanel item in _listItems) 
{
    item.Width = this.Width - 10;
}

this.ResumeLayout();
Treb
A: 

You can try enabling double buffering by using the information here.