Solved

C# - Scrollbars on a Panel

Posted on 2007-04-05
18
710 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now