C# .NET - Resizing Width of vertical ScrollBar of a Panel in WinForm

Asked By Arun Kumar on 16-Jun-08 10:44 AM
Hi,
I am using a panel in my winform application and i want to increase the width of vertical scrollBar.
How to increase the Width of vertical ScrollBar of a Panel in WinForm

create Scrollbar User Control - Deepak Ghule replied to Arun Kumar on 16-Jun-08 11:15 AM

You have to create scroll bar control for that as there is no other option to set some properties of the scrollbar.

You can create the Scrollbar Graphics like :


  1. Up arrow image
  2. Down arrow image
  3. Thumb top image
  4. Thumb top span image
  5. Thumb middle image
  6. Thumb bottom span image
  7. Thumb bottom image

can write properties exposed:

  • Maximum - int
  • Minimum - int
  • Value - int
  • LargeChange - int
  • SmallChange - int

and the following events:

  • Scroll
  • ValueChanged

and the following custom properties to facilitate to skin:

  • ChannelColor - Color
  • DownArrowImage - Image
  • ThumbBottomImage - Image
  • ThumbBottomSpanImage - Image
  • ThumbMiddleImage - Image
  • ThumbTopImage - Image
  • ThumbTopSpanImage - Image
  • UpArrowImage - Image

Its somewhat big work

but you can do many thing with width of the scrollbar

Refer this link

Here are all the details of it


http://www.codeproject.com/KB/miscctrl/customscrollbar.aspx

hi - alice johnson replied to Arun Kumar on 16-Jun-08 12:17 PM

namespace Scrollbar
{
    public class Form1 :  Form
   {
      private  OpenFileDialog openFileDialog1;
      private  PictureBox pictureBox1;
      private  VScrollBar vScrollBar1;
      private  HScrollBar hScrollBar1;
   
      public Form1()
      {
         InitializeComponent();
      }
   
      private void InitializeComponent()
      {
         this.openFileDialog1 = new OpenFileDialog();
         this.pictureBox1 = new PictureBox();
         this.vScrollBar1 = new VScrollBar();
         this.hScrollBar1 = new HScrollBar();
         this.pictureBox1.SuspendLayout();
         this.SuspendLayout();
       
         this.pictureBox1.BorderStyle = BorderStyle.FixedSingle;
         this.pictureBox1.Controls.AddRange(
             new Control[] { this.vScrollBar1, this.hScrollBar1});
         this.pictureBox1.Dock = DockStyle.Fill;
         this.pictureBox1.Size = new System.Drawing.Size(440, 349);
         this.pictureBox1.DoubleClick += new EventHandler(this.pictureBox1_DoubleClick);

         this.vScrollBar1.Dock = DockStyle.Right;
         this.vScrollBar1.Location = new System.Drawing.Point(422, 0);
         this.vScrollBar1.Size = new System.Drawing.Size(16, 331);
         this.vScrollBar1.Visible = false;
         this.vScrollBar1.Scroll +=
             new ScrollEventHandler(this.HandleScroll);
        
         this.hScrollBar1.Dock = DockStyle.Bottom;
         this.hScrollBar1.Location = new System.Drawing.Point(0, 331);
         this.hScrollBar1.Size = new System.Drawing.Size(438, 16);
         this.hScrollBar1.Visible = false;
         this.hScrollBar1.Scroll +=
             new ScrollEventHandler(this.HandleScroll);
        
         this.ClientSize = new System.Drawing.Size(440, 349);
         this.Controls.AddRange(new Control[] {this.pictureBox1});
         this.Text = "Form1";
         this.Resize += new System.EventHandler(this.Form1_Resize);
         this.pictureBox1.ResumeLayout(false);
         this.ResumeLayout(false);

      }

      [STAThread]
      static void Main()
      {
         Application.Run(new Form1());
      }

private void pictureBox1_DoubleClick (Object sender, EventArgs e)
{
   // Open the dialog box so the user can select a new image.
   if(openFileDialog1.ShowDialog() != DialogResult.Cancel)
   {
      // Display the image in the PictureBox.
      pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
      this.DisplayScrollBars();
      this.SetScrollBarValues();
   }
 }
 
private void Form1_Resize (Object sender, EventArgs e)
{
   // If the PictureBox has an image, see if it needs
   // scrollbars and refresh the image.
   if(pictureBox1.Image != null)
   {
      this.DisplayScrollBars();
      this.SetScrollBarValues();
      this.Refresh();
   }
}
 
public void DisplayScrollBars()
{
   // If the image is wider than the PictureBox, show the HScrollBar.
   if (pictureBox1.Width > pictureBox1.Image.Width - this.vScrollBar1.Width)
   {
      hScrollBar1.Visible = false;
   }
   else
   {
      hScrollBar1.Visible = true;
   }
 
   // If the image is taller than the PictureBox, show the VScrollBar.
   if (pictureBox1.Height >
       pictureBox1.Image.Height - this.hScrollBar1.Height)
   {
      vScrollBar1.Visible = false;
   }
   else
   {
      vScrollBar1.Visible = true;
   }
}
 
private void HandleScroll(Object sender, ScrollEventArgs se)
{
   /* Create a graphics object and draw a portion
      of the image in the PictureBox. */
   Graphics g = pictureBox1.CreateGraphics();
  
   g.DrawImage(pictureBox1.Image,
     new Rectangle(0, 0, pictureBox1.Right - vScrollBar1.Width,
     pictureBox1.Bottom - hScrollBar1.Height),
     new Rectangle(hScrollBar1.Value, vScrollBar1.Value,
     pictureBox1.Right - vScrollBar1.Width,
     pictureBox1.Bottom - hScrollBar1.Height),
     GraphicsUnit.Pixel);

     pictureBox1.Update();
}

public void SetScrollBarValues()
{
   // Set the Maximum, Minimum, LargeChange and SmallChange properties.
   this.vScrollBar1.Minimum = 0;
   this.hScrollBar1.Minimum = 0;

   // If the offset does not make the Maximum less than zero, set its value.
   if( (this.pictureBox1.Image.Size.Width - pictureBox1.ClientSize.Width) > 0)
   {
      this.hScrollBar1.Maximum =
          this.pictureBox1.Image.Size.Width - pictureBox1.ClientSize.Width;
   }
   // If the VScrollBar is visible, adjust the Maximum of the
   // HSCrollBar to account for the width of the VScrollBar. 
   if(this.vScrollBar1.Visible)
   {
      this.hScrollBar1.Maximum += this.vScrollBar1.Width;
   }
   this.hScrollBar1.LargeChange = this.hScrollBar1.Maximum / 10;
   this.hScrollBar1.SmallChange = this.hScrollBar1.Maximum / 20;

   // Adjust the Maximum value to make the raw Maximum value
   // attainable by user interaction.
   this.hScrollBar1.Maximum += this.hScrollBar1.LargeChange;
    
   // If the offset does not make the Maximum less than zero, set its value.   
   if( (this.pictureBox1.Image.Size.Height - pictureBox1.ClientSize.Height) > 0)
   {
      this.vScrollBar1.Maximum =
          this.pictureBox1.Image.Size.Height - pictureBox1.ClientSize.Height;
   }

   // If the HScrollBar is visible, adjust the Maximum of the
   // VSCrollBar to account for the width of the HScrollBar.
   if(this.hScrollBar1.Visible)
   {
      this.vScrollBar1.Maximum += this.hScrollBar1.Height;
   }
   this.vScrollBar1.LargeChange = this.vScrollBar1.Maximum / 10;
   this.vScrollBar1.SmallChange = this.vScrollBar1.Maximum / 20;
  
    // Adjust the Maximum value to make the raw Maximum value
   // attainable by user interaction.
   this.vScrollBar1.Maximum += this.vScrollBar1.LargeChange;
   }

  }
}

Hi friend the above code is for the picture Box give the panel here..Hope  its clear....

Panel with AutoScroll =True - alice johnson replied to Arun Kumar on 16-Jun-08 12:18 PM

will this will satisfy your conditions
Resizing Width of vertical ScrollBar of a Panel in WinForm - Swapnil Salunke replied to Arun Kumar on 16-Jun-08 03:18 PM

Hello Arun
Call SendMessage(lbListbox.hwnd, LB_SETHORIZONTALEXTENT, fTextWidth, 0)
ws one of the way used in vb
Alos you can try it out through the designer by increasing some length.
Here are some links 
   http://lists.trolltech.com/qt-interest/2004-11/thread00680-0.html
http://www.visualbasic.happycodings.com/Forms/code1.html

Hello more over if you want to use the Custom Scroll bars
this is the main event handled by this method

private void CustomScrollbar_MouseDown(object sender, MouseEventArgs e) {
    Point ptPoint = this.PointToClient(Cursor.Position);
    int nTrackHeight = (this.Height - (UpArrowImage.Height + 
                        DownArrowImage.Height));
    int nThumbHeight = GetThumbHeight();
    int nTop = moThumbTop;
    nTop += UpArrowImage.Height;

    Rectangle thumbrect = new Rectangle(new Point(1, nTop), 
              new Size(ThumbMiddleImage.Width, nThumbHeight));
    if (thumbrect.Contains(ptPoint))
    {
    //hit the thumb
    nClickPoint = (ptPoint.Y - nTop);
    this.moThumbDown = true;
    }
    Rectangle uparrowrect = new Rectangle(new Point(1, 0), 
       new Size(UpArrowImage.Width, UpArrowImage.Height));
    if (uparrowrect.Contains(ptPoint))
    {
        int nRealRange = (Maximum - Minimum)-LargeChange;
        int nPixelRange = (nTrackHeight - nThumbHeight);
        if (nRealRange > 0)
        {
            if (nPixelRange > 0)
            {
                if ((moThumbTop - SmallChange) < 0)
                    moThumbTop = 0;
                else
                    moThumbTop -= SmallChange;
                //figure out value
                float fPerc = (float)moThumbTop / (float)nPixelRange;
                float fValue = fPerc * (Maximum - LargeChange);

                moValue = (int)fValue;
                Debug.WriteLine(moValue.ToString());
                if (ValueChanged != null)
                ValueChanged(this, new EventArgs());
                if (Scroll != null)
                Scroll(this, new EventArgs());
                Invalidate();
            }
        }
    }
    Rectangle downarrowrect = new Rectangle(new Point(1, 
              UpArrowImage.Height+nTrackHeight), 
              new Size(UpArrowImage.Width, UpArrowImage.Height));
    if (downarrowrect.Contains(ptPoint))
    {
        int nRealRange = (Maximum - Minimum) - LargeChange;
        int nPixelRange = (nTrackHeight - nThumbHeight);
        if (nRealRange > 0)
        {
            if (nPixelRange > 0)
            {
                if ((moThumbTop + SmallChange) > nPixelRange)
                    moThumbTop = nPixelRange;
                else
                    moThumbTop += SmallChange;
                //figure out value
                float fPerc = (float)moThumbTop / (float)nPixelRange;
                float fValue = fPerc * (Maximum-LargeChange);

                moValue = (int)fValue;
                if (ValueChanged != null)
                ValueChanged(this, new EventArgs());
                if (Scroll != null)
                Scroll(this, new EventArgs());
                Invalidate();
            }
        }
    }
}

private void CustomScrollbar_MouseUp(object sender, MouseEventArgs e) {
    this.moThumbDown = false;
    this.moThumbDragging = false;
}

private void MoveThumb(int y) {
    int nRealRange = Maximum - Minimum;
    int nTrackHeight = (this.Height - (UpArrowImage.Height + 
                                       DownArrowImage.Height)); 
    int nThumbHeight = GetThumbHeight();
    int nSpot = nClickPoint;
    int nPixelRange = (nTrackHeight - nThumbHeight);
    if (moThumbDown && nRealRange > 0) {
        if (nPixelRange > 0) {
            int nNewThumbTop = y - (UpArrowImage.Height+nSpot);

            if(nNewThumbTop<0)
            {
                moThumbTop = nNewThumbTop = 0;
            }
            else if(nNewThumbTop > nPixelRange)
            {
                moThumbTop = nNewThumbTop = nPixelRange;
            }
            else {
                moThumbTop = y - (UpArrowImage.Height + nSpot);
            }

            //figure out value
            float fPerc = (float)moThumbTop / (float)nPixelRange;
            float fValue = fPerc * (Maximum-LargeChange);
            moValue = (int)fValue;
            Debug.WriteLine(moValue.ToString());
            Application.DoEvents();
            Invalidate();
        }
    }
}

private void CustomScrollbar_MouseMove(object sender, MouseEventArgs e) {
    if(moThumbDown == true)
    {
        this.moThumbDragging = true;
    }
    if (this.moThumbDragging) {
        MoveThumb(e.Y);
    }
    if(ValueChanged != null)
        ValueChanged(this, new EventArgs());
    if(Scroll != null)
        Scroll(this, new EventArgs());
    }
}
You can refer this links
http://www.codeproject.com/KB/miscctrl/customscrollbar.aspx

Happy Coding takecare
Try this for resizing scrollbar - Sagar P replied to Arun Kumar on 17-Jun-08 01:15 AM

I think you have to make a custom scroll bar for the same;

You have to program our very own scrollbar control. This is not necessarily an easy task to accomplish. However, once you are done making this work, you should never have to code another one ever again, and if you do, at least you will have a code base to start from with what we have developed here.

In order to make our custom scrollbar, we will need to override and/or respond to the following events:

  • OnPaint - override
  • MouseUp - handle
  • MouseDown - handle
  • MouseMove - handle

 For further code and all details just go thr this link;

http://www.codeproject.com/KB/miscctrl/customscrollbar.aspx

Here they have provided all the details;

Also go thr this link;

http://www.visualbasic.happycodings.com/Forms/code1.html

Best Luck!!!!!!!!!!!!!!!
Sujit.

Programming Resources, Algorithms, E-Book Links, Tutorials, Faqs and Code Examples for java, c, c#, c++, php, asp, assembly, python, visual basic, xml, cobol, delphi, logo, lisp, ruby, forth, fortran,
Ozgur replied to Arun Kumar on 13-Jun-11 07:39 AM
Programmingpages.net - www.programmingpages.net
Best Programming Resources and Source Code Examples for Java, Php, Visual Basic, C++ ,Asp, Python, Javascript, Ada, Cobol ,C, C#, Delphi, Fortran, Logo, Ruby, Xml.