Link to home
Start Free TrialLog in
Avatar of Venkatesan Dinesh
Venkatesan DineshFlag for India

asked on

PictureBox Inside panel C#/C++ windows application

I have panel that has multiple pictureboxes which are added dynamically at runtime.  The pictureboxes can be moved around the panel individually or as a user defined group.  I want the user to be able to drag a mouse across the screen to "define" which pictureboxes are selected.  

here is the code

 private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog d = new OpenFileDialog();

            // allow multiple selection
            d.Multiselect = true;

            // filter the desired file types
            d.Filter = "JPG |*.jpg|PNG|*.png|BMP|*.bmp";

            // show the dialog and check if the selection was made
            if (d.ShowDialog() == DialogResult.OK)
            {
                int n = 4;
                foreach (string image in d.FileNames)
                {
                    // create a new control
                    PictureBox pb = new PictureBox();

                    // assign the image
                    pb.Image = new Bitmap(image);

                    // stretch the image
                    pb.SizeMode = PictureBoxSizeMode.StretchImage;
                    // set the size of the picture box
                    pb.Height = pb.Image.Height / 10;
                    pb.Width = pb.Image.Width / 10;

                    // add the control to the container
                    panel1.Controls.Add(pb);
                }
            }
and now my query is if i select any of the add control that should get highlight and i need to move around panel1.

now i can count how many control are there but i cant find name of the each control

sample code

   MessageBox.Show(panel1.Controls.Count.ToString());

thanks in advance

User generated image
Avatar of sarabande
sarabande
Flag of Luxembourg image

if PictureBox is your own class, add a new string member 'pictureName' and change the constructor to take an input string. when you create the PictureBox, you could pass the string 'image' (or extract only filename from image).

if PictureBox is 3rdParty class, derive a new class from PictureBox, say 'class NamedPictureBox' and define a string member 'pictureName' and a constructor that takes the string as argument. you then would create NamedPictureBox instead of PictureBox but still could use a NamedPictureBox pointer wherever a PictureBox pointer was required.

both ways would allow you to get the name of the stored picturebox by providing a GetName member function.

Sara
now i can count how many control are there but i cant find name of the each control
Your dynamic controls don't have names because you never assigned names to them.  You can assign names, but it's not actually necessary.  Just collect all the "selected" controls in a List<> and update their positions using the references in it.

The pictureboxes can be moved around the panel individually or as a user defined group.  
Does this mean you already have it so they can be moved around?...or are you asking how to make it so you can move them around?  If you want to be able to move them around individually then you'll have to wire up some events for them when you create them.  This can be done with a common handler and you simply use the "sender" parameter to determine which control was the source of the event.

I want the user to be able to drag a mouse across the screen to "define" which pictureboxes are selected.  
So...it seems like this part is NOT done yet (since you have the word "want" in there).  This is called "rubberbanding" or "rubberband selection", and is usually accomplished with ControlPaint.DrawReversibleFrame().  You draw the rectangle once and it appears, then you draw the SAME rectangle again and it gets erased (thus the "reversible" part in the name).  Then you update the rectangle to the new position as the mouse is dragged and repeat the process.  Once the mouse is release you simply see which controls intersect the "selection rectangle" using Rectangle.Intersects().  Collect all "hits" in a List<> so you know which controls to manipulate as a group.
Avatar of Venkatesan Dinesh

ASKER

best best solution... by reading i can understand, but im just a fresher can u share some code to do this, please..

code for "I want the user to be able to drag a mouse across the screen to "define" which pictureboxes are selected."
and   now i can count how many control are there but i cant find name of the each control,

me already shared some coding if there is any error please let me know
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.