2014-02-08 129 views
2

我有一個flowlayoutpanel在我的winform中動態添加圖像。我想vertical scroll bar總是在底部顯示最後添加的圖像。我怎樣才能做到這一點? 我總是滾動到垂直滾動條的底部

AutoScroll = true

FLow Direction = Top Down

Wrap Content = False

回答

6

滾動容器控件,比如FlowLayoutPanel的,自動保持在視圖中的聚焦控制。但PictureBox是特殊的,它不能獲得焦點。所以你必須明確要求FLP使添加的控件可見,並使用它的ScrollControlIntoView()方法。就像這樣:

var pic = new PictureBox(); 
    //... 
    flowLayoutPanel1.Controls.Add(pic); 
    flowLayoutPanel1.ScrollControlIntoView(pic); 

有了強大的優勢,這適用於任何佈局您設置應用到FLP。您也可以與AutoScrollPosition屬性鼓搗,但是這是很難獲得這種權利:

flowLayoutPanel1.AutoScrollPosition = new Point(
     pic.Right - flowLayoutPanel1.AutoScrollPosition.X, 
     pic.Bottom - flowLayoutPanel1.AutoScrollPosition.Y); 
+0

日Thnx。它做了這項工作。我可以使用這兩種方法,因爲我有linkbox下面的picturebox。 – umuieme

1

試試這個:

scrollBar.Value=scrollBar.Maximum; 

這裏滾動條在winform的滾動條控制。

對於更詳細地,check this.

0

這是一種強制的最後控制進入視野。

 flowLayoutPanel.ScrollControlIntoView(Control_To_Add); // Control_To_Add is the control we want to scroll to 
     Button TempButton = new Button(); 
     TempButton.Width = _Panel.ClientRectangle.Width - 6; // Make the last control in the _Panel 
     flowLayoutPanel.Controls.Add(TempButton); // We add this TempButton so we can scroll to the bottom of the _Panel.Controls 
     flowLayoutPanel.ScrollControlIntoView(b); // We scroll to TempButton at the bottom of the _Panel.Controls 
     flowLayoutPanel.Controls.Remove(b); // We remove TempButton 
     b.Dispose(); // clean up 
0

強制FlowLayoutPanel滾動並顯示所有控件。 碼校正:

flowLayoutPanel.ScrollControlIntoView(Control_To_Add); // Control_To_Add is the control we want to scroll to 
Button TempButton = new Button(); 
TempButton.Width = _Panel.ClientRectangle.Width - 6; // Make the last control in the _Panel 
flowLayoutPanel.Controls.Add(TempButton); // We add this TempButton so we can scroll to the bottom of the _Panel.Controls 
flowLayoutPanel.ScrollControlIntoView(TempButton); // We scroll to TempButton at the bottom of the _Panel.Controls 
flowLayoutPanel.Controls.Remove(TempButton); // We remove TempButton 
b.Dispose(); // clean up 
+0

b.Dispose();應該是TempButton.Dispose() – bclough

+0

如果您希望每次發生滾動事件時都使用滾動事件,則可以使用滾動事件,但可以隨時調用此處顯示的代碼。 – bclough

0

這是正確的做法:

MyControl uct = new MyControl(); 
uct.Parent = flowLayoutPanel; 

this.ActiveControl = uct; 



if (flowLayoutPanel.VerticalScroll.Visible) 
{ 
    flowLayoutPanel.ScrollControlIntoView(uct); 
}