admin管理员组

文章数量:1297082

I'm programmatically generate a TabPage which contains a Panel with various, also programmatically generated controls. The Panel fits it's size to the included controls and can grow larger than the parent TabPage.

I used TabPage.AutoScroll=true; to enable scrolling. The Controls inside the Panel are programmatically generated and receive events, therefore they update their values automatically. That causes the TabPage to scroll to the last updated Control into view.

How can I deactivate this automatic scrolling to the updated Control without disabling the user to scroll inside the TabPage manually?

I'm programmatically generate a TabPage which contains a Panel with various, also programmatically generated controls. The Panel fits it's size to the included controls and can grow larger than the parent TabPage.

I used TabPage.AutoScroll=true; to enable scrolling. The Controls inside the Panel are programmatically generated and receive events, therefore they update their values automatically. That causes the TabPage to scroll to the last updated Control into view.

How can I deactivate this automatic scrolling to the updated Control without disabling the user to scroll inside the TabPage manually?

Share Improve this question edited Mar 5 at 14:31 Poul Bak 10.9k5 gold badges38 silver badges69 bronze badges asked Feb 12 at 10:57 Me.MyBaseMe.MyBase 253 bronze badges 5
  • 1 Have you verified what is the TabIndex sequence in those Controls? See the notes here: Why does my FlowLayoutPanel automatically scroll back to the top when the form loses and regains focus? – Jimi Commented Feb 12 at 11:33
  • 1 Set the page's ActiveControl property to a control that's near the top-left of the page. Typically the first. – Hans Passant Commented Feb 12 at 13:15
  • @jimi; I tried that, but that's not the problem. The autoscroll even triggers, if I don't switch the focus anywhere else. the programmatically updated Controls are a label inside of a UserControl which is again nested into another UserControl (which has a ToolStripLabel which is updated as well) whis is part of the Panel inside the TabPage. – Me.MyBase Commented Feb 14 at 10:32
  • It's not about focus change, it's about the Control that becomes active when the TabPage is presented. Usually, a Control becomes the ActiveControl in a container if its TabIndex is lower than the other Controls in the same container. But you can set the ActiveControl explicitly, as shown in that Q&A. A ScrollableControl (as the Panel - a TabPage is also a Panel) always scrolls to a point where the ActiveControl is visible – Jimi Commented Feb 14 at 12:58
  • @Jimi: sor,y I needed a while to figure it out. that solution was right, yet not as simple as I thought. Because of all those nested controls I hat to set 'this.ParentForm.ActiveControl = this.Parent.Parent.Parent'. Pointing to the Panel mentioned, caused a scroll to the top every second. Pointing to the hosting TabControl fixed the issue. Thanks. :) – Me.MyBase Commented Mar 5 at 11:11
Add a comment  | 

2 Answers 2

Reset to default 1

The solution was to set the Forms ActiveControl Property to a host-control inside the borders of the form.

this.ParentForm.ActiveControl = this.Parent.Parent.Parent.Parent;
  1. Parent: Panel of the hosting UserControl
  2. Parent: hosting UserControl
  3. Parent: Panel of the TabControl
  4. Parent: TabControl

Have you tried the event

 tabControl1.Selecting

You can detect the tab change before it happens with a handler to cancel. Here you can check if the event was initiated by the user or not and cancel the tab change accordingly.

本文标签: winformsC Scrollable TabPage without autoscrollingStack Overflow