0

I've created a form and displayed it inside the panel but sadly the form can't fit. So I need to use Panel.Autoscroll = True in order to navigate the whole form.

When I click the textbox1.text in the lowest part of the form, the panel automatically scrolls up and the textbox can't be seen. Even if I scroll down it continuously scrolls up automatically. How can I stop it from scrolling up?

Here is my code:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Form2.TopLevel = False 
    Me.Panel1.Controls.Clear() 
    Me.Panel1.Controls.Add(Form2) 
    Form2.Show() 
End Sub 

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick 
    Label2.Text = Date.Now.ToString("dd/MMM/yyyy ddddddddd") 
    Label1.Text = Date.Now.ToString("h:mm:ss tt")
End Sub

I realized the panel scrolls up when the date text is set to the label. Is there a way to prevent this?

djv
  • 15,168
  • 7
  • 48
  • 72
  • Is this winforms or asp.net? – djv Dec 02 '14 at 15:31
  • I tried to reproduce this behavior but could not. Can you clarify "displayed a form inside a panel"? Is that a `Form` you put inside a `Panel` in another `Form`? – djv Dec 02 '14 at 15:59
  • first i've created a form with a 2 panels. first panel with dock in the second in order to have scroll bars. second created new form to be display that is longer than the panel with a textbox below. the form is borderless. lastly i display the second form with this code. form2.TopLevel = False Me.Panel1.Controls.Clear() Me.Panel1.Controls.Add(form2) form2.Show() – Ezra Bugarin Dec 02 '14 at 16:05
  • Can you show some code? How do you put the form inside the panel? – djv Dec 02 '14 at 16:06
  • > form2.TopLevel = False > Me.Panel1.Controls.Clear() > Me.Panel1.Controls.Add(form2) > form2.Show() – Ezra Bugarin Dec 02 '14 at 16:17
  • I can only make it scroll to the top by resizing Form1. Do you have a loop that does that? – djv Dec 02 '14 at 16:20
  • i dont have loop. it just automatically scrolls up when i click the textbox i dont know what does that. – Ezra Bugarin Dec 02 '14 at 16:25
  • do you think its a bug? – Ezra Bugarin Dec 02 '14 at 16:33
  • 1
    rethink your UI design – Ňɏssa Pøngjǣrdenlarp Dec 02 '14 at 16:47
  • 1
    Please post your code in your question. Don't hide it within the comments. – Bart Dec 02 '14 at 16:48
  • Since I can't reproduce it with a minimal amount of code, there is something else going on. I don't think it's a bug. – djv Dec 02 '14 at 16:59
  • ok i see whats the cause. its the timer. i have a clock display below in the status strip. And whenever it updates time, it makes the panel scrolls up which means every second. – Ezra Bugarin Dec 02 '14 at 17:10
  • ive tried to recreate it in a new project. heres my code. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Form2.TopLevel = False Me.Panel1.Controls.Clear() Me.Panel1.Controls.Add(Form2) Form2.Show() End Sub Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick Label2.Text = Date.Now.ToString("dd/MMM/yyyy ddddddddd") Label1.Text = Date.Now.ToString("h:mm:ss tt") – Ezra Bugarin Dec 02 '14 at 17:18
  • @EzraBugarin Edit your original question and add the code. – djv Dec 02 '14 at 18:20
  • Be careful, `Me.Panel1.Controls.Clear()` does not dispose of any objects in that collection. It just moves them to an invisible container until you need them again. – LarsTech Dec 02 '14 at 19:27
  • @LarsTech interesting, so some reference is preserved making them ineligible for gc? – djv Dec 02 '14 at 19:41
  • @Verdolino Correct (until the form is closed and disposed). Better to dispose of the objects explicitly if that becomes an issue. – LarsTech Dec 02 '14 at 19:46
  • 1
    I think I will use Hans' extension method: http://stackoverflow.com/questions/1969705/clear-controls-does-not-dispose-them-what-is-the-risk – djv Dec 02 '14 at 19:56

1 Answers1

1

You can resolve your problem by using your own panel and overriding the ScrollToControl function:

Public Class PanelEx
  Inherits Panel

  Protected Overrides Function ScrollToControl(c As Control) As Point
    Return Me.DisplayRectangle.Location
  End Function    
End Class

Replace your Panel1 panel with this new one in your ToolBox after rebuilding your solution.

LarsTech
  • 80,625
  • 14
  • 153
  • 225