Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , ,

Sponsored Links


How to add scrollbars in C#?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 23-01-2010
MELTRONICS's Avatar
Member
 
Join Date: Aug 2006
Posts: 235
How to add scrollbars in C#?

Sponsored Links
I am new to the C# programming language. And I am facing a little trouble in making the scroll bars. I have done the coding in which everything stay centered on the screen despite window resizing. But now I want to add the vertical and horizontal scroll bars to windows that are too small to display all content. Now when I am trying to make it, the remaining part just get cuts off. Does anyone know how to add scrollbars in C#.?? Please provide me some codes and information regarding the same..!!

__________________
3.2 (northwood)
2gig ram
ATI AIW X800xt 256mb
Gigabyte GA-8knxp 875p Chipset
Optiwrite 8X DVD Burner
Win XP PRO Sp2 (Works Perfectly)
2 SATA Raptor 74gig Raid 0
2 7200 IDE 320gig HD
Reply With Quote
  #2  
Old 23-01-2010
MindSpace's Avatar
Member
 
Join Date: Feb 2008
Posts: 1,839
Re: How to add scrollbars in C#?

You will have to use the scrollbars for displaying the whole program, otherwise it will get cut off at the end. Which means you can't see the ending part that doesn't fit in your screen. You can use the the HScrollBar and VScrollBar class to create the scrollbar. Here are the coding for the same :
For creating the HScrollBar Class (Horizontal Scroll), use the coding :
Code:
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)]
[ComVisibleAttribute(true)]
public class HScrollBar : ScrollBar
For creating the VScrollBar Class (Vertical Scroll), use the coding :
Code:
[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)]
public class VScrollBar : ScrollBar
Reply With Quote
  #3  
Old 23-01-2010
Warner's Avatar
Member
 
Join Date: Mar 2008
Posts: 349
Re: How to add scrollbars in C#?

You can also use the following code for creating the horizontal scroll bar :
Code:
hscrollb = new ScrollBar();
hscrollb.Orientation = Orientation.Horizontal;
spanel1.Children.Add(hscrollb);
And this is the coding for creating the vertical scroll bar :
Code:
vscrollb = new ScrollBar();
vscrollb.Orientation = Orientation.Vertical;
vscrollb.HorizontalAlignment = HorizontalAlignment.Left;
vscrollb.Width = (20);
vscrollb.Height = (80);
Canvas.SetLeft(vscrollb, (50));
spanel1.Children.Add(vscrollb);
Hope that these coding will help you.!
Reply With Quote
  #4  
Old 23-01-2010
Member
 
Join Date: Nov 2008
Posts: 1,192
Re: How to add scrollbars in C#?

I have used the coding of creating the scroll bar in my form. I am providing the code of the same form, so that you can get an idea of creating the scroll bar in program. Here is the code for that :
Code:
public MyForm()
{

InitializeComponent();
myRect = new Rectangle(200,250,140,8000); 

Button b = new Button();
b.Location = new Point(0, 8000);
left corner
b.Text = "dummy_button";

this.Controls.Add(b); 
}

private void MyForm_Scroll(object sender, ScrollEventArgs se)
{
this.Invalidate(true);
}

private void MyForm_VisibleChanged(object sender, EventArgs se)
{
this.Invalidate(true);
}

private void MyForm_Resize(object sender, EventArgs se)
{
this.Invalidate(true);
}
Reply With Quote
  #5  
Old 23-01-2010
opaper's Avatar
Member
 
Join Date: May 2008
Posts: 2,371
Re: How to add scrollbars in C#?

I have written a program that acts as a container, that I can drop onto my form and which has a size that is bigger than the visible part, the invisible part being scrollable to with scrollbars. The coding is very large but can be useful for you later :
Code:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace ScrollPanel {

public delegate void ResetClickHandler( object sender, EventArgs ea );

public class ScrollPane : System.Windows.Forms.UserControl {

public event ResetClickHandler ResetClicked;

public enum ResetButtonBehaviors {
Normal=0,
Hidden=1,
Disabled=2
}

private const int SCROLL_THICKNESS = 16;
private const int EDGE_PADDING = 2;

private ResetButtonBehaviors pResetButtonBehavior =
ResetButtonBehaviors.Normal;

private System.Windows.Forms.PictureBox pbContainer;
private System.Windows.Forms.Button btnReset;
private System.Windows.Forms.VScrollBar vScroller;
private System.Windows.Forms.HScrollBar hScroller;
private System.Windows.Forms.PictureBox ViewPort;
private System.Windows.Forms.PictureBox DataPane;

private System.ComponentModel.Container components = null;

public ScrollPane() {

InitializeComponent();

}

protected override void Dispose( bool disposing ) {
if( disposing ) {
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}

#region Component Designer generated code

private void InitializeComponent()
{
this.pbContainer = new System.Windows.Forms.PictureBox();
this.btnReset = new System.Windows.Forms.Button();
this.vScroller = new System.Windows.Forms.VScrollBar();
this.hScroller = new System.Windows.Forms.HScrollBar();
this.ViewPort = new System.Windows.Forms.PictureBox();
this.DataPane = new System.Windows.Forms.PictureBox();
this.SuspendLayout();

this.pbContainer.BorderStyle =
System.Windows.Forms.BorderStyle.Fixed3D;
this.pbContainer.Name = "pbContainer";
this.pbContainer.Size = new System.Drawing.Size(658, 648);
this.pbContainer.TabIndex = 0;
this.pbContainer.TabStop = false;

this.btnReset.Location = new System.Drawing.Point(270, 270);
this.btnReset.Name = "btnReset";
this.btnReset.Size = new System.Drawing.Size(22, 22);
this.btnReset.TabIndex = 7;
this.btnReset.TabStop = false;
this.btnReset.Text = ".";
this.btnReset.Click += new
System.EventHandler(this.btnReset_Click);

this.vScroller.LargeChange = 100;
this.vScroller.Location = new System.Drawing.Point(624, 45);
this.vScroller.Name = "vScroller";
this.vScroller.Size = new System.Drawing.Size(22, 624);
this.vScroller.TabIndex = 6;
this.vScroller.Scroll += new
System.Windows.Forms.ScrollEventHandler(this.vScro ller_Scroll);

this.hScroller.LargeChange = 100;
this.hScroller.Location = new System.Drawing.Point(5, 270);
this.hScroller.Name = "hScroller";
this.hScroller.Size = new System.Drawing.Size(270, 22);
this.hScroller.TabIndex = 5;
this.hScroller.Scroll += new
System.Windows.Forms.ScrollEventHandler(this.hScro ller_Scroll);

this.ViewPort.BorderStyle =
System.Windows.Forms.BorderStyle.Fixed3D;
this.ViewPort.Location = new System.Drawing.Point(2, 2);
this.ViewPort.Name = "ViewPort";
this.ViewPort.Size = new System.Drawing.Size(418, 422);
this.ViewPort.TabIndex = 8;
this.ViewPort.TabStop = false;

this.DataPane.Location = new System.Drawing.Point(4, 4);
this.DataPane.Name = "DataPane";
this.DataPane.Size = new System.Drawing.Size(52, 56);
this.DataPane.TabIndex = 9;
this.DataPane.TabStop = false;

this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.DataPane,
this.ViewPort,
this.btnReset,
this.vScroller,
this.hScroller,
this.pbContainer});
this.Name = "ScrollPane";
this.Size = new System.Drawing.Size(450, 450);
this.Resize += new System.EventHandler(this.ScrollPane_Resize);
this.ResumeLayout(false);

}
#endregion

private void ScrollPane_Resize(object sender, System.EventArgs ea) {

DataPane.Parent = ViewPort;

pbContainer.Location = new Point( 0, 0 );
pbContainer.Size = new Size( this.Width, this.Height );

hScroller.Location = new Point( EDGE_PADDING, pbContainer.Height -
(EDGE_PADDING + SCROLL_THICKNESS) );
hScroller.Size = new Size( pbContainer.Width - ( (EDGE_PADDING*2) +
SCROLL_THICKNESS), SCROLL_THICKNESS );

vScroller.Location = new Point( pbContainer.Width - (EDGE_PADDING +
SCROLL_THICKNESS), EDGE_PADDING );
vScroller.Size = new Size( SCROLL_THICKNESS, pbContainer.Height - (
(EDGE_PADDING*2) + SCROLL_THICKNESS ) );

btnReset.Size = new Size( SCROLL_THICKNESS, SCROLL_THICKNESS );
btnReset.Location = new Point( pbContainer.Width - (EDGE_PADDING +
SCROLL_THICKNESS), pbContainer.Height - (EDGE_PADDING +
SCROLL_THICKNESS) );

ViewPort.Parent = pbContainer;
ViewPort.Location = new Point( 0, 0 );
ViewPort.Size = new Size( pbContainer.Width - ((EDGE_PADDING*2) +
SCROLL_THICKNESS), pbContainer.Height - ((EDGE_PADDING*2) +
SCROLL_THICKNESS ) );

CalculateScrollbars();
}

private void CalculateScrollbars(){
hScroller.Enabled = ( DataPane.Width > ViewPort.Width );
if( hScroller.Enabled ){
hScroller.Maximum = (DataPane.Width - ViewPort.Width)+120;
hScroller.Minimum = 0;
}

vScroller.Enabled = ( DataPane.Height > ViewPort.Height );
if( vScroller.Enabled ){
vScroller.Maximum = (DataPane.Height - ViewPort.Height)+120;
vScroller.Minimum = 0;
}
}

private void SetButtonBehavior( ResetButtonBehaviors behavior ){
switch( behavior ){
case ResetButtonBehaviors.Normal:
btnReset.Enabled = true;
btnReset.Visible = true;
break;
case ResetButtonBehaviors.Hidden:
btnReset.Visible = false;
btnReset.Enabled = true;
break;
case ResetButtonBehaviors.Disabled:
btnReset.Visible = true;
btnReset.Enabled = false;
break;
}
}

private void btnReset_Click(object sender, System.EventArgs e) {
if( ResetClicked != null ){
ResetClicked( btnReset, e );
}
}

private void vScroller_Scroll(object sender,
System.Windows.Forms.ScrollEventArgs e) {
int val = vScroller.Value;
DataPane.Top = (-1 * val)+1;
}

private void hScroller_Scroll(object sender,
System.Windows.Forms.ScrollEventArgs e) {
int val = hScroller.Value;
DataPane.Left = (-1 * val)+1;
}

public ResetButtonBehaviors ResetButtonBehavior{
get{
return pResetButtonBehavior;
}
set{
pResetButtonBehavior = value;
SetButtonBehavior( pResetButtonBehavior );
}
}

public PictureBox DataPanel {
get{
return this.DataPane;
}
}

public Size DataPaneSize {
get{
return DataPane.Size;
}
set{
DataPane.Size = value;
CalculateScrollbars();
}
}

}
}
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to add scrollbars in C#?"
Thread Thread Starter Forum Replies Last Post
How to change the look or appearance of scrollbars in windows XP samborichie Windows XP Support 2 26-09-2012 03:35 AM
Delphi, TWebBrowser, Disable Scrollbars??? alex198555 Software Development 4 26-07-2010 11:02 PM
Datagridview does not detect scrollbars Fragman Software Development 2 10-06-2009 01:37 PM


All times are GMT +5.5. The time now is 01:45 AM.