Solved

C# - Scrollbars on a Panel

Posted on 2007-04-05
18
733 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

615 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