滚动我的控件需要帮助 - c#

时间:2008-12-08 11:11:15

标签: c# winforms controls

我写了一个简单的控件,它基本上显示了几个单词,旁边有一个图像。

我希望在调整父窗体大小时拉伸包含的项目,正如您从注释掉的代码中看到的那样,我不想使用循环,因为它会闪烁。

有关如何以一种不错的方式使表单增长和缩小的任何想法吗?

   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) {

        }
    }

3 个答案:

答案 0 :(得分:2)

通过在foreachSuspendLayout()中嵌入ResumeLayout()来避免每次调整子控件的大小时触发重绘:

this.SuspendLayout();

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

this.ResumeLayout();

答案 1 :(得分:0)

在表格上固定右下角,然后停靠它们。

答案 2 :(得分:0)

您可以尝试使用信息here启用双缓冲。