Solved

C# - Scrollbars on a Panel

Posted on 2007-04-05
18
700 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

21 Experts available now in Live!

Get 1:1 Help Now