I need help scrolling through my control - C #

I wrote a simple control that basically displays a few words with a snapshot next to it.

I want the contained elements to expand when the parent form has been resized, and as you can see from my commented code, I don't want to use a loop when it flickers.

Any idea on how to make objects grow and shrink with a shape in a pleasing 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) {

        }
    }

      

+1


source to share


3 answers


Avoid calling redrawing every time the child control is resized by inserting foreach

in SuspendLayout()

and ResumeLayout()

:



this.SuspendLayout();

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

this.ResumeLayout();

      

+2


source


Anchor right and right on the form and possibly their docking station.



0


source


You can try enabling double buffering using the information here .

0


source







All Articles