2015-06-20 48 views
-2

好吧,我想创建一个列表或一个具有picturebox和double值varuaible的类的数组。我想知道的是,如何在Windows窗体上显示该类,并带有图片,并且当您单击图片时,会弹出一个消息框并说出该图片的值。该列表或数组必须是动态的,因为它的大小将在运行时间内发生变化。我希望这解释了我需要的东西。带图片框变量和双精度的类。还有更多

我到目前为止所做的是我能够创建一个动态数组显示在窗体上,但我无法分配一个双值。所以当我点击图片时,我可以让它移动,但我不知道如何为每个图片分配一个特定的值。分配PictureBox的图像

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; 
     } 
    } 

我的代码: 我的代码做的东西在图像上点击

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); 
     } 
    } 

这是我在创建类的尝试:

public class Container 
    { 
     public PictureBox picture { get; set; } 
     public double number { get; set; } 
    } 

public void AddToList(List<Container> o, Container ContainerToAdd) 
    { 
     o.Add(ContainerToAdd); 
    } 

大部分的代码来自于我提出的问题的更早的帮助

+0

我们展示你的代码。 – deathismyfriend

+0

这是代码。 – GK28

回答

1

你已经有“标签”属性,为什么不使用那个? 否则你可以像这样扩展你的图片框。

public class PictureBoxExt : PictureBox 
    { 
     [Browsable(true)] 
     public double SomeValue { get; set; } 
    } 

现在使用PictureBoxExt,而不是PictureBox的 设置图片框属性 “someValue中” 的值这样的。

pictureBoxExt.SomeValue = 0.123d; 

后来pictureBoxExt点击事件,

private void pictureBoxExt1_Click(object sender, EventArgs e) 
     { 
      PictureBoxExt pic = sender as PictureBoxExt; 
      if (pic != null) { 
       MessageBox.Show("Double Value" + pic.SomeValue); 
      } 

     } 
+0

非常感谢你! – GK28