Page 1 of 1

Using scrollbars

Posted: Tue Aug 24, 2010 2:50 pm
by MrAksel
Hey what is the code for scrollbars both V and H for scrolling a panel or something else like in a listbox or a multiline textbox?

Re: Using scrollbars

Posted: Tue Aug 24, 2010 4:59 pm
by mandai
This would switch selected items in a listbox:
Code: Select all
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        HScrollBar1.LargeChange = 1
        HScrollBar1.Maximum = ListBox1.Items.Count - 1
    End Sub

    Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
        ListBox1.SelectedIndex = HScrollBar1.Value
    End Sub
And this would scroll lines in a textbox:
Code: Select all
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        VScrollBar1.LargeChange = 1
    End Sub

    Private Sub VScrollBar1_Scroll_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles VScrollBar1.Scroll
        If TextBox1.Text.Length > 0 Then
            Dim lineStarts As List(Of Integer) = New List(Of Integer)

            For i As Integer = 0 To TextBox1.Text.Length - 2
                If TextBox1.Text.Substring(i, 2) = vbCrLf Then
                    lineStarts.Add(i)
                End If
            Next

            VScrollBar1.Maximum = lineStarts.Count
            If VScrollBar1.Value < lineStarts.Count Then
                TextBox1.SelectionStart = lineStarts(VScrollBar1.Value)
            Else
                TextBox1.SelectionStart = TextBox1.Text.Length
            End If
            TextBox1.ScrollToCaret()
        End If
    End Sub

Re: Using scrollbars

Posted: Tue Aug 24, 2010 5:03 pm
by MrAksel
Dude thanks!! You own at coding :!!!!

Re: Using scrollbars

Posted: Tue Aug 24, 2010 9:24 pm
by GoodGuy17
Please lock this topic Aksel.

Re: Using scrollbars

Posted: Tue Aug 24, 2010 9:32 pm
by Axel
Looks like this isn't closed yet. xD