0

I want to detect Scrollbar height and current Position in my Form.

For height I'm using systeminformation.horizontalscrollbarheight

How do I detect the current Position?

edit

I want to detect height and position from library.

Vikash Kumar
  • 31
  • 11
  • 1
    What you mean in "current position" cursor position or position of form? – L_J Jul 05 '18 at 10:07
  • end point of scrollbar ,,,suppose if user scrolls halfway of the form and stops then I want the position of scrollbar – Vikash Kumar Jul 05 '18 at 10:12
  • https://stackoverflow.com/questions/18034975/how-do-i-find-position-of-a-win32-control-window-relative-to-its-parent-window – kuch3n Jul 05 '18 at 10:21
  • There are many different kinds of scrollbars in Winforms. It would be too obvious if you talked about the dedicated controls. In general you get them when you set the AutoScroll property to *true*, you then get the position from AutoScrollPosition. Some controls do not allow you to find out at all, ListView is an example. When you talk of "scrolls halfway of the form" then you want AutoScrollPosition. – Hans Passant Jul 05 '18 at 10:35

1 Answers1

0

Use VerticalScroll and HorizontalScroll More infos on MSDN

Example code, showing the scroll informations after the form was scrolled:

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Scroll(object sender, ScrollEventArgs e)
        {
            this.label1.Text = $"scrollbarHeight: {SystemInformation.HorizontalScrollBarHeight}\n"
                                + $"verticalScrollPos: {this.VerticalScroll.Value}\n"
                                + $"horizontalScrollPos: {this.HorizontalScroll.Value}";
        }

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.label1 = new System.Windows.Forms.Label();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.label1);
            this.panel1.Location = new System.Drawing.Point(134, 165);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(458, 459);
            this.panel1.TabIndex = 0;
            this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(36, 23);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(64, 25);
            this.label1.TabIndex = 1;
            this.label1.Text = "label1";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(11F, 24F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.AutoScroll = true;
            this.ClientSize = new System.Drawing.Size(505, 421);
            this.Controls.Add(this.panel1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Scroll += new System.Windows.Forms.ScrollEventHandler(this.Form1_Scroll);
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Label label1;
    }
}
codeteq
  • 1,502
  • 7
  • 13