שתף באמצעות


VB.NET 2010/2012/2013 - Put a form inside a Panel, with scroll bars show when the form moves

Question

Saturday, February 22, 2014 9:29 AM

Good Day everyone

I have a Panel and i want to show a form inside of it, actually i have code for it but the problem is the scroll bars does not show when i'm moving the form that the half of it is in the end of the panel, here's my code

form1.toplevel = false
me.panel1.controls.add(form1)
form1.show()

i set my panel into a autoscroll = to true but nothing happeneds, it only show the scroll bar when you maximize the form inside.

thanks and regards.

All replies (8)

Saturday, February 22, 2014 11:49 AM

I set the AutoScrollMinSize properties to larger than the panel and the scrollbars appeared. Panel size is 200 width, 100 height. Maybe set AutoScrollMinSize to the width and height of Form1?

Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.


Saturday, February 22, 2014 2:13 PM

Hi Aron

You can use the Forms LocationChanged and SizeChanged event handlers to set the scroll.

Here is sample code

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Panel1.AutoScroll = True
        Dim frm2 As Form2 = New Form2()
        AddHandler frm2.LocationChanged, AddressOf Me.Form2_LocationChanged
        AddHandler frm2.SizeChanged, AddressOf Me.Form2_SizeChanged
        frm2.TopLevel = False
        Panel1.Controls.Add(frm2)
        If frm2.Size.Width > Panel1.Size.Width Or frm2.Size.Height > Panel1.Size.Height Then
            Panel1.SetAutoScrollMargin(frm2.Size.Width - Panel1.Size.Width, frm2.Size.Height - Panel1.Size.Height)
        End If
        frm2.Show()

    End Sub

    Private Sub Form2_LocationChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim f As Form = sender
        Panel1.SetAutoScrollMargin(f.Location.X, f.Location.Y)
    End Sub

    Private Sub Form2_SizeChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim f As Form = sender
        If f.Size.Width > Panel1.Size.Width Or f.Size.Height > Panel1.Size.Height Then
            Panel1.SetAutoScrollMargin(f.Size.Width - Panel1.Size.Width, f.Size.Height - Panel1.Size.Height)
        Else
            Panel1.SetAutoScrollMargin(0, 0)
        End If

    End Sub
End Class

Monday, February 24, 2014 6:40 AM

Hi Everyone i have tried your solution but i'm still having a problem

Hi Jeetendra, i tried your solution but when i'm moving it up, to the left and to the right, the scrollbar does not appear but when i drag the form down, it show the vertical scroll. did i miss something or do i have to put some codes again? thanks and regards.


Sunday, July 20, 2014 7:11 PM

Form1.TopLevel = False //Form Top level Property should be False.
Form1.Parent = Me.Panel2 // Target Panel within a MDIform (set target parent panel for a form)
Form1.Show() //show the form

Form1 -->Properties-->autoscroll=True




Sunday, July 20, 2014 10:40 PM

For the record, this is how I have done it, there is absolutely no code in either form for altering size including nothing with anchors

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


Sunday, July 20, 2014 11:14 PM

 I will add that if you want a form inside of a form you will be a lot further ahead just using an MDI Container and an MDI Child to do this if you plan on using a TextBox or a ToolStrip in your Child Form.

 You can do it any of the three ways below but, be warned that if you use a TextBox or a ToolStrip on them then you are going to find bugs in the functionality of one or the other and maybe both controls. I have helped with 2 or three of these same questions and it seems that one way causes a problem of not being able to select the text in the texbox with the mouse and another way will cause the ToolStrip to stop functioning when you click on items with the mouse.

 AS Kevin has shown

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim f As New Form2 With {.TopLevel = False, .AutoSize = False}
        Panel1.Controls.Add(f)
        f.Show()
    End Sub

 A slight mod to the same code

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim f As New Form2 With {.TopLevel = False, .AutoSize = False}
        f.Parent = Panel1
        f.Show()
    End Sub

 

 And using the SetParent Win32 API.

Imports System.Runtime.InteropServices

Public Class Form1
    <DllImport("user32.dll", EntryPoint:="SetParent")> _
    Private Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim f As New Form2 With {.TopLevel = False, .AutoSize = False}
        SetParent(f.Handle, Panel1.Handle)
        f.Show()
    End Sub
End Class

If you say it can`t be done then i`ll try it


Monday, July 21, 2014 7:30 AM

This code seems to work fine.

Form1 code.

Option Strict On

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.CenterToScreen()
        Panel1.AutoScroll = True
    End Sub

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

End Class

Form2 code.

Option Strict On

Public Class Form2

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Left = 0
        Me.Top = 0
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MessageBox.Show("Hello")
    End Sub

End Class

La vida loca


Monday, July 21, 2014 10:01 AM

This code seems to work fine.

 Did you try putting a textbox in the child form and selecting any text in it with the mouse?

If you say it can`t be done then i`ll try it