【问题标题】:I need help with scrolling on my control - c#我需要帮助滚动我的控件 - c#
【发布时间】:2008-12-08 11:11:15
【问题描述】:

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

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

你知道如何让项目以一种很好的方式随着表单的增长和收缩吗?

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

        }
    }

【问题讨论】:

    标签: c# winforms controls


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      在表单的右下角锚定,并可能停靠它们。

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 2023-02-25
          • 2022-01-04
          • 2019-08-08
          • 2016-02-11
          • 2022-12-16
          • 1970-01-01
          • 1970-01-01
          • 2022-01-10
          • 2021-07-04
          相关资源
          最近更新 更多