Vertical Scrollbar

MiPakTeh 1,476 Reputation points
2021-02-26T08:09:34.363+00:00

Hi All,

I test if Vertical Scrollbar can work with listBox.
below th code.It look like cannot go to the bottom Item.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Learning_10
{
    public partial class Form1 : Form
    {
        // VScrollBar vScroller = new VScrollBar();


        public Form1()
        {
            InitializeComponent();


        }

        private void Form1_Load(object sender, EventArgs e)
        {
            vScrollBar1.Height = listBox1.Height;
            vScrollBar1.Left = listBox1.Left + listBox1.Width;
            vScrollBar1.Top = listBox1.Top;
            vScrollBar1.Maximum = listBox1.Height;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 500; i++)
            {
                listBox1.Items.Add(i);

            }
        }

        private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
           listBox1.SelectedIndex = e.NewValue;

        }
    }
}
    
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,605 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 83,096 Reputation points
    2021-02-26T08:38:07.25+00:00

    vScrollBar1.Maximum is wrong, it should be = listBox1.Items.Count; (after the ListBox has been filled)
    and in vScrollBar1_Scroll, it should be :
    listBox1.TopIndex = e.NewValue;


0 additional answers

Sort by: Most helpful