Solved

C# - Scrollbars on a Panel

Posted on 2007-04-05
18
717 Views
Last Modified: 2008-01-09
Hi guys 'n gals,

Is it at all possible to add a border to a Panel, as well as a Scrollbar? If not, what element could I use to achieve this, which would allow me to embed Pictureboxes?

Cheers!
0
Comment
Question by:Cyber-Drugs
  • 10
  • 8
18 Comments
 
LVL 5

Expert Comment

by:Yttribium
ID: 18856452
Attach a vScrollBar to your panel, stretch it to size on the right side as you wish.  And double click the scroll bar in the designer, it should generate a Scroll event for you (if not, manually add it)

Also in your picture adding code, add a TAG to the picture objects and give the tag the value of the Y coordinate.

private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
    foreach (Image c in panel2.Controls)
    {
        if (c.Name != "vScrollBar1")
             c.Top = ((int)c.Tag) - vScrollBar1.Value;
     }
 }

You may just want to check what the Min/Max is of your scroll bar and use the "value" differently, this just illustrates a way to scroll your items.

Hope this helps
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18856490
Sounds simple enough, I'll give it a go in a couple minutes and let you know the result.

Cheers!
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18856613
I left my laptop at home today which has my source code for my original project, so created a dummy project here to test this answer you just gave me, and got the following errors:


Error      1      'System.Drawing.Image' does not contain a definition for 'Name'      \\claymore-server\User Docs\JustinN\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs      35      23      WindowsApplication1
Error      2      'System.Drawing.Image' does not contain a definition for 'Top'      \\claymore-server\User Docs\JustinN\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs      36      23      WindowsApplication1




and here is the source code:


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

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            PictureBox pb = new PictureBox();
            pb.Left = 2;
            pb.Top = 2;
            pb.Width = 148;
            pb.Height = 148;
            pb.Name = "geiger";
            pb.Image = Image.FromFile("geiger.jpg");
            pb.SizeMode = PictureBoxSizeMode.StretchImage;

            pb.Tag = pb.Top.ToString();

            this.panel1.Controls.Add(pb);
        }

        private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            foreach (Image c in panel1.Controls)
            {
                if (c.Name != "vScrollBar1")
                    c.Top = ((int)c.Tag) - vScrollBar1.Value;
            }
        }
    }
}
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 5

Expert Comment

by:Yttribium
ID: 18856624
You have to go through the Controls in the panel, not the Image collection.
That's why I said Control c not Image or PictureBox.
0
 
LVL 5

Accepted Solution

by:
Yttribium earned 500 total points
ID: 18856630
Try using this instead:  Hopefully this works

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

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            PictureBox pb = new PictureBox();
            pb.Left = 2;
            pb.Top = 2;
            pb.Width = 148;
            pb.Height = 148;
            pb.Name = "geiger";
            pb.Image = Image.FromFile("geiger.jpg");
            pb.SizeMode = PictureBoxSizeMode.StretchImage;

            pb.Tag = pb.Top.ToString();

            this.panel1.Controls.Add(pb);
        }

        private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            foreach (Control c in panel1.Controls)
            {
                if (c.Name != "vScrollBar1")
                    c.Top = ((int)c.Tag) - vScrollBar1.Value;
            }
        }
    }
}
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18856639
Ok, I changed it to

foreach (Control c in panel1.controls)


and now get the following error:

Specified cast is not valid.
0
 
LVL 5

Expert Comment

by:Yttribium
ID: 18856646
Eh?  I don't have this problem... which line is this "cast" happening?
0
 
LVL 5

Expert Comment

by:Yttribium
ID: 18856651
Really wish this thing had an EDIT feature, I see your problem.  You did :

 pb.Tag = pb.Top.ToString();

It should be

pb.Tag = pb.Top;

Tag is an Object type.
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18856668
Aha, that did the trick, cheers once again!

Before I close off the question, could you possible tell me how to increase how much the increment for each scroll is, please? Currently it looks like one click is 1 pixel...

Cheers!
0
 
LVL 5

Expert Comment

by:Yttribium
ID: 18856669
To make it clearer, you can cast certain types into other types, depending on what's in the object.

Compiler will not complain if you do:

string s = "1";
object o = s;
int i = (int)o;

But will fall over on run-time, when it tries to cast this "string" into an int.
If you did:
int s = 1;
object o = s;
int i = (int)o;

It will compile and run just fine.

If you do
string s = "1";
int i = (int)s;

It will give you error pre-compile.  Hope that explains the cast problem.  You can use Tag to store an object, which you can cast to any object (provided you know that the cast you do is safe).  Tags are very useful for "extra" info.  They don't HAVE to be strings.
0
 
LVL 5

Expert Comment

by:Yttribium
ID: 18856677
Oh, There's a minimum, maximum value on the vScroll.  You can programatically set this in this sort of way:

If you know you have intRows of images, (each 100pixels with 5 pixel margin space)  You could say that...

vScrollBar1.Min = 0
vScrollBar1.Max = intRows * (100 + 5);

Then when you move the bar up and down (like a progress bar) the position of the slider is a "value" relative to its max-min.   Currently the default is 0-100, so it will move 1px for every 1% you move the bar.

0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18856688
You are a fountain of information, thank you yet again!!

Just noticed you're new to EE, looking forward to bumping into you more often! ;o)
0
 
LVL 5

Expert Comment

by:Yttribium
ID: 18856706
Thank you!  And no problem!  Yes, I'm looking forward to this too, who knows :o)
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18857892
Ok, to the most part, this works...

Except about probably 10 pixels (if even that much) of the bottom does not display... The amount missing, funnily enough is the size of the botton to scroll down...

Is there a way to maybe add some padding to solve this issue?
0
 
LVL 5

Expert Comment

by:Yttribium
ID: 18857938
Yep, add an arb number to the "Max" of the scroll bar
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18858066
Ok, I seem to have made an endless loop which gives the scrollbar an infinite length....


        private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            foreach (Control c in panel2.Controls)
            {
                if (c.Name != "vScrollBar1")
                    c.Top = ((int)c.Tag) - vScrollBar1.Value;
            }
            vScrollBar1.Maximum = vScrollBar1.Maximum + 10;
        }
0
 
LVL 5

Expert Comment

by:Yttribium
ID: 18858103
Well it's not an Endless loop, just that everytime the scroll event fires, you're adding the Maximum by 10 pixels.  You need to put this Code to somewhere that only is executed once, maybe I suggset put that where you insert the images.
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18858194
Good plan, works perfectly!!!


Now going to try out some TreeView code from another question.


Thanks again!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question