2008-12-08 37 views
1

我已經寫了一個簡單的控件,它基本上顯示了幾個字旁邊的圖像。我需要幫助,在我的控制上滾動 - c#

我希望包含項目在父窗體大小調整時拉伸,正如您從我註釋掉的代碼中可以看到的,我不想在使用循環時閃爍。

任何想法如何讓項目成長和收縮的形式在一個很好的方式嗎?

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

     } 
    } 

回答

2

避免被嵌入在每次調整子控件時間觸發重繪你foreachSuspendLayout()ResumeLayout()

this.SuspendLayout(); 

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

this.ResumeLayout(); 
0

在表格上固定右邊和底部,也許停靠它們。

+0

我試圖錨和控制拒絕調整大小 – 2008-12-08 11:26:14