加载应用程序时将滚动条置于滚动条顶部

问题描述 投票:0回答:1

我遇到一个问题,当我的侧边栏加载到面板中时,滚动条一直到按钮的底部。我需要它位于侧边栏按钮的顶部。我尝试了一些方法,但似乎都不起作用。我有一种感觉,在我的代码中的某个地方,它将最后一个按钮识别为第一个按钮或类似的按钮。那个或我有冲突的容器属性,忽略了我放入的代码。

    ' Declare VScrollBar1 with WithEvents
Private WithEvents VScrollBar1 As New VScrollBar()

Private Sub MainScreen_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' Call a method to initialize dynamic controls
    InitializeDynamicControls()
    Panel1.Visible = True

    ' Add the VScrollBar control to the form
    Controls.Add(VScrollBar1)
    ' Set the properties of the VScrollBar control
    VScrollBar1.Dock = DockStyle.Right
    VScrollBar1.Minimum = 0
    VScrollBar1.Maximum = Panel2.Height
    VScrollBar1.LargeChange = Panel2.Height - Panel1.Height ' Set the large change value
    VScrollBar1.SmallChange = 20 ' Set the small change value

    ' Set the value of the VScrollBar to 0 to ensure it starts at the top
    VScrollBar1.Value = 0

    ' Set the vertical scroll position of Panel2 to 0
    Panel2.VerticalScroll.Value = 0
End Sub

Private Sub InitializeDynamicControls()
    ' Define an array with button names in descending order
    Dim buttonNames() As String = {"Button 6", "Button 6", "Button 6", "Button 6", "Button 6", "Button 6", "Button 6", "Button 6", "Button 6", "Button 6", "Button 6", "Button 6", "Button 6", "Button 6", "Button 6", "Button 6", "Button 6", "Button 5", "Button 4", "Button 3", "Button 2", "Document understanding"}

    ' Add buttons dynamically based on the buttonNames array
    For Each buttonName As String In buttonNames
        Dim button As New System.Windows.Forms.Button() ' Fully qualify Button class
        button.Text = buttonName
        button.Dock = DockStyle.Top
        AddHandler button.Click, AddressOf OpenForm ' Add click event handler
        Panel2.Controls.Add(button)
    Next
End Sub

' Inside the MainScreen class
Private WithEvents docunderstanding As New DocUnderstanding()

Private Sub OpenForm(sender As Object, e As EventArgs)
    ' Handle button click event
    Dim button As System.Windows.Forms.Button = DirectCast(sender, System.Windows.Forms.Button)

    ' Check which button was clicked
    Select Case button.Text
        Case "Document understanding"
            ' Show docunderstanding UserControl
            docunderstanding.Show()

            ' Hide Panel1
            Panel1.Visible = False

            ' Bring docunderstanding UserControl to the front
            docunderstanding.BringToFront()
        Case Else
            ' Handle other buttons here
            MessageBox.Show("This module is not yet developed, please email [email protected] for urgent need!")
    End Select
End Sub

' Event handler for the Scroll event of the VScrollBar
Private Sub VScrollBar1_Scroll(sender As Object, e As ScrollEventArgs) Handles VScrollBar1.Scroll
    ' Adjust the location of Panel2 based on the scrollbar value
    Panel2.Location = New Point(Panel2.Location.X, -VScrollBar1.Value)
End Sub

这可能是我放置按钮的顺序吗?非常感谢任何帮助。

vb.net winforms scrollbar
1个回答
0
投票

我已经解决了。

我将 Dockstyle.Top 更改为 Dockstyle.Bottom:

    Private Sub InitializeDynamicControls()
    ' Define an array with button names in descending order
    Dim buttonNames() As String = {"Button 6", "Button 6", "Button 6", "Button 6", "Button 6", "Button 6", "Button 6", "Button 6", "Button 6", "Button 6", "Button 6", "Button 6", "Button 6", "Button 6", "Button 6", "Button 6", "Button 6", "Button 5", "Button 4", "Button 3", "Button 2", "Document understanding"}

    ' Add buttons dynamically based on the buttonNames array
    For Each buttonName As String In buttonNames
        Dim button As New System.Windows.Forms.Button() ' Fully qualify Button class
        button.Text = buttonName
        button.Dock = DockStyle.Bottom
        AddHandler button.Click, AddressOf OpenForm ' Add click event handler
        Panel2.Controls.Add(button)
    Next
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.