.NET集合和访问对象方法

时间:2009-08-20 20:35:26

标签: c# collections

我是GUI编程的新手,需要一些图片框的帮助。

我的想法是我有一个图片盒列表。当用户点击我想要的(例如)更改所选的一个BorderStyle属性为Fixed3D,但将剩余的集合边框更改为FixedSingle(或类似的东西)。做这样的事情的正确方法是什么?我想更大的图片是如何让一个类的方法在没有任何相关信息的情况下调用另一个类的方法?

class myPicture
{
  private int _pictureNumber;
  private PictureBox _box;
  public myPicture(int order)
  {
    _box = new List<PictureBox>();
    _box.Click += new System.EventHandler(box_click);
    _pictureNumber = order;
  }
  public void setBorderStyle(BorderStyle bs)
  {
    _box.BorderStyle = bs;
  }
  public void box_click(object sender, EventArgs e)
  {
    //here I'd like to call the set_borders from myPicturesContainer, but I don't know or have any knowledge of the instantiation
  }
}

class myPicturesContainer
{
  private List<myPicture> _myPictures;
  //constructor and other code omitted, not really needed...
  public void set_borders(int i)
  {
    foreach(myPicture mp in _MyPictures)
      mp.setBorderStyle(BorderStyle.FixedSingle);
    if(i>0 && _MyPictures.Count>=i)
      _MyPictures[i].setBorderStyle(BorderStyle.Fixed3d);
  }
}

1 个答案:

答案 0 :(得分:0)

您需要在Clicked课程中创建myPicture个活动,并在点击该活动时举起该活动。然后,您需要在myPicturesContainer中为您拥有的myPicture的每个实例附加此活动。

这是我的意思的非常简单示例:

class myPicture
{
    public event Action<Int32> Clicked = delegate { };

    private int _pictureNumber;

    public void box_click(object sender, EventArgs e)
    {
        this.Clicked(this._pictureNumber);
    }
}

class myPicturesContainer
{
    private List<myPicture> _myPictures;

    public void set_borders(int i)
    {
        foreach (myPicture mp in _myPictures)
        {
            mp.Clicked += pictureClick;
        }
    }

    void pictureClick(Int32 pictureId)
    {
        // This method will be called and the pictureId
        // of the clicked picture will be passed in
    }
}