Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# - Scrollbars on a Panel

Posted on 2007-04-05
18
Medium Priority
?
763 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

670 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