Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everyone,

How can I focus a panel that is located way below the parent control size. The parent AutoScroll is set to true. See below image.

Controls
pnlControlList Panel - See ImageBox02. This is hidden when form loaded. Shown only when down arrow is click. Items are also panel.
picControlListView PictureBox To diplay the pnlControlList

Please see this image.[^]

ImageBox01 That is the displayed when you run the program. The default selected item is Item Table.
ImageBox02 When you clicked the down arrow top right, all items will be shown.
ImageBox03 This is what I want to happen. I just manually scroll to display the
Item Table.

Note
This is same as a combobox control.

Items Creation
VB
Dim pnl As New Panel
pnl.Padding = New Padding(3, 2, 0, 0)
pnl.Height = 18
pnl.Tag = node
AddHandler pnl.Click, AddressOf ControlList_Click
AddHandler pnl.MouseEnter, AddressOf ControlList_MouseEnter
pnlControlList.Controls.Add(pnl)

Dim lbl2 As New Label
lbl2.Text = "  " & controlStr
lbl2.AutoSize = True
lbl2.Dock = DockStyle.Left
lbl2.Tag = fieldName & "_" & controlStr
AddHandler lbl2.Click, AddressOf ControlList_Click
AddHandler lbl2.MouseEnter, AddressOf ControlList_MouseEnter
pnl.Controls.Add(lbl2)

Dim lbl1 As New Label
lbl1.Text = fieldName
lbl1.AutoSize = True
lbl1.Dock = DockStyle.Left
lbl1.Font = New Font(lblControlListName.Font.FontFamily, lblControlListName.Font.Size, FontStyle.Bold)
lbl1.Tag = fieldName & "_" & controlStr
AddHandler lbl1.Click, AddressOf ControlList_Click
AddHandler lbl1.MouseEnter, AddressOf ControlList_MouseEnter
pnl.Controls.Add(lbl1)
Handlers of some controls
VB
#Region "DROPDOWN LIST"
    Private Sub picControlListView_Click(sender As Object, e As EventArgs) Handles picControlListView.Click
        If pnlControlList.Visible Then
            pnlControlList.Visible = False
        Else
            pnlControlList.Width = TreeView1.Width
            pnlControlList.Visible = True
            For Each c As Control In pnlControlList.Controls
                If c.Controls.Count > 0 Then
                    c.Width = pnlControlList.Width - (2 + IIf(pnlControlList.VerticalScroll.Maximum = 100, 0, 23))
                End If
            Next
            txtControlListFocus.Focus()
        End If
    End Sub

    Private Sub pnlControlListView_Click(sender As Object, e As EventArgs) Handles pnlControlListView.Click
        picControlListView_Click(sender, e)
    End Sub

    Private Sub lblControlListName_Click(sender As Object, e As EventArgs) Handles lblControlListName.Click
        picControlListView_Click(sender, e)
    End Sub

    Private Sub lblControlListType_Click(sender As Object, e As EventArgs) Handles lblControlListType.Click
        picControlListView_Click(sender, e)
    End Sub

    Private Sub txtControlListFocus_LostFocus(sender As Object, e As EventArgs) Handles txtControlListFocus.LostFocus
        pnlControlList.Visible = False
    End Sub
#End Region


What I have tried:

This is what I found in google.
I tried pnlControlList.ScrollControlIntoView(childPanel) but instead of panel getting focus the parent control puts a huge margin/white space above the childs.

Please see this image.[^]
Posted
Updated 16-Aug-17 1:54am
v6
Comments
Graeme_Grant 9-Aug-17 10:29am    
The image and description do not give enough information. Also, we can't see your code from here...
Nelek 10-Aug-17 4:35am    
Code was added

1 solution

I'm having problems reproducing your problem so I have put together a test project that works. You can download and try it here: WinformScrollIntoViewPanel.zip - Google Drive[^]

To test this further, I've put a scrollable Panel inside a scrollable Panel and added buttons to the test form to scroll main Panel elements and sub Panel elements into view. Everything works as expected. Here is the test code for scrolling into view:
VB
Private Sub ButtonClicked(sender As Object, e As EventArgs) _
    Handles butGroupBox1.Click, butPictureBox1.Click, butListBox1.Click,
    butComboBox1.Click, butPictureBox2.Click, butListBox2.Click,
    butGroupBox2.Click, butComboBox2.Click

    Dim mainPanelCtrl As Control = Nothing
    Dim subPanelCtrl As Control = Nothing

    Select Case True

        ' -- MAIN PANEL CONTROLS --

        Case sender Is butGroupBox1
            mainPanelCtrl = GroupBox1

        Case sender Is butListBox1
            mainPanelCtrl = ListBox1

        Case sender Is butComboBox1
            mainPanelCtrl = ComboBox1

        Case sender Is butPictureBox1
            mainPanelCtrl = PictureBox1

        '-- SUB PANEL OF MAIN PANEL CONTROLS --

        Case sender Is butGroupBox2
            mainPanelCtrl = Panel2      ' scroll the sub-panel
            subPanelCtrl = GroupBox2    ' then the control

        Case sender Is butListBox2
            mainPanelCtrl = Panel2
            subPanelCtrl = ListBox2

        Case sender Is butComboBox2
            mainPanelCtrl = Panel2
            subPanelCtrl = ComboBox2

        Case sender Is butPictureBox2
            mainPanelCtrl = Panel2
            subPanelCtrl = PictureBox2

    End Select

    If mainPanelCtrl IsNot Nothing Then
        Panel1.ScrollControlIntoView(mainPanelCtrl)
    End If

    If subPanelCtrl IsNot Nothing Then
        Panel2.ScrollControlIntoView(subPanelCtrl)
    End If

End Sub

Something to note, the inner sub Panels will need to be smaller than the viewport of the main Panel. If not, elements in the sub Panel will not scroll into view. The reason for this is that the ScrollIntoView(...) method only scrolls the upper coordinates into view. if the sub Panel is too large for the Viewport, some elements in the sub Panel may appear to not scroll into view as the top (or left) of the element is outside the main Panel viewport but within the sub Panel viewport.
 
Share this answer
 
v2
Comments
hansoctantan 7-Sep-17 9:06am    
i actually ignored this functionality on my project, the auto scrolling. thank you very much for your answer. not to mention a sample source code, it really helps. I figured out the problem. when i use Panel.ScrollControlIntoView(control), the Panel is not visible. I guess it it messes up the design.
Graeme_Grant 7-Sep-17 9:11am    
Yes, there are a lot of factors to making it work. If it is complicated, it is time to review and simplify.

I'm glad that you found an alternative solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900