Okay, I want to make a list or an array of a class that has picturebox and double value varaibles. What I want to know is, how can I Display that class on the Windows form, with a picture, and when you click on the picture, you get a message box pop up and say the value of the that picture. The list or array must be dynamic since the size of it will be changing through out the run time. I hope this is explains what I need.
What I have so far is that I'm able to create a dynamic array of picturebox to show on the form, but I'm not able to assign a double value. So when i click on the image, I can make it move but I don't know how to assign each image a specific value. My code to do stuff to the image on click:
private void Picturebox_ClickFunction(object sender, EventArgs e)
{
PictureBox pb2 = (PictureBox)sender; // you need to cast(convert) the sende to a picturebox object so you can access the picturebox properties
if (pb2.Location.Y >= 250)
{
pb2.Top -= 20;
// MessageBox.Show(pb2.Tag);
}
else
{
pb2.Top += 20;
}
}
my code for assigning the picturebox images:
void print_Deck(List<Container> b, double []a)
{
double n;
y = 250; x = 66;
for (int i = 0; i < 13; i++)
{
pb2[i] = new PictureBox();
pb2[i].Click += new System.EventHandler(this.Picturebox_ClickFunction);
pb2[i].Visible = true;
pb2[i].Location = new Point(0, 0);
this.Size = new Size(800, 600);
pb2[i].Size = new Size(46, 65);
pb2[i].SizeMode = PictureBoxSizeMode.StretchImage;
pb2[i].Location = new Point(x, y);
n = a[i];
im = face(n);
pb2[i].Image = im;
this.Controls.Add(pb2[i]);
x = x + 20;
Container NewContainer = new Container();
NewContainer.picture = pb2[i];
NewContainer.number = n;
AddToList(b, NewContainer);
}
}
And this is my attempt at creating the class:
public class Container
{
public PictureBox picture { get; set; }
public double number { get; set; }
}
public void AddToList(List<Container> o, Container ContainerToAdd)
{
o.Add(ContainerToAdd);
}
Most of the code is from the help i got from asking question earlier on parts of this
You already have "tag" property why do not use that one ? otherwise you can extend your picture box like this.
public class PictureBoxExt : PictureBox
{
[Browsable(true)]
public double SomeValue { get; set; }
}
Now use PictureBoxExt instead of PictureBox set the value of picture box property "SomeValue" like this.
pictureBoxExt.SomeValue = 0.123d;
later on pictureBoxExt click event would be
private void pictureBoxExt1_Click(object sender, EventArgs e)
{
PictureBoxExt pic = sender as PictureBoxExt;
if (pic != null) {
MessageBox.Show("Double Value" + pic.SomeValue);
}
}